小编Spy*_*tis的帖子

为什么 getchar 不停止获取输入?

我有下面的代码,它应该与用户交互,了解他想要做什么。

int input;
printf("Would you like to update the name of the student?\n");
printf("Enter Y(yes) or N(no)\n");
input = getchar();

if (input == 'Y' || input == 'y') {
  printf("Please enter the updated name\n");
  scanf("%s", tmpSt->name);
}

printf("Would you like to update the student's ID?\n");
printf("Enter Y(yes) or N(no)\n");
input = getchar();
Run Code Online (Sandbox Code Playgroud)

但执行时它不会停止获取第一个输入。

Would you like to update the name of the student?
Enter Y(yes) or N(no)
Would you like to update the student's ID?
Enter Y(yes) or N(no)
Run Code Online (Sandbox Code Playgroud)

它应该得到名称的答案,但它直接转到 …

c user-input input scanf getchar

2
推荐指数
1
解决办法
179
查看次数

标签 统计

c ×1

getchar ×1

input ×1

scanf ×1

user-input ×1