本文介绍了无法弄清楚如何使用的getchar();用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
int main(void)
{
char F,C;
printf("Do you have a Fever? y/n\n");
F = getchar();
printf("Do you have a runny nose or cough? y/n\n");
C = getchar();
printf("Here are the results you input:\n");
printf("Do you have a fever?");
putchar(F);
printf("\nDo you have a runny nose or cough?");
putchar(C);
return 0;
}
这首的getchar的code的输入结果(); ,然后退出,而无需等待更多的输入。这是为什么?
The code inputs results from first getchar(); and then exits without waiting for more input. Why is that?
推荐答案使用,而循环每个的getchar后()如果您想只处理一个字符
Use a while loop after each getchar() if you want to process only one character
printf("Do you have a Fever? y/n\n"); F = getchar(); while((F = getchar()) != EOF && F != '\n') // This will eat up all other characters ; printf("Do you have a runny nose or cough? y/n\n"); C = getchar(); while((C = getchar()) != EOF && C != '\n') ;更多推荐
无法弄清楚如何使用的getchar();用C
发布评论