-
Timeouts on Input
Hi,
I was just wondering is it possible to put a timeout on input in a C++ program (i.e. the user has a set time to enter a value before the program moves on). If this is possible where would I start looking to get such a thing going?
Cheers guys!
Dunners
-
Well this FAQ may point you in the right direction. Basically in your while loop you would determine if the required amount of time has passed, and if it has or the user pressed enter, you could exit the while loop.
-
Cheers, I think that might be what I'm looking for, I'll let you know how it goes. Thanks again
-
That doesn't seem to work. The program just hangs on the getch() until the user inputs a value. I need it to break out as soon as the user enters a value or after a set period of time even if the user hasn't entered a thing. Is this possible?
Cheers
-
Oh, gotcha. Well then I guess you would need to check for when the user pressed a key, you could try a board search for kbhit(). I believe this is also defined in the non standard file conio.h
I am not sure of a standard way of doing this(that is simply), maybe someone else might?
-
Alright, I do not believe there is any easy way to do this, but threads are easy, right?
Here is the code that does what you want, I will be back later to explain it if necessary(have to go get my daughter dinner):
Code:
#include <windows.h>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void getInput(LPVOID param);
int main() {
DWORD myThreadID;
HANDLE myThread;
int userInput = 0;
cout << "We will start our thread and then sleep for 5 seconds (user timeout)" << endl;
myThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)getInput, &userInput ,0, &myThreadID);
WaitForMultipleObjects(1, &myThread, TRUE, 5000);
CloseHandle(myThread);
if(userInput != 0){
cout << "User entered " << userInput << endl;
}else
cout << endl << "Timeout occured" << endl;
return 0;
}
void getInput(LPVOID param){
cout << "Enter a number: ";
cin >> *((int*)param);
}
-
Hi,
I had a fiddle myself and managed to come up with this.
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
bool ch;
long counter = 0;
long ans = 0;
puts ("Press any key to quit");
printf ("%d\n\n", counter);
for(counter = 0; counter != 500000; counter++)
{
ans += 1;
ch = kbhit();
if(ch == true)
break;
}
if(ch == true)
printf("You Quit The System!\n\n");
else
printf("You Did Not Quit The System!\n\n");
printf("%d\n", counter);
printf("%d\n", ans);
system("pause");
}
That's just the rough code I came up with, it's not easy to set a time for the delay (other than trial and error) but it seems to work. The ans+=1 just wastes some CPU cycles, probably won't leave it in the final version, and I know some people find breaking from loops, etc to be a bit evil but it should be OK.
Cheers for your help and for that additional code but I think I'll be best using what I came up with as it's for a school project.
Thanks again!
Dunners
-
Dunners, are you learning C or C++ ? Better decide quick, and start using the appropriate standards. Try not to mix languages unless you have to/need to.
-
I'm supposed to be using C++ but that chunk of code was a bit of an edit of some older C code I had, I'll tidy it up before I put it in my project. Knowing my teacher if I don't there will be hell to pay! :)
-
Just for completeness, here's the nice way to do it on Windows.
Code:
#include <windows.h>
int main(void)
{
const unsigned int MY_TIMEOUT = 5000; // In milliseconds.
if (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), MY_TIMEOUT))
{
// Input waiting, get it!
}
}