Click to See Complete Forum and Search --> : user input within 10 seconds


ray
06-15-2005, 01:29 PM
I am writing a small games program, console not Windows application, that requires the user to enter an integer within 10 seconds. If none is entered the system takes a different course of action. How can I code this, that is, say:
int x;
cin >> x; //within 10 seconds

I would be very grateful for all help.

vegeta
06-15-2005, 03:38 PM
THIS IS A clock countdown example
MAY BE ITWILL HELP YOU
#include <stdio.h>
#include <time.h>
#include <iostream.h>
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ;
while (clock() < endwait) {}
}

int main ()
{
int n;
printf ("Starting...\n");
for (n=10; n>1; n--)
{
printf ("%d\n",n);
wait (1);
}

}

Danny
06-15-2005, 04:05 PM
You have no other choice but to use a timer, a feature which standard C++ doesn't support by default. Depending on your platform, use the right timer/signal API to cause the process to wait for 10 seconds.

jonnin
06-15-2005, 04:34 PM
This sort of does what you need using microsoft VC threads. The thread waits on input while the main loop checks the variable. Its just a starting point but it does demonstrate what you wanted -- the main loop either times out or it gets data and shows it. You should kill the thread if time runs out, I did not do that, and it makes ugly display in a console app, which any multithreaded console app will do if care is not taken.
--

#include"afxwin.h" //dont forget to make your app use the mfc library
#include <iostream>
using namespace std;

UINT timethread(LPVOID pointer)
{

cin >> (char*)pointer;
return 0;
}

int main()
{
char str[100] = {0};
AfxBeginThread(timethread, str, THREAD_PRIORITY_LOWEST, 0, 0, NULL);

int timed = clock();
while( (clock() - timed < 5000) && (str[0] == 0 ))
{
cout << "waiting\n" << endl;
_sleep(100);
}

cout << str << endl;

return 0;
}