[求助]一段流状态的查询和控制代码。
#include <iostream>
using namespace std;
void main(){
int ival;
while (cin >> ival,!cin.eof()){
if (cin.bad())
throw runtime_error("IO stream corrupted");
if (cin.fail()){
cerr << "bad data,try again";
cin.clear(istream::failbit);
continue;
}
}
}
书上抄下来的,结果运行结果不是所期望的!运行下就知道了,期望输入整形值。using namespace std;
void main(){
int ival;
while (cin >> ival,!cin.eof()){
if (cin.bad())
throw runtime_error("IO stream corrupted");
if (cin.fail()){
cerr << "bad data,try again";
cin.clear(istream::failbit);
continue;
}
}
}
若遇到输入非整形值就提示出错并重新输入。
但是我输入为字符的时候就出现了无限循环!
点解,谢谢!