Hi im a c++ beginner and i'm doing a tic tac toe program hollywood squares style only with math questions.

what im trying to do here is im trying to insert a timer such that the player only has 20 seconds to answer each question or he/she loses his/her turn..

but from my code,, im getting the timer needs to finish first before getting the user can answer their question..

how do i do a simulataneous occurence of the start of the timer and the cin?? THanks.

cout << "PLAYER 1: What is the product of " << num1 << " and " << num2 <<"?: "; 
          int n;
          for (n=50; n>0; n--)
                printf ("%d\n",n);
                wait (1);
          cin >> userans;

and as the the wait function above..

void wait ( int seconds )
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}

Edited 5 Years Ago by zarbhebz_1012: code too long

you should get the time before you call cin, then check after cin to see if the user took too much time to enter their answer. probably something like:

clock_t starttime = clock();

cin >> userans;

if ( (clock() - starttime) * CLOCKS_PER_SEC > 20)
	//took to long to answer

Edited 5 Years Ago by chrjs: n/a

You can't with Standard C++. Keyboard input is designed to wait until the ENTER key is pressed. You need to get into special O/S system calls for Asynchronous input -- input that simply checks if there is input and continues.

This article has been dead for over six months. Start a new discussion instead.