我正在尝试创建一个菜单,但我的代码一直在循环

I'm trying to create a menu but my code keeps looping

本文关键字:我的 菜单 代码 循环 一直 一个 创建      更新时间:2023-10-16

我正在尝试创建一个菜单,用户在其中选择一个数字来执行函数,但我的代码一直在循环。即使在 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;
}