_kbhit () works only in the first while-loop

October 29, 2010 at 09:34:29
Specs: Windows XP
in visual studio - console app the program is
while ( ! _kbhit()) {}
... program
_getch();
while ( ! _kbhit()) {}


program runs through the second while- loop without stop. How to reset _kbhit to be used again


See More: _kbhit () works only in the first while-loop

Report •


#1
October 29, 2010 at 09:39:43
use 'cin' to clear console buffer ??

Report •

#2
October 29, 2010 at 16:35:20
I guess it's because the user has hit the Enter key, which produces two characters: '\r' and '\n'. The call to _getch() throws away the '\r' but there is still the '\n' remaining in the input buffer, so therefore _kbhit() continues to return non-zero.

Why aren't you just calling _getch()? Your while-loop needlessly wastes CPU time.


Report •

Related Solutions


Ask Question