C - 使用 while 和 switch/case 的菜单程序返回具有无效选择的重复菜单

标签 c loops while-loop menu switch-statement

头文件:

    #include <stdio.h>
    #include <stdlib.h>

    int print_menu(){
      printf ("MENU\n");
      printf ("1. Total of All Top Scores for the Week\n");
      printf ("2. Total of All High Scores for the Week\n");
      printf ("3. Total Machine High Scores for the Week\n");
      printf ("4. Machine High Score for the Week\n");
      printf ("5. EXIT\n");
      printf ("Enter Selection:");

      int selection = getchar();
      return selection;
    }

主要的 C 文件:

    #include <stdio.h>
    #include <stdlib.h>
    #include "lab1.h"

    int main(int argc, char *argv[]){
      int selection = print_menu();
      while(1)
      {
        switch (selection)
        {
          case '1':
            printf ("\nselected 1\n");
            break;
          case '2':
            printf ("\nselected 2\n");
            break;
          case '3':
            printf ("\nselected 3\n");
            break;
          case '4':
            printf ("\nselected 4\n");
            break;
          case '5':
            printf ("\nExit\n");
            exit(0);
            break;
          default:
            printf ("Invalid Selection");
            print_menu();
            break;
        };
      };
    }

我的问题是,当我运行程序并输入错误的字符时,程序会重新打印菜单并再次要求选择。除了它打印出菜单两次。示例:

    maiah@maiah-vb:~/shared$ ./a.out
    MENU
    1. Total of All Top Scores for the Week
    2. Total of All High Scores for the Week
    3. Total Machine High Scores for the Week
    4. Machine High Score for the Week
    5. EXIT
    Enter Selection:d
    Invalid Selection
    MENU
    1. Total of All Top Scores for the Week
    2. Total of All High Scores for the Week
    3. Total Machine High Scores for the Week
    4. Machine High Score for the Week
    5. EXIT
    Enter Selection:
    Invalid Selection
    MENU
    1. Total of All Top Scores for the Week
    2. Total of All High Scores for the Week
    3. Total Machine High Scores for the Week
    4. Machine High Score for the Week
    5. EXIT
    Enter Selection:

然后您可以选择输入另一个选项。 当我经历时,我注意到它似乎正在选择“d”并正确输出,但随后表现得像一个空格或新行已自动输入并继续检查选择(我不确定这是否是但实际问题 - 这就是它的表现方式)。如果有人对如何解决此问题有任何想法并解释为什么会这样。任何帮助都会很棒!

最佳答案

您需要保留打印函数返回的内容。

      default:
        printf ("Invalid Selection");
        selection = print_menu();
        break;

第二个问题是您的 getchar() 调用也会在您选择后获取返回值。添加另一个 getchar() 以使用它。

int selection = getchar();
(void) getchar(); /* ignore enter key */
return selection;

附带说明一下,不要将代码放入标题中,只能放在声明中。
否则,代码将在每个包含标题的代码文件(多个)中编译,并给您带来多个定义错误。如果您只有一个包含 header 的代码文件,这并不明显,但您应该及早养成正确的习惯。

最后,您需要始终再次阅读,而不仅仅是在 5 的情况下。

      default:
        printf ("Invalid Selection");
        break;
    };
    selection = print_menu();
}; /* end of while */

即在循环内但在 case 语句之外执行它,因为只有在没有执行其他分支时才会采用默认分支。

关于C - 使用 while 和 switch/case 的菜单程序返回具有无效选择的重复菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57985506/

相关文章:

c# - .Net 中的定时器和循环是否准确?

php - 为什么在 for 循环的条件下调用函数不好?

c - 为什么这个程序在我输入第一个单词时结束并且不循环回来

C 编程。硬币翻转程序循环没有像我想要的那样工作

c - 指针子串数组

loops - 如何在 Ansible 中强制执行 with_dict 的顺序?

java - 循环中每百万次才执行一条语句

python - 如何管理 while 循环来反转此打印

c - 多维数组有多少级间接寻址?

c - 周期最长的随机数算法是什么?