福尔。while 循环:我应该怎么做,使输入 (int) 不包含数字 '1' 或"0"?
Do.. while loop: How should I do so that the input (int) does not include digit '1' or '0'?
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
删除该数字,并重复,直到剩下零。显然,您需要单独测试n
从0开始的特殊情况。
相关文章:
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何确定integer_sequence在编译时是否包含给定的数字?
- 如何将较大数字中从0到9的单个数字放入只包含数字的数组中
- 如何识别字符串只包含数字
- 如何在C++中创建 2d 数组,其中一部分包含循环计数器,另一部分包含数字列表?
- 如何对包含数字和符号的字符串进行排序?
- 如何在包含数字的.bmp(二进制图像)中执行组件标记9 4 3 6
- 分类包含数字和单词的字符串向量
- 如何检查整数是否包含数字 0 和 1
- 求所有和中最大的和,它包含数字和
- 用于确定 2x2 正方形是否包含数字 1-4(无重复)的算法
- 将字符串(包含数字)转换为整数并返回该整数
- 函数来检查字符串是否包含数字
- 对包含数字的字符串进行排序
- 将包含数字的字符串解析为整数数组
- 在检测字符串是否包含数字字符时,这有什么问题
- 用于查找包含数字的非重叠范围的有效数据结构
- 福尔。while 循环:我应该怎么做,使输入 (int) 不包含数字 '1' 或"0"?
- c++递归检查number是否包含数字
- 将包含数字和单词的文本文件列表转换为整数2D数组