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.
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";
#include <iostream>
usingnamespace 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;
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.
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 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. :-)