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

C - menu program using while and switch/case returns duplicate menu with invalid selection

头文件:

    #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' 并正确输出但随后表现得像一个空白 space 或自动输入新行并继续检查选择(我'我不确定这是否是实际问题 - 这就是它的行为方式)。如果有人对如何解决此问题有任何想法并解释为什么会这样。任何帮助都会很棒!

您需要保留您的打印功能 returns。

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

第二个问题是您的 getchar() 调用也会在您选择后占用 return。添加另一个 getchar() 来使用它。

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

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

最后你需要经常再读一遍,不仅仅是5的情况。

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

即在循环内但在 case 语句之外执行它,因为只有在执行了另一个的 none 时才会采用默认分支。

尝试

  while(1)
  {

      int selection = print_menu();
      switch (selection)
      ....
  }

getchar() 具有从输入缓冲区中删除下一个字符的副作用。

因为 scanf() 被告知只读取一个字符 (%c) 这具有忽略该输入行上的所有其他内容的效果。

在重新打印菜单之前还要清除控制台。

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

   int print_menu()
   {
    char selection;

    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:");

    scanf("%c", &selection);

    return selection;
   }

   int main(int argc, char *argv[])
   {
    int selection;

    while(1) {
      selection = print_menu();

      switch (selection)
      {
        case '1':
          printf("\nselected 1\n");
          exit(selection);
          break;
        case '2':
          printf("\nselected 2\n");
          exit(selection);
          break;
        case '3':
          printf("\nselected 3\n");
          exit(selection);
          break;
        case '4':
          printf("\nselected 4\n");
          exit(selection);
          break;
        case '5':
          printf ("\nExit\n");
          exit(0);
          break;
        default:
          system("@cls||clear");
          printf("\nInvalid Selection\n");
          break;
      };
    };
   }