the program has to accept a keypress. It should wait for some fixed amount of time. If a key is pressed within this time, the program should call a function. If a key is not pressed in this time limit the program should continue its normal execution. Can someone please help me with the code ? The problem with getch() is that it essentially requires you to press a key and it does not allow other instructions to execute until the key is pressed.
That would do normal execution if a key was not pressed regardless of the timeLimit.
I suggest:
1 2 3 4 5 6 7 8 9 10
constint timeLimit = 10; //Within ten seconds
for(int i = 0; i < timeLimit; i++)
{
Sleep(1000); // Waits one second Defined in Windows.h, or just use the ctime version
if(_kbhit()) //kbhit() and getch() are depricated. VS 2012 says to use these
{
myfunction(_getch());
}
}
//Normal execution