Point me in the way to a timer?

D

Djanvk

Ok I'm working on a program where you have say x number of seconds to
do something. I'm at a loss on how to go about running a timer
function. Can anyone point me in the right direction or post a
snippet of code for me to look at?

The program I'm trying to write is a 2nd grade math program to help my
daughter with her addition and subtraction.


Thanks

Djanvk
 
R

Raghu Uppalli

Can you be more specific.

Do you need a way to be interrupted after x seconds?
OR
Sleep for x seconds?
 
D

DHOLLINGSWORTH2

Djanvk said:
Ok I'm working on a program where you have say x number of seconds to
do something. I'm at a loss on how to go about running a timer
function. Can anyone point me in the right direction or post a
snippet of code for me to look at?

The program I'm trying to write is a 2nd grade math program to help my
daughter with her addition and subtraction.


Thanks

Djanvk

I have some C++ code for a game timer. The interupt is trapped using a
freind function to the timer class.

At object creation, the interupt is trapped, the timer is set to 120 ticks
per second. A static var is used to prevent multiple objects from messing
up the int handler, and the timing. The int handler increments a public
field in the object.
The Freind funct, int handler, and public static count needs to be locked in
memory to prevent unloading.

I do not recomend trying to run a great deal of code in the int handler.
however, I use the timer.count>>4 to synchronize the game at 32 frames per
sec.

If you'd like I'll send you a copy of the code, however the version I would
send is outdated for my purposes, and is untested on most platforms. I used
it under Windows 95/98. in a 32 bit Protected invironment.

Dan
(e-mail address removed)
 
O

osmium

Djanvk said:
Ok I'm working on a program where you have say x number of seconds to
do something. I'm at a loss on how to go about running a timer
function. Can anyone point me in the right direction or post a
snippet of code for me to look at?

The program I'm trying to write is a 2nd grade math program to help my
daughter with her addition and subtraction.

A timer is a service provided by the operating system for client programs to
use. *Your* compiler may list a function named something such as "sleep()".
But for what you want to do a simpler way is to just burn up some time.
[sleep() would take control back from your program and give the time to
someone else to use until the timer expired.]

Try running this program, experimenting with the input parameter, try an
initial value such as 5,000. Or, you can be scientific if you wish by using
the clocks/sec output provided.

Formally, the type returned by a call on clock() is clock_t, rather than an
int. But I didn't declare it as such, it gives things an esoteric look I
would rather avoid in situations such as this..

#include <iostream>
#include <ctime>

using namespace std;

// burn up n clock cycles
void delay(int n)
{
int now = clock();
int then = now + n;
while(now < then)
// do nothing
now = clock();
}
//===============
int main()
{
cout << "This system advances clock " << CLOCKS_PER_SEC << " times per
second.\n";
int cycles;
cin >> cycles;
while(1)
{
cout << "Start\n";
delay(cycles);
cout << "Fin\n";
}
}
 
K

Karl Heinz Buchegger

Djanvk said:
Ok I'm working on a program where you have say x number of seconds to
do something. I'm at a loss on how to go about running a timer
function. Can anyone point me in the right direction or post a
snippet of code for me to look at?

The program I'm trying to write is a 2nd grade math program to help my
daughter with her addition and subtraction.

If you try to do it the way I think you want, you will get into
serious troubles.
What I think you want to implement is this:

program outputs an assignement
a timer is started and the program waits for an input
Now 2 things are possible
* the timer expires, in which case the assignment counts as not done
* an input is given
...

Well. The point I am heading at is, that in order to implement it that way
ou need some sort of interruptable input function. That is something C++
does not provide out of the box. Also: What should happen, if the user
starts typing and that timer interrupt occours? Does the answer count
(because eg. your daughter is just a slower typer and spends half of
her time in searching for the keys), or does it not count?

For simplicity I would suggest some slightly change

program outputs an assignement
program saves the current time
program waits for input
-> (user enters his answer)
program gets the current time and compares it with the
previously stored time. If more then x seconds have passed,
the answer is not counted.
program checks the answer ...

For this you only need a function that gives you the current time.
Look up the functions in
#include <ctime>
(the C header would be: #include "time.h" )
It contains everything you need to implement this strategy.
 
D

Djanvk

yes I want to be able to have it stop putting problems up for my
daughter to do after a x number of minutes/seconds, like 2 min.
 
D

Djanvk

Thanks I'll give this a run.

Djanvk said:
Ok I'm working on a program where you have say x number of seconds to
do something. I'm at a loss on how to go about running a timer
function. Can anyone point me in the right direction or post a
snippet of code for me to look at?

The program I'm trying to write is a 2nd grade math program to help my
daughter with her addition and subtraction.

A timer is a service provided by the operating system for client programs to
use. *Your* compiler may list a function named something such as "sleep()".
But for what you want to do a simpler way is to just burn up some time.
[sleep() would take control back from your program and give the time to
someone else to use until the timer expired.]

Try running this program, experimenting with the input parameter, try an
initial value such as 5,000. Or, you can be scientific if you wish by using
the clocks/sec output provided.

Formally, the type returned by a call on clock() is clock_t, rather than an
int. But I didn't declare it as such, it gives things an esoteric look I
would rather avoid in situations such as this..

#include <iostream>
#include <ctime>

using namespace std;

// burn up n clock cycles
void delay(int n)
{
int now = clock();
int then = now + n;
while(now < then)
// do nothing
now = clock();
}
//===============
int main()
{
cout << "This system advances clock " << CLOCKS_PER_SEC << " times per
second.\n";
int cycles;
cin >> cycles;
while(1)
{
cout << "Start\n";
delay(cycles);
cout << "Fin\n";
}
}
 
D

Djanvk

yes basically she has to be able to do x number of math problems in 2
min's in her class, so I want to program to throw up problems she has
to solve for 2 minutes, it puts one up and she solves it and it puts
up another. Thats pretty much it.
 
D

DHOLLINGSWORTH2

Djanvk said:
yes basically she has to be able to do x number of math problems in 2
min's in her class, so I want to program to throw up problems she has
to solve for 2 minutes, it puts one up and she solves it and it puts
up another. Thats pretty much it.

The whole timing situation requires that the program not "wait" for input
Input. This is what you said, this is what your solution is also.

Under windows, You need a signal comming in to "wake" the program so it can
check the time.

otherwise loop you inputs,
check if info is available,
if so read it, & proccess it
check time, has 2 min expired?
if so signal Semiphore Time Limit Reached.

Just explain it to the computer.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top