Thread: Clearing input buffer after using getch()

  1. #1
    Registered User
    Join Date
    Jul 2003

    Question Clearing input buffer after using getch()

    I've spent probably 4 solid hours searching this forum, on the faq and on google looking for buffer clearing solutions.

    It amazes me that a seemingly routine task does not yet have a 'one size fits all' function in this language. Maybe that's wishfull thinking, but all I've seen in my research are 10 different ways to get the job done, more or less, with each method having a potentially unpleasant downside, and much disagreement on the best methods.

    My first question to you pros is, is there a logical reason why c++ does not have a "clear_the_damn_buffer_already()" function yet, similar to cout.flush? Is it my job as a nube to just, "learn it and live with it"?

    Anyway, my situation seems like it might be a tad different than the examples I've seen, and the language and librarys are still very confusing right now.

    I'm using getch() for a user menu selection. First, is this function the same as getchar()? I believe that I read that getch() takes unbuffered input, but it's obviously buffering it 'cause it's dumping it into my next input stream.

    Here's my code basics. For user option 1 (getch()), the view record function is called and reads the structure from disk with getline() and outputs to screen. No buffer problem here.

    Next, for user option 2 (getch()), the create record funtion is called where I take user input via getline(). The getch() character is displayed on the first input line here, so I have to backspace.

    Now, I've tried cin.ignore(1) before the first getline but that just stalls the program, and takes infinite input until I hit return. I haven't tried any other methods yet because I want to understand what's going on first.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    First, call cin.clear(). This clears any error bits.

    Then, call cin.ignore(256, '\n'). This ignores the first 256 characters in the input buffer or through the '\n' character, whichever comes first.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Ok, well, that does remove the overflow from my next input line, however it forces me to hit the enter key after the getch(). It occurs to me that I could do it the original way and just insert a backspace code, but that seems kind of like a sloppy workaround.

    Thanks for your help.

  4. #4
    Registered User
    Join Date
    May 2003
    Getch() *is* unbuffered, because you get the character before you press enter. The character might also later enter a buffer (when you hit enter) but the traditional cin methods offer no potential to read a character before it is buffered (typically when the enter key is pressed).

    Your problem is, until you hit enter, the character isn't in the buffer, so you can't remove it without forcing it into the buffer -- by hitting enter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. How to put a Char into the Input buffer of a console?
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2003, 10:05 AM