#include
#include
#include
using namespace std;
istream& f(istream& in)
{
int val;
while (in >> val, !in.eof()) {//遇到文件结束符停止
if (in.bad())
throw runtime_error("IO流错误");
if (in.fail()) {
throw runtime_error("输入数据错误,请重试");
in.clear();
in.ignore(100, '\n');
continue;
}
cout << val << endl;
}
in.clear();
return in;
}
int main()
{
cout << "请输入整数以ctrl+Z结束" << endl;
f(cin);
return 0;
}
下面是书本后面程序的运行时候的答案:
123
1
2
3
7.6
7
数据错误,请重试
a
数据错误请重试
4
^Z
但是我运行的时候是下面这样的