Dealing with character data input for an numbe r ic (int or double) value.

If someone enters a string or character to the following:

int x;
cin << x;

The program no longer accepts input, cin is in an error state. You can recognize this case, and deal wit it:

int x;
cin >> x;
while ( ) // Check if there was an input error
     cin.clear(); // Clear the error
     cin.ignore(100,'\n'); // Remove everything in the input buffer (up to the last 100 characters)
     cout << "You must enter a number" << endl;
     cin >> x 
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2015-09-17 - JimSkon
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback