I wanted to make an countdown timer . What I wanted was that,say after, say, 20 seconds, do something (like in my case, change the value of an variable).I have been able to get to it by using a simple timer. What I want is that the function keeps on looping until the timer is stopped.
I intend to use it in a quiz. So it will start with calculating the time with the first question. Then, say after 1 minute, the timer will deduct 0.5 points form the score, and keep doing so for 10 seconds, until the timer is stopped.
Since I am a beginner and don't know anything more than data structures, I was hoping you could help me by guiding me in how to make it.
Thanks in Advance!!
This code segment( look at the first post on this site: http://www.gidforums.com/t-8718.html ) will create a timer for you. You can easily read up on everything defined in the segment.
It will stop the program, won't it? I wanted it to be a little different than that. I wanted it to reduce a variable svore after every, say, 30 seconds. There is already an timer in my program with the code:
seconds = time(NULL);
//Timer Define
int elapTicks;
double elapMilli, elapSeconds, elapMinutes;
clock_t Begin, End; //initialize Begin and End for the timer
//Timer Start
Begin = clock() * CLK_TCK;
for(int a=1; a<=10000; a++);
//Program........
//Timer stop:
End = clock() * CLK_TCK; //stop the timer
//
//Time Taken:
//
elapTicks = End - Begin; //the number of ticks from Begin to End
elapMilli = elapTicks/1000; //milliseconds from Begin to End
elapSeconds = elapMilli/1000; //seconds from Begin to End
elapMinutes = elapSeconds/60; //minutes from Begin to End
//Giving the time using if
But I can't get it to work like an countdown timer, to reduce score with passing time.
The program will stop once the timer runs out, unless you have something after timer such as a loop, that holds back the termination of the program. In the code segment I sent you, the while loop will help you. All you have to do is use if statements, like so:
It isn't working!
I put 50000 for int seconds.
But as soon as it cones across Wait, the program simply stops (If I put it in the first line, it will just don't do anything)!!.
Will this work for the code of the timer I posted before:
1 2 3 4 5 6 7
if (elapMinutes > 2)
{
for (int sec = elapSeconds - 120;sec -= 30;)
{
score -= 1;
}
}
This is a basic console app (tell me if i am wrong, but "Hello World" is also an console app?)
And I am using Windows 7, and the compiler is Visual C++ Express 2010
Well, if you want to wait for user input and run a timer simultaneously, you will need to use threads. A program is one single thread. This forces the timer to use the same thread as the user input. Basically, the timer will not count down until the user has entered something.
How do we use multiple threads?
And as far as I saw, it was counting down. In order to test it, I waited for some time (nearly 2 minutes), before entering the input. The result was nearly 2 minutes, rather than a couple of seconds.
Threads are created with a call to _beginthread( ), and terminated with a call to _endthread( ). You can read upon _beginthread( ) and _endthread( ) in the link at the bottom.
The result was nearly 2 minutes, rather than a couple of seconds.
That's because the thread had to execute the timer before requesting the user input.
Threads are simple to understand. Take a motorway( highway if your American ) with 2 cars. Car One represents your programs timer. Car Two represents the user input request. The motorway has one lane. This motorway is you programs' main thread, and the lanes are your programs child threads. A motorway with only one lane will force Car Two to follow Car One. Car Two will forever remain behind Car One. If the motorway was given another lane, Car Two could use the second lane which will allow Car Two to pass Car One.
Why are you using threads? With the idea you laid out you could take the time the user started, wait for them to answer the question and penilise them if theirs too much of a difference. I'm not saying don't user threads, this is indeed a perfect context for them. But you can simplify this if you wanted to.
Save the "timestamp" immediatly after the user inputs their answer, there will be some precision loss but I don't think it would make a difference in this case.