简体   繁体   English

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

[英]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.

相关问题 试图找出为什么我的代码保持无限循环 - Trying to find out why my code keeps infinite looping 如何修复我的代码中的无限循环? 我认为它跳过了第二个cin,所以它一直循环播放 - How to fix the infinity loop in my code? I think it skips the second cin so it keeps looping 我正在尝试为我的课程的扫雷游戏创建启动画面 - I'm trying to create splash screen for a Minesweeper game for my class 我正在尝试规范化向量,但我的代码返回失败? - I'm trying to Normalise a vector but my code returns a fail? 我试图在数组中得到一个随机元素,为什么我的简单代码不起作用? - I'm trying to get a random element in an array, why wont my simple code work? 我正在尝试使用 cpp 中的链表实现二叉树,但我的代码不起作用 - I'm trying to implement Binary tree using linked list in cpp, But my code is not working 我试图通过我的代码找到任何数字的质因数。 但是对于大数字,我的代码不会终止。 为什么? - I'm trying to find prime factors of any number through my code. But for large numbers, my code is not terminating. Why? 即使串行可用,代码也会不断循环 - Code keeps looping even if serial is available 我的for循环不断循环并导致程序崩溃 - My for loop keeps looping and causes the program to crash 我正在尝试为我的 DynamicArray 类创建一个迭代器。 为什么 STL 排序不适用于我的迭代器? - I'm trying to create an iterator for my DynamicArray class. Why doesn't STL sort work with my iterator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM