【发布时间】:2015-08-23 08:35:20
【问题描述】:
我正在制作一个需要用户输入生产订单(长 7 位)的应用,如下所示:
int order = 0;
cout << "Insert the order number: ";
cin >> ordem;
如何防止用户输入字母?喜欢“I2345G789”吗?
这样做,我的应用程序将进入无限循环。我正在考虑使用这样的功能:
bool isLetter(int a)
{
string s = to_string(a);
for (int i = 0; i < s.size()-1; i++)
{
if (isdigit(s[i]))
{
return false;
}
else
return true;
}
}
然后:
if (isLetter(order))
{
cout << "Insert only numbers \n";
}
但它不起作用。为什么?以及如何改进代码?
PS:我对编程很陌生,所以,对于初学者的任何错误,我深表歉意。
【问题讨论】:
-
@Wimmel 这提供了一个不同的 解决方案,但不会帮助 OP 理解他最初的错误。我不会将此标记为重复。
-
为什么要否决我的问题?你们太严格了,拜托!菜鸟怎么会这样学..
标签: c++ windows c++11 type-conversion