Second call to the getch function does not wait for any key

  • Thread starter Thread starter forever a loser
  • Start date Start date
F

forever a loser

Guest
#include <stdio.h>
#include <conio.h>
int main(int argc, char* argv[])
{
puts("Press any key to continue");
_getch();
puts("Press another key to continue");
_getch();
return 0;
}

When I run the above C program in Microsoft Visual C++ 2010 Express, I see the output:

Press any key to continue​

When I press any key, I expect to see the output:


Press any key to continue

Press another key to continue

And when I press another key the program terminates and the console window (cmd) disappears.

But instead (not what I expect) when I press the first key, Press another key to continue appears below the Press any key to continue for very short time and my program is shutdown immediately without waiting for the second key press.

The first call to the getch function does wait for any key and I expect that the second call to the getch function waits for any key too but the second call to the getch function doesn't wait at all and returns immediately.

Why?

I did try:

rewind(stdin), fflush(stdin) and even FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)), but these calls didn't solve the problem at all.

What am I doing wrong?

Continue reading...
 
Back
Top