I'm new to C++. A section of my current program is giving me a bit of a problem. The computer asks the user to input an answer (an integer) to a math equation, but I want to set it so that if the user accidentally puts a character in, the computer will notify the user about their 'invalid integer'. If the user does, it just goes into an endless loop instead. Any help/suggestions on what I can fix?
My code:
int num=16*9;
int prim;
cout << " What is 16*9? (Use keypad, please)\n";
cin >> prim;
while (isnan(prim)||prim!=num)
{
cout << "You sure? I think it's something else. Try again. ";
cin >> prim;
continue;
}
if (prim==num)
{
cout << "The answer is indeed " << num << ". Not bad for a human!";
}
Any advice and/or solutions is greatly appreciated!
First things first, when posting, use the [code] tags. Ok, well looking at your while loop, what does the, isnan function do? Also reconsider using the continue statement. Remember that when continue is encountered, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration.