Wrong input handling with cin
Hello,
I am trying to write code that will accept user input, and any non-integer value will return an error message followed by a prompt to re-enter.
My current method of doing this works fine if a user enters characters, however if the user enters a float my program becomes confused.
Whats the best way to do this?
Here's my code:
Code:
do {
test=false;
cout << "\nEnter steps per turn\n\n> ";
if (((cin >> spt)==0)) //Input and error handling
{
cin.clear();
cin.ignore(10000,'\n');
cout << "\nOnly positive integers please!\n";
test=true;
}
}while(test);
Thanks a bunch!
J G