c++ - Bad data, try again -
i found code fragment in book:
int ival; // read cin , test eof; loop executed if there other io failures while (cin >> ival, !cin.eof()) { if (cin.bad()) // input stream corrupted; bail out throw runtime_error("io stream corrupted"); if (cin.fail()) { // bad input cerr<< "bad data, try again"; // warn user cin.clear(istream::failbit); // reset stream istream::iostate cin_state = cin.rdstate(); continue; // next input } // ok process ival }
if click 'f' in command window, countless "bad data, try again", , cin_state 0x02, equal badbit. failbit has not been clear, why?
the issue f
never removed input stream, cin >> ival
keeps trying read on , on again.
you need skip past it. see, example, how istream::ignore( ) work?
Comments
Post a Comment