为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
Why is my do-while loop not breaking when I enter a number that is accepted?
无论我输入接受还是不接受的值,循环都会继续运行。
#include <iostream>
#include <vector>
using namespace std;
int main(){
int size;
bool accepted = ((size == 0) || (size == 3) || (size == 4) || (size == 5));
/************************************************************************/
do {
cout << "Enter number of digits in code (3, 4 or 5): " << flush;
cin >> size;
} while (!accepted);
/************************************************************************/
//static_cast<const int>(size);
cout << size;
return 0;
}
布尔值不会每次迭代都会更新。仅在开始时。要使逻辑工作,您需要将
accepted = ((size == 0) || (size == 3) || (size == 4) || (size == 5));
循环内部。
int main(){
int size;
bool accepted;
/************************************************************************/
do {
cout << "Enter number of digits in code (3, 4 or 5): " << flush;
cin >> size;
accepted = ((size == 0) || (size == 3) || (size == 4) || (size == 5));
} while (!accepted);
/************************************************************************/
//static_cast<const int>(size);
cout << size;
return 0;
}
相关文章:
- 我想做一个彼此不同但重复出现的数字
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 为什么兰德每次都给我几乎相同(但略有不同)的数字
- 当我输入字符类型的数字时,为什么我无法获得整数?
- 如何修复我的最大公约数代码?它适用于除零和零以外的所有数字
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- 我的问题是关于C++中数字和序列的重复以及如何打印它们?
- 对于使用高级服务的分钟数和应付金额,我不断得到相同的数字
- 我想使用哈希图打印非重复的数字;
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 为什么我的位移给出不正确的数字
- 如何检查用户的输入是否有效以及我正在寻找的数字?
- 有人可以解释我这个代码吗?它给出与我们输入的数字相反
- 我需要静态转换我的数字模板值吗
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 为什么 rand() 给我的数字超出了我定义的间隔?(昨天工作正常)
- 为什么这段代码会给我的数字加一个额外的零
- C++完成了随机猜测我的数字游戏Lookover