举个例子:
#include
using namespace std;
int main()
{
int n;
while (1)
{
cin>>n; //输入字符会引发cin异常
if (cin.fail())
{
cout<<"ERROR,Plase input again:";
cin.clear(); //先清除异常状态
cin.ignore(999, '\n'); //再清理缓冲区,清理999个字符或者碰到'\n',给个足够大的数字就好
continue;
}
break;
}
cout<<"OK!!"<return 0;
}