我發現這個代碼片段在一本書:利空數據,再試一次
int ival;
// read cin and test only for EOF; loop is executed even if there are other IO failures
while (cin >> ival, !cin.eof()) {
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
istream::iostate cin_state = cin.rdstate();
continue; // get next input
}
// ok to process ival
}
如果我在命令窗口中單擊「F」,然後無數的「壞數據,再試一次」,並cin_state爲0x02,這等於壞道。 Failbit尚未清楚,爲什麼?
可能的重複[爲什麼下面的c + +代碼保持輸出「壞數據,再試一次」?](http://stackoverflow.com/questions/4297469/why-the-following-c-code-keeps-output- bad-data-try-again) – jerry 2015-02-16 12:46:29