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--)
          {
              
                gotoxy(x1-1,y1-1);
                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) {}
}

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
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.