无法弄清楚如何使用的getchar();用C [英] Cannot figure out how to use getchar(); in C

查看:146
本文介绍了无法弄清楚如何使用的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆