Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Point me in the way to a timer?

Reply
Thread Tools

Point me in the way to a timer?

 
 
Djanvk
Guest
Posts: n/a
 
      03-11-2005
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
 
Reply With Quote
 
 
 
 
Raghu Uppalli
Guest
Posts: n/a
 
      03-11-2005
Can you be more specific.

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

 
Reply With Quote
 
 
 
 
DHOLLINGSWORTH2
Guest
Posts: n/a
 
      03-11-2005

"Djanvk" <> wrote in message
news:...
> 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



 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      03-11-2005
"Djanvk" writes:

> 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";
}
}


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      03-11-2005
Djanvk wrote:
>
> 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.

--
Karl Heinz Buchegger

 
Reply With Quote
 
Djanvk
Guest
Posts: n/a
 
      03-11-2005
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.

On 10 Mar 2005 21:11:28 -0800, "Raghu Uppalli" <>
wrote:

>Can you be more specific.
>
>Do you need a way to be interrupted after x seconds?
>OR
>Sleep for x seconds?


 
Reply With Quote
 
Djanvk
Guest
Posts: n/a
 
      03-11-2005
Thanks I'll give this a run.

On Fri, 11 Mar 2005 04:47:35 -0800, "osmium" <>
wrote:

>"Djanvk" writes:
>
>> 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";
> }
> }
>


 
Reply With Quote
 
Djanvk
Guest
Posts: n/a
 
      03-11-2005
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.


On Fri, 11 Mar 2005 14:34:18 +0100, Karl Heinz Buchegger
<> wrote:

>Djanvk wrote:
>>
>> 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.


 
Reply With Quote
 
DHOLLINGSWORTH2
Guest
Posts: n/a
 
      03-12-2005

"Djanvk" <> wrote in message
news:...
> 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
IP Route Tables - Point to Point Connection - Only Routing 1 way SallyBridges Cisco 3 12-06-2007 06:06 PM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
point to point protocol Gopi VHDL 1 07-13-2004 02:37 PM
help, please!! cisco 827H point per point configuration javi Cisco 1 10-16-2003 07:33 AM



Advertisments
 



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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57