新手:清除了错误标志位,为什么还是死循环!
#include <iostream>using namespace std;int main(){ cout<<"input a number:"<<endl; int ival; while(cin>>ival,!cin.eof()){ if(cin.bad()){ throw runtime_error("Can not fix"); } if(cin.fail()){ cerr<<"Not the correct type needed"<<endl; cin.clear(istream::failbit); continue; } } return 0;}
#include <iostream>using namespace std;int main(){ cout<<"input a number:"<<endl; int ival; while(cin>>ival,!cin.eof()){ if(cin.bad()){ throw runtime_error("Can not fix"); } if(cin.fail()){ cerr<<"Not the correct type needed"<<endl; cin.clear(); //cin.clear(istream::failbit); cin.sync(); continue; } } return 0;}