[英]I'm trying to create a menu but my code keeps looping
I'm trying to create a menu where the user picks a number to execute a function but my code keeps looping.我正在尝试创建一个菜单,用户可以在其中选择一个数字来执行函数,但我的代码一直在循环。 My 2d array keeps looping statements even outside of the for loop.
我的二维数组即使在 for 循环之外也保持循环语句。 I found a way to stop the loop with
我找到了一种停止循环的方法
cout << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
But then I tested it with a simple cout statement (usernum ==3 section) and that loops too.但是后来我用一个简单的 cout 语句(usernum ==3 部分)对其进行了测试,并且它也循环了。 I'm not sure what I'm doing wrong?
我不确定我做错了什么? I think it has to do with my while loop but I'm not sure.
我认为这与我的 while 循环有关,但我不确定。 I would appreciate any help!!
我将不胜感激任何帮助!!
#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;
ll二
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;
}
Your userNum == 3
case does not read in a new userNum
, so that is why it loops.您的
userNum == 3
案例未读入新的userNum
,因此这就是它循环的原因。 Once userNum
is 3 it never changes again.一旦
userNum
为 3,它就再也不会改变了。
One way to fix it is to move your "get user input" code into a separate function, and then just call that at the end of your loop.修复它的一种方法是将“获取用户输入”代码移动到一个单独的函数中,然后在循环结束时调用它。 That way you don't have to repeat code all over the place, and you don't forget to add it when you implement a new case.
这样您就不必到处重复代码,并且在实现新案例时也不会忘记添加它。
Something like this:像这样的东西:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.