如何处理捕捉这个异常
int ix
cin>>ix;
如果输入是字符,该如何判断异常,如何捕捉
望大家懂的赐教!
问题点数:100、回复次数:5Top
1 楼rambo_yzw(小蛇)回复于 2006-01-03 23:40:59 得分 0
不好意思,漏了个分号
int ix;
cin>>ix;Top
2 楼cenlmmx(学海无涯苦作舟)回复于 2006-01-04 00:13:49 得分 30
cout << "good() = " << cin.good() << endl;
cout << "eof() = " << cin.eof() << endl;
cout << "fail() = " << cin.fail() << endl;
cout << "bad() = " << cin.bad() << endl << endl;
cin.clear(); //重設
cout << "Reset error state ...\n";
cout << "good() = " << cin.good() << endl;
cout << "eof() = " << cin.eof() << endl;
cout << "fail() = " << cin.fail() << endl;
cout << "bad() = " << cin.bad() << endl;Top
3 楼whyglinux(山青水秀)回复于 2006-01-04 00:38:10 得分 40
int main()
{
int ix;
cerr << "Input an integer: ";
cin >> ix;
while ( !cin ) {
cin.clear();
cin.ignore();
cerr << "Input error. Input again: ";
cin >> ix;
}
cout << ix << endl;
}
Top
4 楼forverlove()回复于 2006-01-04 00:44:03 得分 30
这个不被认为是异常,在BCB中可以用int atoi(char);这个函数来输入.
如:
string strin;
int i;
cin>>strin;
if (i=atoi(strin.c_str() ) {
return;
}Top
5 楼rambo_yzw(小蛇)回复于 2006-01-04 00:53:54 得分 0
谢谢大家!
问题能解决就好Top




