Countdown Timer

Pages: 12
May 16, 2011 at 3:08pm
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!!
May 17, 2011 at 6:37am
Someone? please help.
May 17, 2011 at 6:46am
closed account (zb0S216C)
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.
May 17, 2011 at 6:57am
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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.
Last edited on May 17, 2011 at 6:58am
May 17, 2011 at 7:04am
closed account (zb0S216C)
It will stop the program, won't it?

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:
1
2
3
4
5
6
7
8
9
10
void Wait( int seconds )
{
    clock_t endwait( clock( ) + seconds * CLK_TCK );

    while( clock( ) < endwait ) 
    {
        if( endwait == 30 )
            svore -= // Subtract you value...
    }
}
Last edited on May 17, 2011 at 7:04am
May 17, 2011 at 7:18am
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;
     }
}
May 17, 2011 at 8:13am
Is this GUI app or a console app? And what platform are you using?
May 17, 2011 at 8:16am
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
May 17, 2011 at 8:36am
Well, my previous code with one addition, sec >= 0, worked.
Last edited on May 17, 2011 at 8:41am
May 17, 2011 at 3:26pm
closed account (zb0S216C)
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.
May 17, 2011 at 3:29pm
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.
May 17, 2011 at 6:33pm
closed account (zb0S216C)
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.

I hope the above example aids you :)


References:
_beginthread( ): http://msdn.microsoft.com/en-us/library/kdzttdcb(v=vs.80).aspx
Last edited on May 17, 2011 at 6:40pm
May 17, 2011 at 7:07pm
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.
May 17, 2011 at 7:59pm
closed account (zb0S216C)
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.

How would you time the response of the user input on a single thread?

May 17, 2011 at 8:01pm
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.
May 17, 2011 at 8:21pm
closed account (zb0S216C)
Save the "timestamp" immediatly after the user inputs their answer

I don't see why not.
May 18, 2011 at 7:08am
Thanks for the link on Threads.

But which method is better?
I guess I understand what threads are. But I didn't get what Computergeek01 said about using timestamp.
May 18, 2011 at 7:20am

Please tell me if this is correct.
I am using thread like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Declerations

//Thread begin:
uintptr_t _beginthread
	( 
	void( __cdecl *start_address )( void * ),
	unsigned stack_size,
	void *arglist 
	);

//Timer definition
//..........
		
//Functions, structs...................

//Main
int main()
{
	//Various other declerations........
	
	// Second thread begin
	uintptr_t _beginthread
		( 
		void( __cdecl *start_address )( void * ),
		unsigned stack_size,
		void *arglist 
		);
	//Timer Start
	Begin = clock() * CLK_TCK;
	for(int a=1; a<=10000; a++);

	// Many questions, calls to functions etc... (Every thing else)

	_endthread;
	_endthread;
	return 0;
}
Last edited on May 18, 2011 at 10:22am
May 18, 2011 at 10:38am
closed account (zb0S216C)
You don't need to declare a prototype of _beginthread( ) before using it - that's already been done in the process.h header.

Also, you call _endthread( ) twice without actually creating threads.

Here's an example on how to create threads:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <process.h>
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

void Sample_function( void *Args );

int main( )
{
    _beginthread( &Sample_function, 0, NULL );

        // Do something while the thread executes...
  
    _endthread( );

    cin.get( );
    return 0;
}

void Sample_function( void *Args )
{
    // Do something here...
}
Last edited on May 18, 2011 at 11:49am
May 18, 2011 at 10:41am
O.K.
A couple of things.
First, what does using std::cout; and all mean?
Second, what are the three parameters you have used in _beginthread ().
Last edited on May 18, 2011 at 10:46am
Pages: 12