cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
What's the best way to check for incorre
What's the best way to check for incorrect data input
Sep 13, 2013 at 1:13am UTC
pronesti
(54)
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
Sep 13, 2013 at 1:30am UTC
Sep 13, 2013 at 2:37am UTC
maeriden
(872)
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.