Re: How do I do something every x increment in time?
From: CaptainJ (jeffseacrest_at_spamjam.bresnan.net)
Date: 03/30/05
- Next message: James Dennett: "Re: Member Functions"
- Previous message: Vince Morgan: "Re: How do I do something every x increment in time?"
- In reply to: wwwolf: "How do I do something every x increment in time?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 29 Mar 2005 21:56:51 -0700
"wwwolf" <abuse@alltel.net> wrote in message
news:c6587$4249f2b3$a6664320$13830@ALLTEL.NET...
> How do I write code that executes a statement every 0.25 seconds while
> other
> conditions are still being evaluated? (I hope I got the terminology right)
>
> Psuedocode:
> 1. Set up a timer to display a message 5 seconds from now.
> 2. While 1 is activated, "cout << "." << flush;" once every 0.25 seconds
> until 1
> is ready to print its message.
>
> I understand I will need 2 time variables. 1 will be "timer" and 2 will be
> "dotDelay". I think I will need to evaluate dotDelay and any dotDelay
> statements while it is placed inside the "timer while block" since this
> block
> needs to keep control. Or, dotDelay needs to be given control to execute
> its
> statement every 0.25 seconds and then pass control back to the timer loop.
>
> I've noticed that loops will 'delay' the next statement outside of their
> block
> loop until the loop returns false. At first I tried to set up a while loop
> inside of another while loop to 'delay' the "." message. It did exactly
> what I
> told it to (not what I wanted) by delaying the "." until x time and then
> continue to blast the screen with the "." message until timer was ready to
> cout.
>
> 1. What is the terminology called for the block that executes a statement
> every
> x increment in time?
>
> 2. Is this where a function would be needed?
>
> Could someone show me an example of how to set this up?
> Thanx again for your patience.
>
> // Example for 5 second timer w/ delay dot
>
> #include <iostream>
> #include <ctime>
>
>
> int main()
> {
> using std::cin;
> using std::cout;
> using std::flush;
>
> time_t timer = ((time (0)) +5); // Add 5 sec to current time
>
> while( timer >= time (0) ) // Compare the timer w/ current time
> {
> cout << "." << flush; // Would like this to only execute every 0.25
> seconds
> }
> cout << "\nFive seconds have passed.\n" << flush;
> return 0;
> }
>
> If I have gotten any terminology wrong then plaease correct me. One of the
> best
> tools for learning is being able to ask the right questions.
>
> Thanx again,
This works for a one second delay, but I'm not familiar with the time()
function so this is just a structural idea:
#include <iostream>
#include <ctime>
int main()
{
using std::cin;
using std::cout;
using std::flush;
time_t timer = ((time (0)) + 5); // Add 5 sec to current time
time_t increment = ((time(0)) + 1);
time_t base = time(0);
while( timer >= time (0) ) // Compare the timer w/ current time
{
if(base >= increment)
{
cout << "." << flush; // Would like this to only execute every
0.25seconds
increment = ((time(0)) + 1);
}
base = time(0);
}
cout << "\nFive seconds have passed.\n" << flush;
return 0;
}
- Next message: James Dennett: "Re: Member Functions"
- Previous message: Vince Morgan: "Re: How do I do something every x increment in time?"
- In reply to: wwwolf: "How do I do something every x increment in time?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|