【问题标题】:Repeat a menu and prompt if the user entered invalid choice in C如果用户在 C 中输入了无效的选择,则重复菜单并提示
【发布时间】:2014-11-11 05:14:18
【问题描述】:
void menu(){ 
printf("\n"); 
printf("1. Convert integers in decimal number system to binary numbers \n"); 
printf("2. Compute a consecutive square root expression \n"); 
printf("3. Solve a quadratic equation \n"); 
printf("4. Print something fun \n"); 
printf("q. Quit\n \n"); 
printf(" Enter your choice: ");
}
main () {   
char choice;
do { 
    menu();
    scanf("%c", &choice);
    switch (choice){ 
         case '1':
             ...
         case '2':
             ....
         case '3':
             ...
         case '4':
             ....
        default: 
            printf("Wrong choice. Please enter again: "); 
            break; 
    }

}
while (choice != 'q');
}

这是我的总体思路,但我无法让它提示错误的选择并重复菜单。当我输入错误的选择时,输出如下: 比如我输入5:

  Enter your choice: 5
  Wrong choice, please enter again:
  1. Convert integers in decimal number system to binary numbers
  2. Compute a consecutive square root expression
  3. Solve a quadratic equation
  4. Print something fun
  q. Quit
  Enter your choice: (this is where I get to input)

【问题讨论】:

  • 能否在您的问题中包含输出结果?
  • 我添加了输出,请看一下

标签: c menu switch-statement choice


【解决方案1】:

看看下面的变化: 将您的 scanf() 更改为

scanf(" %c",&choice);

%c 之前的空格将确保忽略包括换行符在内的所有特殊字符。如果没有这个,每次缓冲区中有一个换行符,scanf 从它读取,你会发现你的外观没有按预期工作。

在此之后,请确保一旦遇到默认情况,您需要中断 while() 循环。

    do {  
          menu();
          scanf(" %c", &choice);
          switch (choice){ 
             case '1':
                break;
             case '2':
                break;
             case '3':
                break;
             case '4':
                break;
             default:
               {   
                  printf("Wrong choice. Please enter again: "); 
                  break; 
               }   
          }   
   }   
   while (choice != 'q');

【讨论】:

    【解决方案2】:

    首先,在每种情况下都输入break;,这样只有您选择的情况才会适用。要解决打印 2 次的问题,只需将 scanf("%c", &choice); 中的 %c 更改为 %s >> scanf("%s", &choice);

    这是代码:

     #include <stdio.h> 
    void menu(){ 
    printf("\n"); 
    printf("1. Convert integers in decimal number system to binary numbers \n"); 
    printf("2. Compute a consecutive square root expression \n"); 
    printf("3. Solve a quadratic equation \n"); 
    printf("4. Print something fun \n"); 
    printf("q. Quit\n \n"); 
    printf(" Enter your choice: ");
    }
    main () {   
        char choice;
        do { 
            menu();
            scanf("%s", &choice);
            switch (choice){ 
                case '1':
                    printf("1 \n");
                    break;
                case '2':
                    printf("2 \n");
                    break;
                case '3':
                    printf("3 \n");
                    break;
                case '4':
                    printf("4 \n");
                    break;
                 case 'q': 
                    break;
                default: 
                    printf("Wrong choice. Please enter again: "); 
                    break; 
            }
    
        }
        while (choice != 'q');
    }
    

    【讨论】:

    • 我已经休息了;在每种情况下并将 %c 更改为 %s 但它仍然给出相同的输出
    • 我用 %s 对其进行了测试,效果很好。我会在答案中包含我的代码。
    • @hzjw 这里不需要使用字符串。
    猜你喜欢
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多