【问题标题】:how to use a while loop to keep asking for input until the input is correct (C)? [duplicate]如何使用while循环不断询问输入,直到输入正确(C)? [复制]
【发布时间】:2018-01-11 21:05:43
【问题描述】:

我想使用 while 循环来不断询问用户正确的输入,直到给出。 scanf() 可以做到这一点吗?我知道如果输入不匹配,它会被保留为未分配并保存以供第二个 scanf() 捕获。

以下程序永远运行,当第一个输入错误时,不会要求我输入第二个输入。

#include<stdio.h> 
/* Check the input, if the input does not contain a single integer value, 
then keep asking for the integer value */
/* I used a counter variable so that the program does not run on forever */
int
main (int argc, char * argv[]){
    int counter = 10, items = 0, input = 0;
    while (counter){
        printf("input an integer value: ");
         items = scanf("%d",&input);
         if (items == 1){
             printf("successfully read an item");
             break;
         }   
         else{
             counter --;
             input = 0;
             printf("failed to read an item, please try again\n");
         }
    }
    return 0;
}

【问题讨论】:

  • 据我所知,问题似乎是 scanf 在第二次被调用后没有暂停读取第二个输入。不知道如何解决这个问题,但我认为这是 OP 遇到的真正问题
  • 尝试按 Ctrl-D 而不是直接返回
  • 如果它一直在运行,这可能意味着您的文件流有错误,可能需要清除。
  • 所以我刚刚做了一个测试用例:int main(void) { int input; printf("A"); scanf("%d", &amp;input); printf("B"); scanf("%d", &amp;input); printf("C"); } ,我认为 OP 的一个简化问题是:如何在第一次给出无效输入后让 scanf 第二次触发跨度>
  • 我的beginners' guide away from scanf() 中涵盖了这个确切的(有缺陷的)尝试,您可能想阅读它。简而言之,scanf() 是用来解析的,不是用来读取的;当它无法解析时,它也不会读取,所以它只是将东西留在输入缓冲区中。更好的循环,例如fgets() 然后用atoi()strtol()strtod()sscanf() 等解析该行。

标签: c loops input


【解决方案1】:

无限循环的原因是由于错误的 scanf 调用,如果没有读取任何内容,则返回 0,如果有错误,则返回 EOF。不管流中的非整数项永远不会被删除,因此 scanf 调用会一直返回相同的 0 值,因为它在被删除或跳过之前无法继续(根据此处的其他答案)。

解决此问题的一种方法是清除缓冲区。这是解决问题的一种方法。

char tmp;
...
items = scanf("%d",&input);    
if(items!=1){
  // perhaps printf('That was not a number\n');
  while(scanf('%c',&tmp)!=EOF);
}
else if(items==1){
   ... //hooray you read an item, it's value is in input
}
else{
      // the user quit with a Control-D or something.
}

附言我喜欢 reference by Felix 关于如何摆脱 scanf 的内容。

【讨论】:

  • 如果scanf 无法读取数字,它不会返回EOF。在这种情况下,如果将返回 0。你可以检查items ! = 1
  • 您的替代检查很好,但有关 scanf 的评论不正确。根据参考manual:成功时,将返回转换和存储的输入字段数。如果发生输入错误,则返回 EOF。
  • 它被我绊倒了很多次。
  • @信息 是的!也就是说,如果发生输入故障。意味着有人在输入上按了 Ctr+D,或者输入文件已完成。但是如果有内容并且不能解析为整数,则不会返回EOF,会返回0。
  • @AjayBrahmakshatriya 你是对的。看,我还是被那个绊倒了。感谢您的反馈。我也会更新帖子。
【解决方案2】:

好的,经过一些测试和搜索,我发现您可以使用fseek(stdin,0,SEEK_END); 来解决您的问题。

尝试:

#include<stdio.h>
/* Check the input, if the input does not contain a single integer value,
   then keep asking for the integer value */
/* I used a counter variable so that the program does not run on forever */
int
main (int argc, char * argv[]){
        int counter = 10, items = 0, input = 0;
        while (counter){
                printf("input an integer value: ");
                items = scanf("%d",&input);
                if (items == 1){
                        printf("successfully read an item");
                        break;
                }
                else{
                        counter --;
                        input = 0;
                        printf("failed to read an item, please try again\n");
                        fseek(stdin,0,SEEK_END);
                }
        }
        return 0;
}

【讨论】:

  • 请注意,stdin 不保证支持定位请求,因此fseek(stdin, ...) 可能会失败。
猜你喜欢
  • 2014-12-17
  • 1970-01-01
  • 2020-09-02
  • 2011-06-27
  • 2014-04-12
  • 2021-11-19
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
相关资源
最近更新 更多