我正在尝试创建一个菜单,但我的代码一直在循环
I'm trying to create a menu but my code keeps looping
我正在尝试创建一个菜单,用户在其中选择一个数字来执行函数,但我的代码一直在循环。即使在 for 循环之外,我的 2d 数组也会保持循环语句。我找到了一种停止循环的方法
cout << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
但后来我用一个简单的 cout 语句(usernum ==3 部分(对其进行了测试,并且它也循环了。我不确定我做错了什么?我认为这与我的while循环有关,但我不确定。我将不胜感激任何帮助!!
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
// function #1, finds the minimum value in a array
int findMin(int a[], int size){
int i;
int min;
min = a[0];
for (i = 1; i < size; ++i) {
if(min > a[i]){
min = a[i];
}
}
return min;
}
// will write other functions later
int main()
{
int userNum = 0;
int arraySize = 4;
int array[arraySize];
int i;
int j;
int rowSize = 3;
const int colSize = 4;
int array2[rowSize][colSize];
cout << "MENU" << endl;
cout << "-1: exit" << endl;
cout << "1: minimum value of integer array" << endl;
cout << "2: computes the average of all elements in a 2D array" << endl;
cout << "3: Is it a leap year?" << endl;
cout << "4: calculates addition, subtraction, multiplation, divison" << endl;
cout << "5: read a text file and store each word into a vector of string" << endl;
cout << "6: count the frequency of each word" << endl;
将
cout << "7: remove all the stop words from words" << endl;
cout << "Please input a number from the menu:" << endl;
cin >> userNum;
while (userNum != -1) {
if (userNum == 1) {
cout<<"Enter 4 array elements: ";
for(i=0; i< arraySize; i++){
cin>>array[i];
}
cout << "minimum value of integer array: " << findMin(array, arraySize) << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
else if (userNum == 2) {
cout << "here are the elements in the 2d array.";
for (i=0; i < rowSize; i++ ){
cout<< endl;
for (j =0; j < colSize; j=j+1){
array2[i][j] = 1+(rand()%9);
cout << array2[i][j] << " ";
}
}
cout << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
else if (userNum == 3){
cout << "hahah" << endl;
}
l
else {
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
}
cout << "exited";
return 0;
}
您的userNum == 3
案例在新userNum
中没有读取,所以这就是它循环的原因。一旦userNum
是 3,它就再也不会改变。
修复它的一种方法是将"获取用户输入"代码移动到一个单独的函数中,然后在循环结束时调用它。这样,您就不必到处重复代码,并且在实现新案例时也不会忘记添加它。
像这样:
int getMenuItem()
{
cout << "MENU" << endl;
cout << "-1: exit" << endl;
cout << "1: minimum value of integer array" << endl;
cout << "2: computes the average of all elements in a 2D array" << endl;
cout << "3: Is it a leap year?" << endl;
cout << "4: calculates addition, subtraction, multiplation, divison" << endl;
cout << "5: read a text file and store each word into a vector of string" << endl;
cout << "6: count the frequency of each word" << endl;
cout << "7: remove all the stop words from words" << endl;
cout << "Please input a number from the menu:" << endl;
int input;
cin >> input;
return input;
}
int main()
{
...
userNum = getMenuItem(); // <- Before the loop, to get the initial selection
while (userNum != -1) {
if (userNum == 1) {
...
}
else if (userNum == 2) {
...
}
else if (userNum == 3) {
...
}
userNum = getMenuItem(); // <- At the end of the loop, to get the next
}
cout << "exited";
return 0;
}
相关文章:
- 我正在尝试创建一个菜单,但我的代码一直在循环
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- 如何为我的游戏设置主菜单场景?QT C++
- 如何将我的程序添加到文件和文件夹的macOS右键单击菜单?
- 为什么我的 Do-while 循环无法在C++中运行菜单
- 我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 我的开关菜单在一段时间循环中出现问题
- 我的C++程序不接受菜单后的输入
- 为什么我的打开菜单在C++不起作用
- 如何返回我的菜单C++
- 我如何制作用户可以选择的菜单
- 当我输入字符串时,我的菜单重复了太多次
- 我的菜单的双重缓冲
- 为什么我的菜单项不可点击?
- 如何将菜单栏添加到我的 WTL 对话窗口
- 如何修复我的C++案例切换菜单
- 查看我的C++滚动菜单并给出任何提示
- 为什么我的菜单不起作用 c++ 大小写开关
- 如何做一个功能,将返回我的主菜单从程序的任何地方