Empty the inbuffer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Knuut Olsen-Solberg

    Empty the inbuffer

    (Windows XP, Borland C++ Builder5 Console program.)

    Is there some function for emptying the inbuffer?
    Look at this:

    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    double Number;
    char Ch;

    cout << "Number: ";
    cin >> Number; // The '\n' is left in the buffer

    cout << "Char: ";
    cin.get(Ch); // will take the '\n' and the program does not stop.

    cout << "\nNumber: " << Number << " Char: " << Ch << " END\n";

    cout << "\n\nPress a button..."; getch();
    return 0;
    }


    Here we are preceded by a cin >> Number, and the user might have written
    "2.3", or "2.3abcd" We could use cin.ignore(80, '\n'), but what if we
    generally wanted to be sure the inbuffer is empty?

  • Ivan Vecerina

    #2
    Re: Empty the inbuffer

    Hi,
    "Knuut Olsen-Solberg" <knut@iet.hist. no> wrote in message
    news:3F9A7E0C.1 080805@iet.hist .no...[color=blue]
    > Is there some function for emptying the inbuffer?
    > Look at this:
    >
    > #include <iostream.h>
    > #include <conio.h>[/color]
    Note: <iostream> is standard C++, not <iostream.h>
    [color=blue]
    > int main()
    > {
    > double Number;
    > char Ch;
    >
    > cout << "Number: ";
    > cin >> Number; // The '\n' is left in the buffer
    >
    > cout << "Char: ";
    > cin.get(Ch); // will take the '\n' and the program does not stop.
    >
    > cout << "\nNumber: " << Number << " Char: " << Ch << " END\n";
    >
    > cout << "\n\nPress a button..."; getch();
    > return 0;
    > }
    >
    > Here we are preceded by a cin >> Number, and the user might have written
    > "2.3", or "2.3abcd" We could use cin.ignore(80, '\n'), but what if we
    > generally wanted to be sure the inbuffer is empty?[/color]

    C++ has no portable concept of an 'in buffer' as you describe it.
    What if the input of the program was redirected, and didn't come
    from the console ?

    If you want to ignore the rest of the line, the right approach is
    indeed:
    cin.ignore( numeric_limits< int>::max() , '\n' );

    The following will skip all input until the end of the file:
    cin.ignore( numeric_limits< int>::max() );
    But this will fail in console mode, as the function will
    keep waiting for more input to be ignored.

    Your specific implementation might have a way to empty a specific
    input buffer (e.g. keyboard buffer).
    Or, since you are already using the non-portable conio.h header,
    you could try something like:
    while( kbhit() ) getch(); // platform-specific...


    Cheers,
    Ivan
    --
    Ivan Vecerina - expert in medical devices, software - info, links, contact information, code snippets

    http://www.brainbench.com <> Brainbench MVP for C++


    Comment

    • Knuut Olsen-Solberg

      #3
      Re: Empty the inbuffer


      Ivan Vecerina wrote:[color=blue]
      > C++ has no portable concept of an 'in buffer' as you describe it.
      > What if the input of the program was redirected, and didn't come
      > from the console ?[/color]

      Thanks.
      I did not know that. For me it seemed natural that the program made an
      in buffer in memory when running one of the buffered functions e.g.
      cin.get(). In my mind this buffer might be used even if the input was
      redirected.
      I would have thought that may be each function got its own buffer
      throughout the program...

      Comment

      Working...