gpt4 book ai didi

c - scanf() 之后跳过输入

转载 作者:行者123 更新时间:2023-11-30 14:50:29 28 4
gpt4 key购买 nike

在下面的代码中,scanf() 之后对于 newMIn它跳过 gets()对于 newLname 。我尝试将其重新定位到其他部分,但它仍然会跳过,我也尝试使用 getchar()而不是scanf()但它仍然跳过下一个立即函数。所有其他变量都是字符串,并且此函数是较大程序的一部分。

void getINF(char *newStID, char *newLname, char *newMIn,char *newFname, char *newHomeAdd, char *newCourse, char *newMumNam,char *newPopsNam  ){

printf("Input Student ID:\n");
gets(newStID);
printf("Input First Name:\n");
gets(newFname);
printf("Input Middle Initial:\n");
scanf(" %c", &newMIn);
printf("Input Last Name:\n");
gets(newLname);
printf("Input Home Address:\n");
gets(newHomeAdd);
printf("Input Course:\n");
gets(newCourse);
printf("Input Name of Mother:\n");
gets(newMumNam);
printf("Input Name of Father:\n");
gets(newPopsNam);

printf("\n\n%s\n",newStID);
printf("%s\n",newFname);
printf("%c\n",newMIn);
printf("%s\n",newLname);
printf("%s\n",newHomeAdd);
printf("%s\n",newMumNam);
printf("%s\n",newPopsNam);
printf("%s\n",newCourse);

return;
}

最佳答案

当将 %c 标记与 scanf 一起使用时,它不会消耗换行符的任何额外空白。因此,按 Enter 键后的换行符将保留在输入缓冲区中,然后由以下 gets 调用使用。

关于c - scanf() 之后跳过输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48958619/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com