0

嘿,谁能看到我在 while 循环中的菜单有什么问题,如果我在无限循环中按 1,它会不断打印出 dixie。我在菜单周围有一个 while 循环,这样菜单总是在那里供用户返回选择。这是我的代码:

#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

4 回答 4

2

没有任何代码可以更改menuchoice在您的while循环内更改。所以一旦开始,它就永远不会停止。

于 2012-04-15T12:58:14.987 回答
0

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

另外,我认为您想cin >> choice在循环中添加,否则它只会一次又一次地重复选择。

于 2012-04-15T12:58:19.597 回答
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";
    }
}

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

于 2012-04-15T12:58:33.453 回答
0

menu变量总是true在循环内。和现在一样while(true)

于 2012-04-15T13:21:43.353 回答