#include<stdio.h>
#include<conio.h>
#include<dos.h>

void main()
{
 int minutes,seconds,timeremaining,input=1;
 char cAns,cAnswer;
 clrscr();
 gotoxy(35,15);
 printf("Press S to start the game: ");
 cAns=getche();

 if(cAns=='S'||cAns=='s')
    {
     clrscr();
     printf("\nAre you a human? Y or N: ");
     scanf(" %c",&cAnswer);
     timeremaining=input;
     do
      {
       input=timeremaining;
       minutes=input/60;
       input=input-(minutes*60);
       seconds=input;
       gotoxy(1,1);
       printf("%02d:%02d",minutes,seconds);
       timeremaining++;
       delay(1000);
      }while(timeremaining<=90);
     if(timeremaining>=90)
        {
         clrscr();
         gotoxy(33,12);
         printf("TIME'S UP!!");
         getch();
        }//if timeremaining==0
    }//if cAns=='S'
 else
    printf("\n\n Please enter a valid choice.");
 getch();
}

This is the sample code i'm working on. I have a question and i want to make the timer run the same time the question is displayed. But in this code, the question will show first and when answered, the timer will just start.. hope someone can help. Thanks in advance!

What you're asking here is for two different sets of code to be executed at once. You want the timing countdown code to run AT THE SAME TIME as the code waiting for the user to answer the question (or at the very least, interleaved with it so that they appear to run at the same time).

The common to do this sort of thing is with threads. Essentially, you create a new thread containing some code that you want to be run at the same time as the main thread. Plain C does not know anything about threads (and rightly so, in my opinion, as that kind of thing is dependent on the hardware and operating system); they are handled by additional headers and libraries targetted at your particular OS and hardware. Sometimes the C ompiler is bundled with threading libraries. Sometimes, your OS already contains useful threading functions and libraries ready for you to use.

Head over to google, enter something along the lines of operating-system-name threading, and see what you find.

It is also worth noting that some OS come with a timer function that essentially takes care of the threading etc for you, and simply does something you specify when the timer runs down in the background. However, this is a great opportunity for you to get into threads yourself.

Edited 3 Years Ago by Moschops

Ohh i see.. Thank you very much for this information! I think I can learn more things about Turbo C from this.

This question has already been answered. Start a new discussion instead.