Error Message

How do I put an error message to this? like for example: you accidentally pressed a letter intead of a number in grades then an error message will show



#include<iostream>
using namespace std;

int main()
{
double assignment1, assignment2, seatwork1, seatwork2, quiz1, quiz2, majorexam, grade, major, assignment, seatwork, quiz;
string name;

cout<<"Enter Student name: ";
cin>>name;

cout<<"Enter grade for assignment 1: ";
cin>>assignment1;
cout<<"Enter grade for assignment 2: ";
cin>>assignment2;
cout<<"\nEnter grade for seatwork 1: ";
cin>>seatwork1;
cout<<"Enter grade for seatwork 2: ";
cin>>seatwork2;
cout<<"\nEnter grade for quiz 1: ";
cin>>quiz1;
cout<<"Enter grade for quiz 2: ";
cin>>quiz2;
cout<<"\nEnter grade for major exam: ";
cin>>majorexam;


float aveAssignment=(assignment1+assignment2)/2;
assignment = (aveAssignment)*.1;
float aveSeatwork=(seatwork1+seatwork2)/2;
seatwork = (aveSeatwork)*.2;
float aveQuiz=(quiz1+quiz2)/2;
quiz = (aveQuiz)*.3;
major = majorexam*.4;
grade = (assignment+seatwork+quiz+major);
cout<<"Final Grade is: "<<grade;
return 0;
}
You can test the state of stream (after attempt to read):
1
2
3
if ( !(cin>>assignment1) ) {
  std::cout << "input failed; not a number";
}

Note that the stream does still have the offending content, so you have to handle that if you want to continue.
You can do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <limits>
using namespace std;

double getNum(const std::string& prm)
{
	double d {};

	while ((std::cout << prm) && (!(std::cin >> d) || std::cin.peek() != '\n')) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return d;
}

int main()
{
	string name;

	cout << "Enter Student name: ";
	getline(cin, name);

	const auto assignment1 {getNum("Enter grade for assignment 1: ")};
	const auto assignment2 {getNum("Enter grade for assignment 2: ")};
	const auto seatwork1 {getNum("Enter grade for seatwork 1: ")};
	const auto seatwork2 {getNum("Enter grade for seatwork 2: ")};
	const auto quiz1 {getNum("Enter grade for quiz 1: ")};
	const auto quiz2 {getNum("Enter grade for quiz 2: ")};
	const auto majorexam {getNum("Enter grade for major exam: ")};

	const auto aveAssignment {(assignment1 + assignment2) / 2.0};
	const auto assignment {aveAssignment * .1};
	const auto aveSeatwork {(seatwork1 + seatwork2) / 2.0};
	const auto seatwork {aveSeatwork * .2};
	const auto aveQuiz {(quiz1 + quiz2) / 2.0};
	const auto quiz {aveQuiz * .3};
	const auto major {majorexam * .4};
	const auto grade {assignment + seatwork + quiz + major};

	cout << "Final Grade is: " << grade << '\n';
}



Last edited on
Both codes doesn't work
Last edited on
What do you mean 'doesn't work'? I tried it with VS2019.


Enter Student name: foo bar
Enter grade for assignment 1: q
Not a number
Enter grade for assignment 1: 1
Enter grade for assignment 2: 3w
Not a number
Enter grade for assignment 2: 2
Enter grade for seatwork 1: q2
Not a number
Enter grade for seatwork 1: 3
Enter grade for seatwork 2: 4
Enter grade for quiz 1: 5
Enter grade for quiz 2: 6
Enter grade for major exam: a
Not a number
Enter grade for major exam: 7
Final Grade is: 5.3

It doesn't work on the shell either.
Maybe C++17 required?
Yes, C++17 is required. But in 2021 with C++20 now the current standard, C++17 should be the minimum.
I agree, just a shame that the shell only supports C++14
Topic archived. No new replies allowed.