I dont know how to cout for error

I have an assignment which is to do a program that accepts only grades, in which I used float. but I dont how to add cout for invalid input. please help me. i really dont know how to do it. I been trying to figure it out for two days.

#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main(int argc, char** argv) {

float a=0, b=0, c=0, d=0, grade = 0;


cout << " Enter your Grade on Quiz no. 1 : \t";
cin >> a ;
cout << " Enter your Grade on Quiz no. 2 : \t";
cin >> b ;
cout << " Enter your Grade on Quiz no. 3 : \t";
cin >> c;
cout << " Enter your Grade on Quiz no. 4 : \t";
cin >> d;
system ("cls");

while ( a < -1 && b < -1 && c < -1 && d < -1 ) {
cout << " Enter your Grade on Quiz no. 1 : \t";
cin >> a ;
cout << " Enter your Grade on Quiz no. 2 : \t";
cin >> b ;
cout << " Enter your Grade on Quiz no. 3 : \t";
cin >> c;
cout << " Enter your Grade on Quiz no. 4 : \t";
cin >> d;
system ("cls");

if ( a > -1 && b > -1 && c > -1 && d > -1){
do{

cout << " Quiz#" << "\t\tGrade\n";
cout <<" 1\t\t" << a << endl;
cout <<" 2\t\t" << b << endl;
cout <<" 3\t\t" << c << endl;
cout <<" 4\t\t" << d << endl;
cout << "\n";

cout << " TOTAL AVE."<< "\t\tREMARKS\n";

grade = (a+b+c+d)/ 4;

if (grade> 94 && grade< 101 ) {

cout << " " << setw(4)<< left<<grade << "\t\tExcellent!\n\n";
}

else if (grade> 89 && grade< 95){

cout << " " << setw(4)<< left<< grade << "\t\tVery Satisfactory!\n\n";
}

else if (grade> 84 && grade< 90 ) {

cout << " " << setw(4)<< left<<grade << "\t\tSatisfactory!\n\n";
}

else if (grade> 79 && grade< 85 ) {

cout << " " << setw(4)<< left<<grade << "\t\tFine!\n\n";
}

else if (grade> 74 && grade< 80 ) {

cout << " " << setw(4)<< left<<grade << "\t\t\Fair!\n\n";
}

else if (grade> -1 && grade < 75 ) {

cout << " " << setw(4)<< left<<grade << "\t\t\Poor.\n\n";
}

else {
cout << " " << setw(4)<< left<< " " << "\t\t\Invalid Input.\n\n";
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
	int x;
	cout<<"Enter a value for x : ";
	cin>>x;

	//Use cin.fail() to check if input type matches variabe type
	//test by enetering a character for x.

	cin.fail() ? cout<<"Wrong input"<<endl : cout<<"Good to go!"<<endl;

	system("pause");
	return 0;
}
You can use !cin instead of cin.fail() as well. Easiest way would be simply to do
std::cout << (std::cin >> x ? "Good to go!" : "Wrong input") << std::endl;
Last edited on
smooth..
Thanks.. but I still dont get it.. I have variables that a,b,c,d that should be numbers only. thank you again
What dont you understand?
sorry.. the solutions that you provided are working.. it just that the program should repeat asking until the user give a correct input. it should only proceed to the next process if a valid input is given. thank you so much.. and sorry. I am really having a hard time here. I hope you will still help me.
We provided you means of checking for correct input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
	int x;
	while(!(cin>>x))
	{
		cout<<"Wrong. Try again"<<endl;
		cin.clear();
		cin.ignore();
	}
	cout<<"Good to go"<<endl;
	system("pause");
	return 0;
}
You may want to ignore everything in the buffer and not just the last character. What if they enter asdfasdf instead of just a. Something like cin.ignore(1024, '\n'); or cin.ignore(numeric_limits<streamsize>::max(), '\n');
you ideas are good. but after the first "while", it doesnt proceed to the next process. i dont know why
You have a few errors in your program with braces. You definitely need to get in the habit of using good bracing because that is what I believe was the cause of the problem here.

First, there is no closing brace to the while loop. On the line after system("cls"), you need to put a closing brace. Why? Think about it logically and you should be able to see why it makes no sense.

Secondly, you have what appears to be the beginning of a do-while loop. The do{ comes on the line after the large if-else statement, but it doesn't serve any purpose and you could do without it. Not only that, but it isn't even structured properly. Here's what that loop should look like:

1
2
3
do {
    // code
} while ( condition );


But like I said, you don't need it so just delete do{.

It should be able to compile safely after that.

EDIT: Also note that you don't need to do cin.fail() or cin.ignore() or anything like that. Just fix what I pointed out and it will run as you expected it to. Get in the habit of looking over everything line by line. Happy coding, mang. :-)
Last edited on
Topic archived. No new replies allowed.