C++ switch 语句结束菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19146866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:33:18  来源:igfitidea点击:

C++ switch statement ending the menu

c++switch-statement

提问by Mitch Honsa

Hey I am working on a Switch selection menu for my C++ programming class. I have managed to get my menu to work, but I don't know how to stop the program on the exit selection in the menu I have made.

嘿,我正在为我的 C++ 编程课程设计一个 Switch 选择菜单。我已经设法让我的菜单工作,但我不知道如何在我制作的菜单中的退出选择上停止程序。

My code is

我的代码是

#include <iostream>
using namespace std;

int main()
{
    char selection;

    do
    {
        cout << "  IHCC Computer Science Registration Menu\n";
        cout << "  ====================================\n";
        cout << "  1.  Welcome to Computer Programming In C++\n";
        cout << "  2.  Welcome to Java Programming\n";
        cout << "  3.  Welcome to Android Programming\n";
        cout << "  4.  Welcome to iOS Programming\n";
        cout << "\n";
        cout << "  5.  Exit\n";
        cout << "  ====================================\n";
        cout << "  Enter your selection: ";
        cin >> selection;
        cout << endl;

        switch (selection)
        {
            case '1':
                cout << "Computer Programming In C++\n";
                cout << "\n";
                break;

            case '2':
                cout << "Java Programming\n";
                cout << "\n";
                break;
            case '3':
                cout << "Android Programming\n" ;
                cout << "\n";
                break;

            case '4':
                cout << "iOS Programming\n";
                cout << "\n";
                break;

            case '5':
                cout << "Goodbye.\n";
                break;

            default: cout <<selection << "is not a valid menu item.\n";

                cout << endl;
        }

    }while (selection != 0 );

    return 0;
}

回答by LihO

Change

改变

while (selection != 0)

to:

到:

while (selection != '5')


Also note that your current approach reads a single character only. In case you don't want numbers with more digits invoke the selection based on their first digit, read intinstead:

另请注意,您当前的方法仅读取单个字符。如果您不希望具有更多数字的数字根据其第一个数字调用选择,请int改为阅读:

int selection = 0;

do {

    cout << ...        
    cin >> selection;

    switch (selection) {
        case 1:
            ...
            break;
        ...
        case 5:
            cout << "Goodbye.\n";
            break;

        default:
            ...
    }

} while (selection != 5);

回答by demize

LihO and sharth have the proper solution, however if the idea is to exit the program when they input 5 then I'd suggest doing this with the case:

LihO 和 sharth 有适当的解决方案,但是如果他们的想法是在他们输入 5 时退出程序,那么我建议在这种情况下这样做:

case '5':
  cout << "Goodbye.\n";
  return 0;

It makes the program exit there and not later on in the code.

它使程序退出那里,而不是稍后在代码中。

回答by user6448585

Simply set the selectionvariable to meet the condition that you already have to exit the loop:

只需设置selection变量以满足您必须退出循环的条件:

case '5':
    cout << "Goodbye.\n";
    selection = 0;
    break;