This question already has an answer here:
I have a program as given below:
#include<iostream>
using namespace std;
int main()
{
while(true)
{
//do some task
if(Any_key_pressed)
break;
}
return 0;
}
how can exit from the loop if any key is pressed.
C++ Compiler: GCC 4.2 and higher
OS: Linux-Mint
Thanks
You could use SDL, which is (mostly) platform independent.
The above code works for Windows, Linux, MacOS as well as a nnumber of other OSes. As for how to set up SDL, there's a tutorial.
Try this:
Standard C++ doesn't offer a way to do this. You will need a platform-specific API that tells you whether a key was pressed (or input is available on stdin, possibly) without blocking. Which means you need to tell us which platform you're using.
You can use ncurses: