65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
void main()
{
int age;
while(1)
{
cout <<"输入年龄";
cin>>age;
if(cin.fail()) //判断输入的是不是数字
{
cout << "not a number" << endl;
cout << "请从新输入" << endl;
cin.clear();//加上这两句,可以清除错误标记,并清空缓冲区内容。
cin.ignore();
}
else break;
}
}