初次运行是没有问题,但是程序存在隐患,原因是你的while()在输入流没有异常的时候是个死循环。改成如下就可以了:
#include
using namespace std;
int main()
{
int ival;
char rflg_;
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;
}
cout << ival <
// control the loop's quit
cout << "Do you want to quit?";
cin >> rflg_;
if (rflg_ == 'Y' || rflg_ == 'y'){
break;
}
}
return 0;
}