Search code examples
c++breakconio

Breaking out of a loop with a keypress


I am writing a Win32 GUI app which has a loop which I would like to restart only when a keypress is made. The main loop is already running, and I can't simply restart it, so I have to insert a 'hang' point which the user can manually break out of. The code below represents what I have put at the end of my main loop. It is supposed to pause the program by putting it into an endless sub-loop which can only be broken when the letter 'q' is pressed.

for (;;)
 {
 char temp;
 temp = _getch();

 if (temp == 'q')
  {
  break;
  }
 }

This successfully makes the program hang, but pressing 'q' does nothing to end the loop. I understand that using cin.ignore() or cin.get() would be preferable, but for some reason when I add iostream to the header list, I get a bunch of errors, so I am currently trying to do it using _getch() with the conio.h header.

Any help much appreciated.


Solution

  • You can just call MessageBox.