【问题标题】:Prompt error when user enters a char instead of an expected int用户输入 char 而不是预期的 int 时提示错误
【发布时间】:2017-12-01 14:33:13
【问题描述】:

我正在制作一个列出选项 1-3 的菜单。用户应该输入一个整数。

scanf("%d", &select_option)

当用户输入一个字符(例如长字符串的“a”或“asd”或“1a2”之类的混合)而不是预期的int时,如何提示错误?谢谢。

注意:当用户输入 'a'、'asd' 之类的 'char' 时,代码会由于某种原因进入无限循环。

这是我的程序(最小示例):

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

        int main(void)
            {
                printf("Favourite sports? \n");
                printf("1. Tennis\n");
                printf("2. Badminton\n");
                printf("3. Basketball\n");
                printf("4. Exit program.\n");
                printf("Enter your choice (1-4): "); 
                scanf("%d", &select_option);

                while(select_option != 4) 
                {
                    switch(select_option)
                    {
                        case 1:
                            printf("You like tennis! Nice! \n");
                            break; 

                        case 2:
                            printf("You like badminton! Nice!");
                            break;      

                        case 3: 
                            printf("You like basketball! Nice!");
                            break; 

                        default:
                            system("clear");
                            printf("Invalid option. Please re-enter your choice (1-4).\n");

                    }//end switch

                printf("Favourite sports? \n");
                printf("1. Tennis\n");
                printf("2. Badminton\n");
                printf("3. Basketball\n");
                printf("4. Exit program.\n");
                printf("Enter your choice (1-4): "); 
                scanf("%d", &select_option);

                }//end while
            }//end main

【问题讨论】:

  • 您可能对我的beginners' guide away from scanf() 感兴趣,以了解更多关于scanf() 的所有可能陷阱以及其他使用指南。
  • getline获取整行,去掉换行符,用strtol转换成整数,检查是否正常
  • 如果用户输入的不是数字,只会抛出错误。例如if (select_option &lt; '0' &amp;&amp; select_option &gt; '9') { printf("error: wrong input\n"); } 会尽可能简单。
  • @LinuxStuff 但是当用户输入 'a'、'asd' 之类的 'char' 时,由于某种原因,代码进入了无限循环。
  • 试试这个sn-p的代码。 if ((select_option &gt; 0) &amp;&amp; (select_option &lt;= 9)) { printf("you have entered %d\n", select_option); } else { printf("you have entered wrong menu option\n"); } 这是处理输入的最简单方法。

标签: c validation io switch-statement scanf


【解决方案1】:

我假设你是初学者。您可以使用 Switch Case,它通常用于创建菜单并根据用户的选择执行特定情况。 我给你看一个小例子。

#include<stdio.h>
#include<conio.h>
int main()
{
  int n;
  printf("Select the sports u want to do\n");
  printf("1.Tennis\n2.Karate\n3.Football\n");
  scanf("%d",&n);
  Switch(n)
  {
     case 1:printf("You chose Tennis\n");
          break;   //To prevent from all cases being executed we use 
                   //break which helps from coming out of a loop 
     case 2:printf("You chose Karate\n");
          break;

     case 3:printf("You chose Football\n");
          break;

     default:printf("Please enter an appropriate number !");
            //Cases which dont match with the input are handled by default !
  }
}

还可以让用户输入输入直到他想退出添加一个带有变量的while循环!

我希望这会有所帮助!

【讨论】:

  • scanf语句后,请更改Case切换。这样就不会有任何语法错误
  • 实际上这就是我为我的代码所做的,但问题是当用户输入其他整数时,是的,默认处理它。但是当用户输入 'a'、'asd' 之类的 'char' 时,由于某种原因,代码会进入无限循环。
  • 据我所知它必须处理所有情况......否则你可以粘贴你的整个代码
  • 我已经粘贴了输出的图片
  • @Shaurya 我认为这是因为我在开关外放置了一个while循环。我想让它就好像它允许用户重新输入一个有效的选项一样。
【解决方案2】:

可以这样做:

#include <stdio.h>

int main(void) {
    int v;
    int ret = scanf("%d", &v);
    if(ret == 1)
        printf("OK, %d\n", v);
    else
        printf("Something went wrong!\n");
    return 0;
}

我利用了scanf() 的返回值,并基于该值,我做了一个假设。这对于“1a2”的情况会失败,但对于“12”和“a”会成功。

但是,这是一个宽泛的问题,我个人的做法是:

  1. 使用fgets() 读取输入。
  2. 丢弃换行符。
  3. 将字符串转换为整数(例如strtol())。
  4. Validate input

【讨论】:

  • 我明白了,谢谢。我看到很多人建议使用 fgets() 函数。如果我继续这样做,我应该怎么做?
  • @WCKennedays 欢迎您。你问了一个很好的问题(我赞成),所以我也谢谢你! fgets() 是个好方法。我建议您自己尝试并接受回答您已经提出的这个问题。如果您在使用fgets() 时遇到困难,请通过提供您尝试的最小示例来发布一个 问题。当然,如果您愿意,可以将我链接到这个问题(如果您发布过)。干杯!
  • 我正在尝试,谢谢!主要的斗争是因为(发布了我的代码的最小示例)我使用了while 循环和switch。我遵循了评论者指南之一:Felix Palmen's beginners' guide away from scanf(),在第 4 部分,使用 atoi 方法。问题是验证只在开始时有效,当用户输入有效选项时,验证不再适用,因为我已经将代码向下移动了。我想知道如何插入验证以覆盖我的 whileswitch 循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多