无法弄清楚如何使用的getchar();用C

编程入门 行业动态 更新时间:2024-10-25 19:22:58
本文介绍了无法弄清楚如何使用的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

本文发布于:2023-11-27 02:56:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   弄清楚   getchar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!