How to clear input buffer in C?

前端 未结 12 2357
一个人的身影
一个人的身影 2020-11-21 08:45

I have the following program:

int main(int argc, char *argv[])
{
  char ch1, ch2;
  printf(\"Input the first character:\"); // Line 1
  scanf(\"%c\", &ch         


        
相关标签:
12条回答
  • 2020-11-21 09:16

    you can try

    scanf("%c%*c", &ch1);
    

    where %*c accepts and ignores the newline

    one more method instead of fflush(stdin) which invokes undefined behaviour you can write

    while((getchar())!='\n');
    

    don't forget the semicolon after while loop

    0 讨论(0)
  • 2020-11-21 09:17

    I encounter a problem trying to implement the solution

    while ((c = getchar()) != '\n' && c != EOF) { }
    

    I post a little adjustment 'Code B' for anyone who maybe have the same problem.

    The problem was that the program kept me catching the '\n' character, independently from the enter character, here is the code that gave me the problem.

    Code A

    int y;
    
    printf("\nGive any alphabetic character in lowercase: ");
    while( (y = getchar()) != '\n' && y != EOF){
       continue;
    }
    printf("\n%c\n", toupper(y));
    

    and the adjustment was to 'catch' the (n-1) character just before the conditional in the while loop be evaluated, here is the code:

    Code B

    int y, x;
    
    printf("\nGive any alphabetic character in lowercase: ");
    while( (y = getchar()) != '\n' && y != EOF){
       x = y;
    }
    printf("\n%c\n", toupper(x));
    

    The possible explanation is that for the while loop to break, it has to assign the value '\n' to the variable y, so it will be the last assigned value.

    If I missed something with the explanation, code A or code B please tell me, I’m barely new in c.

    hope it helps someone

    0 讨论(0)
  • 2020-11-21 09:21

    But I cannot explain myself how it works? Because in the while statement, we use getchar() != '\n', that means read any single character except '\n'?? if so, in the input buffer still remains the '\n' character??? Am I misunderstanding something??

    The thing you may not realize is that the comparison happens after getchar() removes the character from the input buffer. So when you reach the '\n', it is consumed and then you break out of the loop.

    0 讨论(0)
  • 2020-11-21 09:23

    A portable way to clear up to the end of a line that you've already tried to read partially is:

    int c;
    
    while ( (c = getchar()) != '\n' && c != EOF ) { }
    

    This reads and discards characters until it gets \n which signals the end of the file. It also checks against EOF in case the input stream gets closed before the end of the line. The type of c must be int (or larger) in order to be able to hold the value EOF.

    There is no portable way to find out if there are any more lines after the current line (if there aren't, then getchar will block for input).

    0 讨论(0)
  • 2020-11-21 09:24

    Another solution not mentioned yet is to use: rewind(stdin);

    0 讨论(0)
  • 2020-11-21 09:25

    I am surprised nobody mentioned this:

    scanf("%*[^\n]");
    
    0 讨论(0)
提交回复
热议问题