C ++菜单。菜单中无限循环

时间:2012-04-15 12:54:37

标签: c++

嘿,任何人都可以看到我的菜单在while循环中有什么问题,如果我在无限循环中按1,它会一直打印出dixie。我在菜单周围有一个循环循环,以便菜单始终在那里供用户返回选择。 这是我的代码:

#include <iostream>
using namespace std;

int main()
{
    int choice;
    bool menu = true;
    cout <<"========Welcome to the database menu========\n";

    cout << "Press 1 to insert a new record at a particular position\n"
            "Press 2 to delete a record from a particular position\n"
            "Press 3 to search the database  and print results\n"
            "Press 5 to find the average experience points of players at a particular level\n"
            "Press 6 to find and remove all duplicate entries\n"
            "Press 0 to quit\n\n\n\n\n\n\n\n\n";

    cout << "Choice: ";
    cin >> choice;
    //*****************************************************************************
    // Switch menu to display the menu.
    //*****************************************************************************
    while(menu)
    {
        switch (choice)
        {
            case 1:
                cout << "dixie";
                break;
            case 2:
                cout << "bexie";
                break;
            default:
                cout<< "That is not a choice!!!\n";
        }
    }    
    getchar();
    getchar();
}

4 个答案:

答案 0 :(得分:2)

没有代码可以更改menu循环中的choicewhile。所以一旦它开始,它将永远不会停止。

答案 1 :(得分:0)

它说while (menu),这意味着它会继续这样做,直到您将菜单设置为false

另外,我想你想在循环中添加cin >> choice,或者只是反复重复选择。

答案 2 :(得分:0)

我认为while循环应该包括打印菜单选项并让用户选择如下选项:

while (menu)
{
    cout <<"========Welcome to the database menu========\n";
    cout << "Press 1 to insert a new record at a particular position\n"
            "Press 2 to delete a record from a particular position\n"
            "Press 3 to search the database  and print results\n"
            "Press 5 to find the average experience points of players at a particular level\n"
            "Press 6 to find and remove all duplicate entries\n"
            "Press 0 to quit\n\n\n\n\n\n\n\n\n";

    cout<< "Choice: ";
    cin>> choice;

    switch (choice)
    {
        case 1:
            cout << "dixie";
            break;
        case 2:
            cout << "bexie";
            break;
        default:
            cout<< "That is not a choice!!!\n";
    }
}

另一种可能性是在cout << "Choice: "行之前启动while循环。

答案 3 :(得分:0)

循环中

menu变量始终为true。它与目前的while(true)相同。