当我运行代码并在终端中输入实例1时,它进入“ else”状态并中断。但是我给它提供了一个数字,所以我很难理解它为什么会这样。有人可以帮忙澄清一下吗?
int main()
{
vector<int> positions;
int p;
for(int i = 0; i <= 3; i++){
cout << "Enter a number: ";
cin >> p;
if(isdigit(p)){
positions.push_back(p);
} else
{
cout << "Please provide numbers from 0 to 100!" <<"\n";
break;
}
}
return 0;
}
该函数是针对字符定义的,如果您编写 isdigit('1').
另外isdigit(49) = true
,由于ascii 49中的数字为1,所以isdigit(49) = true;
检查isdigit()
以下网址的参考:http : //www.cplusplus.com/reference/cctype/isdigit/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句