What's the best way to check for incorrect data input

I'm using this to check for a non float entered by the user. Is there a better way, I know you can read in a string and convert it.

while (cout << float_disp && !(cin >> float_from_keyboard))
{
cin.clear();
cin.ignore();
cout << "Invalid input; please re-enter" << endl;
}
Last edited on
I've often seen if(!(cin >> type)) on this site, so that should be enough. According to the reference operator>> reads the input as a string and parses it to convert to the numeric type, so it's the same thing you would do.
Topic archived. No new replies allowed.