使用scanf进行验证检查在循环内不起作用 [英] Validation check with scanf does not work inside a loop

查看:159
本文介绍了使用scanf进行验证检查在循环内不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数来检查用户的输入,并让他们再次尝试以防他们输入错误的输入。

I am trying to make a function to check the input of the user and let them try again in case they enter a wrong kind of input.

所以当我在函数中输入错误的输入,它将我抛入无限循环。我该怎么做才能解决它?

So when I enter a wrong input into the function it throws me inside an infinite loop. What can I do to fix it?

我只能使用 getchar scanf 用户输入。

I am allowed to use only getchar and scanf for user input.

int sizeOfField()
{
    int size,inputCheck,flag=0;

    while (!flag)
    {
        inputCheck= scanf(" %d ", &size );
        if ( inputCheck < 1 )
        {
            printf( "Invalid Input!\n" );
            printf( "Try agian");
        } else if (inputCheck == 1)
            flag=1;
    }
    return size;
}


推荐答案


允许仅使用getchar和scanf进行用户输入。

allowed to use only getchar and scanf for user input.

使用 fgets()会更好。但要忍受这个限制....

Using fgets() would be better. But to live with that restriction ....

scanf(%d, & size); 返回0,非数字输入保留在 stdin

When scanf(" %d ", &size ); returns 0, the non-numeric input remains in stdin.

代码需要消耗无效输入 - 这是OP代码中缺少的。

Code needs to consume the invalid input - that is missing from OP's code.

为了保持一致性,即使输入良好,也要消耗其余部分。此代码不会检测同一行上的良好输入后是否存在尾随垃圾。

For consistency, even with good input, consume the rest of the line too. This code does not detect if trailing junk exists after the good input on the same line.

%d中的第二个空格也是一个问题。 指示 scanf()读取所有空格,直到某些检测到非空白区域。这意味着 scanf(%d,& size); 123 \\\
4 \ n之后才会返回。读取 4 ,确定它不是空格并放回以供以后输入。从格式中删除尾随空格。

The 2nd space in " %d " is also a problem. A " " directs scanf() to read all white space until some non white space is detected. This means scanf(" %d ", &size ); will not return until something like "123\n4\n" is entered. The 4 is read, determined not to be a white space and put back for later input. Drop the trailing white-space from the format.

如果输入返回 EOF ,代码也有问题(结束文件或罕见的输入错误)。函数 sizeOfField()无法传达文件结尾。函数可以重新定义,或者现在,简单退出程序。

Code also has a problem should an input return EOF (end-of-file or rare input error). The function sizeOfField() has no way to convey end-of-file. Function could be re-defined or for now, simple exit the program.

int sizeOfField(void) {
  int inputCheck;

  do {
    int size;
    inputCheck = scanf("%d", &size);
    if (inputCheck == EOF) {
      break;
    }

    // consume rest of line
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);

    if (inputCheck == 1) {
      return size;
    }

    if (ch == EOF) {
      break;
    }

    // Non-numeric input occurred.
    printf("Invalid Input!\n" "Try again");
    fflush(stdout);
  }

  // No recovery possible
  exit(EXIT_FAILURE);  
}






好奇这个是%d的好处是%d。当输入为非数字时,代码确定至少消耗了所有前导空格(包括'\ n')。关于哪些租船人由于非数字而被放回 stdin 的规格是有问题的,如果需要将一个角色放回标准输入。没有这种保证, while((ch = getchar())!='\ n'&& ch!= EOF); 可能不会消耗所有违规的非 - 数字输入。


Curiously this is a case where " %d" has benefit over "%d". When input is non-numeric, code is certain at least all leading white space (including '\n') are consumed. The specifications about what charterers are put back due to being non-numeric into stdin are problematic if more that one character needs to be "put back" into stdin. Without that assurance, while ((ch = getchar()) != '\n' && ch != EOF); may not consume all the offending non-numeric input.

这篇关于使用scanf进行验证检查在循环内不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆