While循环中的If循环中的While循环
While loop in a If loop in a while loop
在下面的代码中,我遇到了这个问题,当我第一次运行它时,它工作得很好,但是在随后的循环中,当我选择选项1时,没有任何显示只是回到"菜单",它只在第一次工作,之后没有时间,有人能解释这个问题吗?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int choice=0;
while(choice != 2){
cout << "(1)Create" << endl;
cout << "(2)Exit" << endl;
cin >> choice;
if(choice == 1){
int choice2;
while(choice2 != 7){
cout << "You chose one " << endl;
cin >> choice2;
}
}
}
}
我不明白你的问题,但这对我来说很好…(实际上是相同的代码,只是更整洁)。
int main()
{
int choice = 0;
while (choice != 2)
{
std::cout << "(1)Create" << std::endl;
std::cout << "(2)Exit" << std::endl;
std::cin >> choice;
if (choice == 1)
{
int choice2 = 0;
while (choice2 != 7)
{
std::cout << "You chose one " << std::endl;
std::cin >> choice2;
}
}
}
return 0;
}
问题可能是,您没有在声明后初始化choice2变量,这意味着它包含垃圾值
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- while循环中while循环的时间复杂度是多少
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 擦除while循环中迭代的元素
- 为什么我不能在 while 循环中创建线程?
- 退出简单while循环时出现问题
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 嵌套While循环不起作用(C++问题)
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 使用 GMP 的 while 循环出现问题
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 为什么在 while 循环中返回表达式不起作用
- 如何使用 do while 循环确定最高值和最低值
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- OpenCV QT,显示视频的帧(不使用while循环)
- 输入循环 while 函数
- c++中的循环(while)
- 为什么这段代码没有循环(while)