I want to make a program with a time to respond

I want to make a program where the console gives me numbers and i have to multiply they in the time i give, for example 5 seconds, but i don't know any method to do that, my problem is the method to make the timer, how could i do that?
this is tricky in console.
you can time it with clock() if the time is in seconds its good enough (or <chrono> tools if you want to go there) but the gist of it is that you need a thread to keep track of the time and a way to interrupt your cin (or other reading) statement once time is up.

if in windows you can use something like the kbhit() routine, but that is nonstandard. If you have access to that, you can probably find a way to do a
while(!kbhit() && timenotup)
{
timenotup = now-start; //pseudocode, you need a now-time and a 'start' time saved
}
if(kbhit())
get answer from input stream etc
else
time up message

the clock part itself is
start = clock();
..
double timetaken = (clock()-start)/(double)CLOCKS_PER_SEC;

the neverending issue of c++ is that cin stops everything until the user types a line and presses enter key, and all the fixes for that are nonstandard extensions.
Last edited on
Topic archived. No new replies allowed.