While 循环在第二次迭代时跳过第一个“fgets()”

While loop skips the first `fgets()` on second iteration

我知道这样的问题一直被问到,我已经阅读了好几本,但是,我从来没有像其他所有代码一样在我的代码中使用 scanf(),所以我找不到可比较的问题。我不知道为什么在第二次、第三次、第四次等等迭代中,while 循环跳过了我的第一个 fgets().

代码:

    int main()
{
    char word[40];
    char answer[2];
    while(true) {
        printf("Please enter word: ");
        fgets(word, sizeof(word), stdin);
        printf("Your word is: %s\n", word);
        printf("Would you like to go again?");
        fgets(answer, sizeof(answer), stdin);
        printf("%d\n", strcmp(answer, "F"));
        if (strcmp(answer, "F") == 0) {
            printf("breaking");
            break;
        } else {
            printf("continuing");
            continue;
        }
    }
}

输出:

Please enter word: Hey
Your word is: Hey

Would you like to go again?Y
Please enter word: Your word is: 

Would you like to go again?Y
Please enter word: Your word is: 

Would you like to go again?Y

...等等

我认为这与清除输入缓冲区有关,我尝试了几种方法,但没有任何效果。第二天和C混在一起,所以我不太了解。 (Python 更容易哈哈)

当您输入 Y<ENTER> 时,输入缓冲区中有 2 个字符:Y\n。由于 answer 是一个 char[2] 并且 fgets 总是写入一个 C 字符串,因此 answer 中保存的字符串将是 "Y".

换行符保留在输入缓冲区中,因此下一个 fgets 读取输入缓冲区的剩余部分。因为它是换行符,所以 fgets 只读取换行符,因此 word 将具有字符串 "\n".

您可以声明具有更大维度(至少 3)的数组 answer 或改用 word。如果你选择前者,那么你应该做

char answer[10];
...
if (strcmp(answer, "F\n") == 0)
   ...

编辑

我想引用 fgets 手册页中的一句话,它总结了 fgets 工作。

man fgets

#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);

DESCRIPTION

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('[=33=]') is stored after the last character in the buffer.