0

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) {}
}

Edited by zarbhebz_1012: code too long

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by WaltP
0

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 by chrjs: n/a

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.