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

℡╲_俬逩灬. 提交于 2019-12-17 18:59:36

问题


#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;
}

The code inputs results from first getchar(); and then exits without waiting for more input. Why is that?


回答1:


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')
    ;



回答2:


First, getchar() returns an int, not a char. This is so it can return any valid character (as a value 0..255 for systems where CHAR_BIT is 8) and a separate value (usually -1) as EOF.

Second, when users type an answer, the information contains the character (Y or N, you hope) plus a newline. There could be leading blanks; there could be trailing garbage.

So, your F probably gets the first character; the C reads the newline, not waiting for more input.

If you want to read lines and process each in turn, use fgets() to read the line and sscanf() to parse the result. Or use a function to encapsulate similar processing, such as the get_answer() function below.

#include <stdio.h>

extern int get_answer(void);    /* Declare in a header? */

int get_answer(void)
{
    int c;
    int answer = 0;
    while ((c = getchar()) != EOF && c != '\n')
    {
        if (answer == 0 && (c == 'y' || c == 'n'))  // Upper-case?
            answer = c;
        /* ?check for garbage here and complain? */
    }
    return answer;
}

int main(void)
{
    int F,C;

    printf("Do you have a Fever? y/n\n");
    F = get_answer();

    printf("Do you have a runny nose or cough? y/n\n");
    C = get_answer();

    printf("Here are the results you input:\n");
    printf("Do you have a fever? %c\n", F);
    printf("Do you have a runny nose or cough? %c\n", C);

    return 0;
}

Note that newlines go at the end of outputs, in general. You could omit them from the prompt messages so that the input appears on the same line as the prompt in an interactive session. The calling code does not really handle EOF properly — where the uses triggers an EOF condition (by typing Control-D for example) before entering any data. The code in get_answer() is OK; the code in main() should test for a zero return.




回答3:


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 <stdio.h>
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;
}



回答4:


When you enter a character ,it is stored in F,then when you press enter,it is stored in stdin buffer and when next getchar() comes it reads it's input from the stdin buffer ,for this use fflush(stdin) before every getchar() you use.



来源:https://stackoverflow.com/questions/19059730/cannot-figure-out-how-to-use-getchar-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!