Input Validation (don't know what I did wrong)
Mar 9, 2015 at 3:54pm UTC
Hello guy, I'm new here
I finally got figure it out how to stop infinite loop when I input a character instead of integer. Now I can't input integer first because it want character first. I want to make character read as no letters and double read as no integer what am I doing wrong here?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
cout
<< "\nPart 1 (validate an integer) \n"
<< "Enter a number: " ;
cin >> insert; cin.ignore(80, '\n' );
while (!(cin >> insert) || insert !=(int )insert)
{
if (!(cin >> insert))
{
cin.clear(); cin.ignore(80, '\n' );
cout << "Please no letters: " ;
}
else
{
cin.clear(); cin.ignore(80, '\n' );
cout << insert <<" not an integer: " ;
}
}
cout << "Good! " << insert << " is an integer!" << endl;
cin.get();
Mar 9, 2015 at 4:03pm UTC
Try something like this.
1 2 3 4 5 6 7 8 9 10
cout << "Enter a number: " ;
cin >> insert;
)
while (!cin.good()){
cout << "Error msg: " ;
cin.clear();
cin.ignore(80, '\n' );
cin >> insert;
}
Mar 9, 2015 at 4:15pm UTC
I believe I'm not suppose to use cin.good() yet.
I'm trying to make while(!(reading this) || (that)) and print out either one of the two as I input character or double.
Mar 9, 2015 at 4:21pm UTC
Topic archived. No new replies allowed.