Search code examples
c++loopswhile-loopkeypress

C++ breaking a loop with keypress


I am writing a console application for windows where there is a while loop that goes through over and over, and I am trying to find a way to be able to break out of the loop with any keypress, while the loop keeps running. cin or getch stop the program to wait for input but I don't want that. Any help?


Solution

  • Your while loop must have a stop condition, like while(count < x)

    When the user presses a key, set a Boolean to false and make your while loop check for that

    while(!keyPressed){}