| Warrior2089 (91) | |||
|
Compiled with no errors (2 warnings) in Visual C++ 2008 Express Edition I am designing a program that puts a lot of numbers on the screen for a long period of time (i.e. cout<<"7 8 9 " that continues over and over until an integer reaches a certain number), that tests the person's patience to see if they'll sit through all of it or attempt to stop it early. Only one problem. A big problem. I can't figure out how to allow that user to add input to stop the program early. The program will always pause the loop, waiting for the user input. I'm going to post the full code. The code in question begins at line 163, and ending at line 170 (I'm posting the full code incase you may need some of the info from earlier on or later on in order to help fix it).
Any ideas on how to allow the user to input while not pausing the loop at line 109, it's very much appreciated. Oh, and if you're wondering why I purposefully cause the program to repeat system("pause"), it's to simulate the program shutting down for some type of error. Not needed, just for fun. Regards, Warrior2089 | |||
|
Last edited on
|
|||
| Duoas (6734) | |
|
Best solution: http://www.cplusplus.com/forum/general/3389/page1.html#msg14326 This just tells you that a key was pressed. It doesn't actually read the key. Hope this helps. | |
|
|
|
| kacko (55) | |
|
I would assume (Again I have no idea how to do it) You would need to use an events handler. But that may only limit you to one key stroke (Maybe the enter button) An article on event can be found here. http://msdn.microsoft.com/en-us/magazine/cc163659.aspx | |
|
|
|
| Warrior2089 (91) | |||
|
Duoas: I just tried your advice and added the function: bool iskeypressed() { return WaitForSingleObject( GetStdHandle( STD_INPUT_HANDLE ), 0 ) == WAIT_OBJECT_0; } into my program, and called the function in the code. However I've gotten 4 errors and I'm not sure on how to organize your code in my program. I'm getting "undeclared identifier" for the following: STD_INPUT_HANDLE WAIT_OBJECT_0 WaitForSingleObject GetStdHandle My new code, with the function in, is the following:
I realize I should probably tinker with it a bit more before asking for further assistance, but I'd like your advice on how exactly I put that into the code for it to call the iskeypressed() function for it to be called, and how exactly I actually declare the identifiers listed above. Thanks. | |||
|
|
|||
| Duoas (6734) | |||
You forgot to #include <windows.h> I'll edit my post on the other page to make the need to do that explicit. Remember, iskeypressed() is a function returning bool, and it does not actually get the key out of the keyboard buffer. How you do that depends on how you plan to interact with the user... but you can get the single key press with the following:
Have fun! | |||
|
|
|||