do
{
cout << "Enter a positive integer: ";
cin >> n;
}while ( n <= 1 || n == 0);
我应该如何编码,以便如果我输入“1234”,它会拒绝,因为那里有一个数字“1”。
最佳答案
一种方法是从用户输入一个 std::string
,然后使用 std::string::find
查看是否存在 0 或 1。完成后,尝试将字符串转换为整数。
或者,如果你想保留所有“数字”,你总是可以使用 % 10
来提取最右边的数字:
if (n % 10 < 2){
/*not allowed: I'm assuming n is positive here*/
}
紧随其后
n/= 10
删除该数字,然后重复,直到剩下零。显然,您需要分别测试从 0 开始的 n
的特殊情况。
关于c++ - 做.. while 循环 : How should I do so that the input (int) does not include digit '1' or '0' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40152294/