《C++ Primer练习8.1节》中关于IO库的操作
#include <iostream>
#include <stdexcept>
using namespace std;
istream &f(istream &in)
{
int v;
while (in>>v, !in.eof()){
if (in.bad()){
throw runtime_error("IO流错误");
}
if (in.fail()){
cerr << "数据错误,请重试" << endl;
in.clear();
in.ignore(100, '\n');
continue;
}
cout << v << endl;
}
in.clear();
return in;
}
int main()
{
cout << "请输入一些整数,按Ctrl+Z结束" << endl;
f(cin);
return 0;
}
输入7.6,结果会输出7和"数据错误"请重试。下面是VS Code调试结果
请输入一些整数,按Ctrl+Z结束
7.6
7
数据错误,请重试
如果输出"数据错误,请重试",不是应该执行后面的continue吗?为什么还会执行cout << v << endl?
求大神解答!