C++ 从switch语句调用函数缺少什么?

C++ 从switch语句调用函数缺少什么?,c++,C++,该开关可以调用DisplayMenuChoice函数,但任何选择都会结束程序 所有函数都可以独立工作,我认为这与我的参数和DisplayMenuChoice返回有关 void GoblinSickDays(); void DisplayCoolMessage(); int DisplayMenuGetChoice (int) { int menu_choice; cout << "Please select the following menu items: \

该开关可以调用DisplayMenuChoice函数,但任何选择都会结束程序

所有函数都可以独立工作,我认为这与我的参数和DisplayMenuChoice返回有关

void GoblinSickDays();

void DisplayCoolMessage();

int DisplayMenuGetChoice (int)

{

   int menu_choice;

   cout << "Please select the following menu items: \n"  << endl; 

   cout << "1. Enter Sick days." << endl;

   cout << "2. Display Something cool." << endl;

   cout << "3. End program \n" << endl;

   cin >> menu_choice;

}



int main()

{

   int menu_choice;

   DisplayMenuGetChoice(menu_choice);

   switch (menu_choice)

   {

      case 1: GoblinSickDays();
             break;

      case 2: DisplayCoolMessage();
             break;

   }
}
void GoblinSickDays();
void DisplayCoolMessage();
int DisplayMenuGetChoice(int)
{
int菜单选项;

cout继续上面的注释,当您将变量作为参数传递时,它是按值传递的。这意味着函数将收到该变量的副本,并且对函数中变量所做的任何更改都不会返回到
main()中的原始版本
,而是在您的函数中创建本地副本

C++中有三种基本的处理方法:

  • 像在C中一样传递一个指向变量的指针,并在函数中更新该地址的值,例如

    int DisplayMenuGetChoice (int *menu_choice) { ...
    
    int DisplayMenuGetChoice (int& menu_choice) { ...
    
  • 并调用
    main()
    中的函数,如下所示:

        DisplayMenuGetChoice(&menu_choice);
    
  • 将引用传递到
    菜单选项
    ,并直接在函数中更新值,例如

    int DisplayMenuGetChoice (int *menu_choice) { ...
    
    int DisplayMenuGetChoice (int& menu_choice) { ...
    
  • 并称之为:

        DisplayMenuGetChoice(menu_choice);
    
  • 或者最后根本不必担心传递
    菜单选项
    。您已将
    显示菜单选项
    声明为type
    int
    ,因此只需从函数中选择
    返回菜单
    ,并在
    main()
    中指定返回,例如

    int DisplayMenuGetChoice (void)
    {
        int menu_choice;
        ...
        return menu_choice;
    }
    
  • main()

    将其放在几个示例中(忽略C解决方案),您可以:

    #include <iostream>
    
    void GoblinSickDays()
    {
        std::cout << "Goblin is sick.\n";
    }
    
    void DisplayCoolMessage()
    {
        std::cout << "Cool Message.\n";
    }
    
    int DisplayMenuGetChoice (void)
    {
        int menu_choice;
    
        std::cout << "\nPlease select the following menu items: \n\n" 
                        " 1. Enter Sick days.\n"
                        " 2. Display Something cool.\n"
                        " 3. End program\n\n"
                        "choice: ";
    
        if (!(std::cin >> menu_choice)) {
            std::cerr << "error: invalid integer input.\n";
            exit (EXIT_FAILURE);
        }
    
        return menu_choice;
    }
    
    int main() {
    
        int done = 0;
    
        do {
            switch (DisplayMenuGetChoice()) {
                case 1: GoblinSickDays(); break;
                case 2: DisplayCoolMessage(); break;
                case 3: std::cout << "(bye)\n";
                        done = 1;
                        break;
                default: std::cerr << "invalid choice.\n"; break;
            }
        } while (!done);
    }
    
    传递参考资料

    int DisplayMenuGetChoice (int& menu_choice)
    {
        std::cout << "\nPlease select the following menu items: \n\n" 
                        " 1. Enter Sick days.\n"
                        " 2. Display Something cool.\n"
                        " 3. End program\n\n"
                        "choice: ";
    
        if (!(std::cin >> menu_choice)) {
            std::cerr << "error: invalid integer input.\n";
            exit (EXIT_FAILURE);
        }
    
        return menu_choice;
    }
    
    int main() {
    
        int menu_choice;
    
        DisplayMenuGetChoice(menu_choice);
    
        switch (menu_choice) {
            case 1: GoblinSickDays(); break;
            case 2: DisplayCoolMessage(); break;
            case 3: std::cout << "(bye)\n"; break;
            default: std::cerr << "invalid choice.\n"; break;
        }
    }
    
    注意:当上面的代码只是在错误输入时存在时,您应该处理错误(无论是
    cin.fail()、cin.bad()还是cin.eof()
    ),只要错误是可恢复的,在下次显示菜单之前,使用从
    stdin
    中删除有问题的字符)

    示例使用/输出

    $ ./bin/goblindays3
    
    Please select the following menu items:
    
     1. Enter Sick days.
     2. Display Something cool.
     3. End program
    
    choice: 1
    Goblin is sick.
    
    Please select the following menu items:
    
     1. Enter Sick days.
     2. Display Something cool.
     3. End program
    
    choice: 2
    Cool Message.
    
    Please select the following menu items:
    
     1. Enter Sick days.
     2. Display Something cool.
     3. End program
    
    choice: 3
    (bye)
    
    试试看,如果你还有其他问题,请告诉我。
    仔细检查一下,如果您还有其他问题,请告诉我。

    继续上面的注释,当您将变量作为参数传递时,它是按值传递的。这意味着函数将收到该变量的副本,并且对函数中的变量所做的任何更改都不会返回到
    main()中的原始值
    ,而是在您的函数中创建本地副本

    C++中有三种基本的处理方法:

  • 像在C中一样传递一个指向变量的指针,并在函数中更新该地址的值,例如

    int DisplayMenuGetChoice (int *menu_choice) { ...
    
    int DisplayMenuGetChoice (int& menu_choice) { ...
    
  • 并调用
    main()
    中的函数,如下所示:

        DisplayMenuGetChoice(&menu_choice);
    
  • 将引用传递到
    菜单选项
    ,并直接在函数中更新值,例如

    int DisplayMenuGetChoice (int *menu_choice) { ...
    
    int DisplayMenuGetChoice (int& menu_choice) { ...
    
  • 并称之为:

        DisplayMenuGetChoice(menu_choice);
    
  • 或者最后根本不必担心传递
    菜单选项
    。您已将
    显示菜单选项
    声明为type
    int
    ,因此只需从函数中选择
    返回菜单
    ,并在
    main()
    中指定返回,例如

    int DisplayMenuGetChoice (void)
    {
        int menu_choice;
        ...
        return menu_choice;
    }
    
  • main()

    将其放在几个示例中(忽略C解决方案),您可以:

    #include <iostream>
    
    void GoblinSickDays()
    {
        std::cout << "Goblin is sick.\n";
    }
    
    void DisplayCoolMessage()
    {
        std::cout << "Cool Message.\n";
    }
    
    int DisplayMenuGetChoice (void)
    {
        int menu_choice;
    
        std::cout << "\nPlease select the following menu items: \n\n" 
                        " 1. Enter Sick days.\n"
                        " 2. Display Something cool.\n"
                        " 3. End program\n\n"
                        "choice: ";
    
        if (!(std::cin >> menu_choice)) {
            std::cerr << "error: invalid integer input.\n";
            exit (EXIT_FAILURE);
        }
    
        return menu_choice;
    }
    
    int main() {
    
        int done = 0;
    
        do {
            switch (DisplayMenuGetChoice()) {
                case 1: GoblinSickDays(); break;
                case 2: DisplayCoolMessage(); break;
                case 3: std::cout << "(bye)\n";
                        done = 1;
                        break;
                default: std::cerr << "invalid choice.\n"; break;
            }
        } while (!done);
    }
    
    传递参考资料

    int DisplayMenuGetChoice (int& menu_choice)
    {
        std::cout << "\nPlease select the following menu items: \n\n" 
                        " 1. Enter Sick days.\n"
                        " 2. Display Something cool.\n"
                        " 3. End program\n\n"
                        "choice: ";
    
        if (!(std::cin >> menu_choice)) {
            std::cerr << "error: invalid integer input.\n";
            exit (EXIT_FAILURE);
        }
    
        return menu_choice;
    }
    
    int main() {
    
        int menu_choice;
    
        DisplayMenuGetChoice(menu_choice);
    
        switch (menu_choice) {
            case 1: GoblinSickDays(); break;
            case 2: DisplayCoolMessage(); break;
            case 3: std::cout << "(bye)\n"; break;
            default: std::cerr << "invalid choice.\n"; break;
        }
    }
    
    注意:当上面的代码只是在错误输入时存在时,您应该处理错误(无论是
    cin.fail()、cin.bad()还是cin.eof()
    ),只要错误是可恢复的,在下次显示菜单之前,使用从
    stdin
    中删除有问题的字符)

    示例使用/输出

    $ ./bin/goblindays3
    
    Please select the following menu items:
    
     1. Enter Sick days.
     2. Display Something cool.
     3. End program
    
    choice: 1
    Goblin is sick.
    
    Please select the following menu items:
    
     1. Enter Sick days.
     2. Display Something cool.
     3. End program
    
    choice: 2
    Cool Message.
    
    Please select the following menu items:
    
     1. Enter Sick days.
     2. Display Something cool.
     3. End program
    
    choice: 3
    (bye)
    
    试试看,如果你还有其他问题,请告诉我。
    仔细检查一下,如果您还有其他问题,请告诉我。

    对语言语法有更深入的了解可能是值得的。让我在您的代码中解释一些事情

    典型的功能定义如下所示:

    returnType FunctionName(paramType paramName)
    {
        returnType a = value;
        return a;
    }
    
    为适应您当前的实施,函数
    DisplayMenuGetChoice
    可能如下所示:

    int DisplayMenuGetChoice()
    {
        int menu_choice;
        {add your main logic here}
        return menu_choice;
    }
    
    要解决这个问题,需要:

    • 名为DisplayMenuGetChoice的函数,可使用DisplayMenuGetChoice()调用
    • 返回类型为int
    通过在main()函数中调用
    menu\u choice=DisplayMenuGetChoice();
    ,您将为
    menu\u选项分配从
    DisplayMenuGetChoice()返回的值

    另一种方法(这似乎是您正在尝试实现的)称为按引用传递。。参数名称的前缀是一个符号(&),以指示这一点,例如:

    void DisplayMenuGetChoice(int &menu_choice)
    {
        {add your main logic here}
    }
    
    相比之下,这是:

    • 我们返回void,这本质上意味着没有返回类型
    • 我们通过引用传递一个名为menu_choice的int。这绝对是您应该了解的内容
    • 当我们直接修改菜单选项时,不返回任何内容
    现在,在主功能中,您可以改为使用
    displaymmenugetchoice(菜单选项)


    另一个小例子是,在变量前面加上一个符号AND(就像在函数调用中那样),您将传递一个指向变量的指针。这是您可能需要阅读的另一个主题。

    对语言语法进行更深入的理解可能是值得的。让我解释一下代码中的一些内容

    典型的功能定义如下所示:

    returnType FunctionName(paramType paramName)
    {
        returnType a = value;
        return a;
    }
    
    为适应您当前的实施,函数
    DisplayMenuGetChoice
    可能如下所示:

    int DisplayMenuGetChoice()
    {
        int menu_choice;
        {add your main logic here}
        return menu_choice;
    }
    
    要解决这个问题,需要:

    • 名为DisplayMenuGetChoice的函数,可使用DisplayMenuGetChoice()调用
    • 返回类型为int
    通过在main()函数中调用
    menu\u choice=DisplayMenuGetChoice();
    ,您将为
    menu\u选项分配从
    DisplayMenuGetChoice()返回的值

    另一种方法