Fgets跳过输入

5

我已经尝试过寻找错误所在,但似乎找不到。我知道这一定与我使用fgets的方式有关,但我无法想象出是什么原因。我读到了混用fgets和scanf可能会产生错误,所以我甚至将第二个scanf更改为fgets,但它仍然跳过了我的其余输入,只打印了第一个。

int addstudents = 1;
char name[20];
char morestudents[4];

for (students = 0; students<addstudents; students++)
{
    printf("Please input student name\n");
    fgets(name, 20, stdin);
    printf("%s\n", name);
    printf("Do you have more students to input?\n");
    scanf("%s", morestudents);
    if (strcmp(morestudents, "yes")==0)
    {
    addstudents++;
    }
}

我的输入是Joe、yes、Bill、yes、John、no。如果我使用scanf代替第一个fgets,一切都按计划进行,但我想要能够使用包含空格的全名。我错在哪里了?

1个回答

7
当程序显示Do you have more students to input?并且您在控制台上输入yes然后按回车键,那么\n将存储在输入流中。
您需要从输入流中删除\n。要做到这一点,只需调用getchar()函数即可。
最好不要混用scanffgetsscanf存在许多问题,最好使用fgets为什么每个人都说不要使用scanf?我应该使用什么代替它? 尝试这个例子:
#include <stdio.h>
#include <string.h>
int main (void)
{
    int addstudents = 1;
    char name[20];
    char morestudents[4];
    int students, c;
    char *p;
    for (students = 0; students<addstudents; students++)
    {
        printf("Please input student name\n");
        fgets(name, 20, stdin);
        //Remove `\n` from the name.
        if ((p=strchr(name, '\n')) != NULL)
            *p = '\0';
        printf("%s\n", name);
        printf("Do you have more students to input?\n");
        scanf(" %s", morestudents);
        if (strcmp(morestudents, "yes")==0)
        {
            addstudents++;
        }
        //Remove the \n from input stream
        while ( (c = getchar()) != '\n' && c != EOF );
    }
    return 0;
}//end main

2
我更喜欢看到:int c; while ((c = getchar()) != EOF && c != '\n') ;,其中循环体的分号将位于自己的一行上。这可以保护您免受用户输入 yes please 或仅在输入末尾放置空格的影响。在这种情况下,使用 int c 而不是 char c 是至关重要的。在原始代码中,您不使用 c(因此我的默认编译器选项会抱怨设置但未使用的变量;如果我在此处使用代码,我将得到 (void)getchar();),因此无法可靠地区分 EOF 和有效字符并不重要。 - Jonathan Leffler
@JonathanLeffler:很高兴你对我的帖子提出了改进建议。谢谢 :) 我按照你的建议进行了更改。如果用户输入“yes”,更新后的更改也将起作用。 - ani627

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接