#include<iostream>
#include<string>
using namespace std;
int strtoint(const string &str)
{
int num = 0;
for (unsigned int i = 0; i < str.length(); i++)
{
if ((str.at(i) >= '0') && (str.at(i) <= '9'))
{
num = num * 10 + str.at(i) - '0';
}
else
{
throw str.at(i);
}
}
return num;
}
int main()
{
string str;
int i_value;
cout << "please input a number string :\n";
cin >> str;
try
{
i_value = strtoint(str);
if (i_value > 10000)
throw i_value;
}
catch (char)
{
cout << "input string include illegal character:" <<char<< endl;
i_value = -1;
}
catch (int)
{
cout << "number is too big:" << i_value << endl;
}
cout << i_value << endl;
return 0;
}
catch (char c)
{
cout << "input string include illegal character:" << c << endl;
i_value = -1;
}
catch (int i_value)
{
cout << "number is too big:" << i_value << endl;
}
这么改试试
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632