<<< General State Handling Logic | Index | Simple State Check >>> |
After
cin.clear();
the stream goes back to
cin.good()
state. Now what?
Great news: the data in the input stream buffer hasn't been lost!
For example,
#include <iostream> using std::cin; using std::cout; int main() { int value = 0; cin >> value; if ( !cin ) { // cin is not good, more checks required: if ( cin.bad() ) { // cin is corrupted return 1; // signal error back to the operating system } if ( cin.eof() ) { // nothing to do: we want all inputs to end like this! return 0; // signal success to the operating system } if ( cin.fail() ) { cin.clear(); // go back to good state... // do something to recover... char ch = 0; while ( cin >> ch ) { cout << "Recovered char(" << int( ch ) << ")\n"; } } } // use value, everything is ok return 0; }
<<< General State Handling Logic | Index | Simple State Check >>> |