Jul 9, 2011 at 6:59pm Jul 9, 2011 at 6:59pm UTC
How do you make a program pause until a keystroke is made. I know cin.ignore(1); or system("pause"); will make the program wait until return or enter is hit. But how do you make a program wait until ANY key is hit, and not display the key hit obviously. Is there a way to do this without having to use the a system command?
I'm using g++ compiler on windows 7 btw...
Last edited on Jul 9, 2011 at 7:00pm Jul 9, 2011 at 7:00pm UTC
Jul 9, 2011 at 9:28pm Jul 9, 2011 at 9:28pm UTC
1. Don't use system().
2. There isn't a good universal way to do this in a console that I know of. You may be able to get it to work using ncurses.
Jul 9, 2011 at 9:47pm Jul 9, 2011 at 9:47pm UTC
here is some code from duoas or disch, I cant remember.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
bool iskeypressed(DWORD timeout_ms )
{
return (WaitForSingleObject(GetStdHandle( STD_INPUT_HANDLE ),timeout_ms) == WAIT_OBJECT_0);
}
int main()
{
string name;
cout << "Please enter your name." ;
while (!iskeypressed( 500 )){cout << "." ;}
getline( cin, name );
cout << "Thank you, " << name << endl;
cin.get();
return 0;
}
The only catch is that you have to open the .exe from the console. At least I do using VS2010
Last edited on Jul 9, 2011 at 9:48pm Jul 9, 2011 at 9:48pm UTC