Cannot figure out how to use getchar(); in C

前端 未结 4 655
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 22:52
#include 
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 c         


        
4条回答
  •  长情又很酷
    2020-12-09 23:10

    It's because when you press Enter, after answering the first question, the enter key gets stored in the next variable C. To correct it just write another getchar to eat up the extra Enter.

    It should be :-

    #include 
    int main(void)
    
    {
    char F,C;
    
    printf("Do you have a Fever? y/n\n");
    F = getchar();
    
    getchar(); /* takes the enter key */
    
    printf("Do you have a runny nose or cough? y/n\n");
    C = getchar();
    
    getchar(); /* takes the enter key */
    
    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;
    }
    

提交回复
热议问题