INF: kbhit() Does Not Take Character Out of Buffer

ID Number: Q44895

4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, if you are repeatedly testing the value of "!kbhit()", you must

get the character out of the keyboard buffer yourself after entering a

keystroke. If you do not, kbhit() returns TRUE and the test repeatedly

fails.

The following code demonstrates the situation:

#include <conio.h>

void main (void)

{

while (!kbhit ()); /* Waits for keystroke */

while ( kbhit ())

getch (); /* Empties buffer */

while (!kbhit ()); /* Waits for keystroke */

while (!kbhit ()); /* Does not wait for keystroke */

}

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00