C++输入出错处理???
我想编一个程序要求输入一个数。。
如果输入字母就提示出错。
怎么判断?
{
int num;
cin> > num;
//num为字母时报错。。
}
菜鸟问题。帮忙解决一下!~~
[解决办法]
根据ASCII码判断即可.
[解决办法]
cin> > num
if(isalpha(num)) //#include <stype.h>
{
cout < < "error!!!...num is char " < <endl;
return;
}
else
//right;
[解决办法]
void main()
{
int num;
cout < < "Please input the number of the month ";
cin> > num;
while(!cin) //如果输入的不是int型值
{
cin.clear();
cin.ignore(1000, '\n '); //清理缓冲区
cout < < "Input error!Please input integer num. " < <endl;
cin> > num;
}
}