How to clear input buffer in C?

前端 未结 12 2356
一个人的身影
一个人的身影 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:00

    The lines:

    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF)
        ;
    

    doesn't read only the characters before the linefeed ('\n'). It reads all the characters in the stream (and discards them) up to and including the next linefeed (or EOF is encountered). For the test to be true, it has to read the linefeed first; so when the loop stops, the linefeed was the last character read, but it has been read.

    As for why it reads a linefeed instead of a carriage return, that's because the system has translated the return to a linefeed. When enter is pressed, that signals the end of the line... but the stream contains a line feed instead since that's the normal end-of-line marker for the system. That might be platform dependent.

    Also, using fflush() on an input stream doesn't work on all platforms; for example it doesn't generally work on Linux.

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

    You can do it (also) this way:

    fseek(stdin,0,SEEK_END);
    
    0 讨论(0)
  • 2020-11-21 09:07

    Short, portable and declared in stdio.h

    stdin = freopen(NULL,"r",stdin);
    

    Doesn't get hung in an infinite loop when there is nothing on stdin to flush like the following well know line:

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

    A little expensive so don't use it in a program that needs to repeatedly clear the buffer.

    Stole from a coworker :)

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

    The program will not work properly because at Line 1, when the user presses Enter, it will leave in the input buffer 2 character: Enter key (ASCII code 13) and \n (ASCII code 10). Therefore, at Line 2, it will read the \n and will not wait for the user to enter a character.

    The behavior you see at line 2 is correct, but that's not quite the correct explanation. With text-mode streams, it doesn't matter what line-endings your platform uses (whether carriage return (0x0D) + linefeed (0x0A), a bare CR, or a bare LF). The C runtime library will take care of that for you: your program will see just '\n' for newlines.

    If you typed a character and pressed enter, then that input character would be read by line 1, and then '\n' would be read by line 2. See I'm using scanf %c to read a Y/N response, but later input gets skipped. from the comp.lang.c FAQ.

    As for the proposed solutions, see (again from the comp.lang.c FAQ):

    • How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work?
    • If fflush won't work, what can I use to flush input?

    which basically state that the only portable approach is to do:

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

    Your getchar() != '\n' loop works because once you call getchar(), the returned character already has been removed from the input stream.

    Also, I feel obligated to discourage you from using scanf entirely: Why does everyone say not to use scanf? What should I use instead?

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

    Try this:

    stdin->_IO_read_ptr = stdin->_IO_read_end;

    0 讨论(0)
  • 2020-11-21 09:15
    unsigned char a=0;
    if(kbhit()){
        a=getch();
        while(kbhit())
            getch();
    }
    cout<<hex<<(static_cast<unsigned int:->(a) & 0xFF)<<endl;
    

    -or-

    use maybe use _getch_nolock() ..???

    0 讨论(0)
提交回复
热议问题