【问题标题】:Dealing with "non-numerical characters" when expecting an integer input在期望整数输入时处理“非数字字符”
【发布时间】:2013-12-24 22:23:30
【问题描述】:

我需要以下函数的帮助,它需要一个整数输入;当我插入“F”(非数字字符)之类的内容时,程序卡住了,它不显示任何输出或让我插入更多输入。 如何解决这个问题?

int input_legality(int game_board[FIELD_ROWS][FIELD_COLS])
{
    int input=0;
    while(1)
    {
        if(scanf("%d", &input)==1)
        {
            if(input==DOWN || input==LEFT || input==RIGHT || input==UP)
            {
                return input;
            }
            else
                if(input==EXIT)
                {
                    printf("\n program exited by user \n");
                    return 1;
                }
                else
                    if(input==PRINT)
                    {
                        printField(game_board);
                        continue;
                    }
                    else
                    {
                        fprintf(stderr,"your step is undefined, please try another one\n");
                        continue;
                    }
        }
    }  
    return 0;
}

【问题讨论】:

  • cplusplus.com/reference/cstdio/scanf 你应该处理错误情况(你的 "if(scanf("%d", &input)==1)" 没有 "else")。
  • 我已经尝试过了,它不起作用,给出完全相同的结果。 (我试过 else 继续;)
  • “一模一样”是什么意思?比如说,如果你放在那里,那就不一样了。
  • 请格式化您的代码并提供一个最小示例(主函数、预期输入、预期输出)。目前您的缩进与大括号不匹配,所以我认为您的大括号可能是错误的。

标签: c char integer scanf


【解决方案1】:

如果 scanf() 没有读取整数,似乎“F”留在标准输入中。 如果未检测到整数,一个答案是扫描字符串...

尝试添加类似

的内容
char bla[256];
scanf("%s",bla);

在 while 循环结束时(或在 scanf("%d) 失败的情况下)

这是一个基本的“主要”代码:

#include <stdio.h>

#define DOWN 0
#define UP 1
#define LEFT 2
#define RIGHT 3

#define EXIT 4
#define PRINT 5
int input_legality()
{
    int input=0;
    while(1)
    {
        if(scanf("%d", &input)==1)
        {


            if(input==DOWN || input==LEFT || input==RIGHT || input==UP)
            {
                return input;
            }

            else{
                if(input==EXIT)
                {
                    printf("\n program exited by user \n");
                    return 1;
                }
                else{
                    if(input==PRINT)
                    {
                        printf("ble %d \n",input);
                        continue;
                    }
                    else
                    {
                        fprintf(stderr,"your step is undefined, please try another one\n");
                        continue;
                    }
                }
            }
        }
        //while(getchar() != EOF); 

        //fflush(stdin);
        char bla[256];
        scanf("%s", bla);

    }  
    return 0;
}

int main ( int argc , char * argv [] )
{
    int i;
    for(i=0;i<42;i++){
        input_legality();
    }
    return 0;
}

这个 scanf 是简单的方法:其他的可能更好。 使用 switch-case 可以澄清代码。 再见,

弗朗西斯

【讨论】:

  • 哇弗朗西斯,这对我来说非常适合,太棒了!我添加了这个:char bla[256]; scanf("%s", bla);
【解决方案2】:

总是,总是,总是每次获取(交互式)输入一行。用户是这么想的,所以程序逻辑应该差不多。

POSIX getline 函数非常有用。或者,您可以使用fgets 并自己处理超长的行(也许实现getline,因为它很容易)。

获取一行输入后,使用任何方法解析它(确保在尾随垃圾上出错):sscanfstrtok_rstrtou?ll?、...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多