在看c++primer时,直接从io标准库开始了,以前的东西都没看,从而遗留了这个问题,等只是增长了再反过来看
先上代码
- int ival;
- // read cin and test only for EOF; loop is executed even if there are other IO failures
- while (cin >> ival, !cin.eof())
- {
- cin.setstate(ios_base::badbit);
- if (cin.bad()) // input stream is corrupted; bail out
- throw runtime_error("IO stream corrupted");
- if (cin.fail()) { // bad input
- cerr<< "bad data, try again"; // warn the user
- cin.clear(istream::failbit); // reset the stream
- cin.setstate(ios_base::failbit);
- continue; // get next input
- }
- // ok to process ival
- }
- return 0;