Thread: How can you tell if the input buffer is empty?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    15

    How can you tell if the input buffer is empty?

    Code:
    	if (getch() != EOF)
    		getch();
    Basically if the buffer is empty, then good, but if it's not then I want to do the getch() (there would only be 1 thing in the buffer so this would work perfectly)

    Any ideas?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    If you're using VC++: int _kbhit(void); in <conio.h>. It return non-zero if a key was pressed.

    Here's an example right from the docs:
    Code:
    /* KBHIT.C: This program loops until the user
     * presses a key. If _kbhit returns nonzero, a
     * keystroke is waiting in the buffer. The program
     * can call _getch or _getche to get the keystroke.
     */
    
    #include <conio.h>
    #include <stdio.h>
    
    void main( void )
    {
       /* Display message until key is pressed. */
       while( !_kbhit() )
          _cputs( "Hit me!! " );
    
       /* Use _getch to throw key away. */
       printf( "\nKey struck was '%c'\n", _getch() );
       _getch();
    }

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. input buffer settings
    By valaris in forum Linux Programming
    Replies: 6
    Last Post: 09-10-2008, 04:04 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Checking Input Buffer...
    By AndyBomstad in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2005, 11:40 AM