time delaying?

S

Sargo

Hey all,

Im fairly new with C++ and i havent been able to find an answer anywhere
else so far, so I'm asking here.

I'm looking for a way to do a time delay. What I mean by this is where the
code outputs a line to the screen (console app) and then waits say... 5
seconds and then prints out the next line.

Is this possible? Thanks in advance
 
V

Victor Bazarov

Sargo said:
Im fairly new with C++ and i havent been able to find an answer anywhere
else so far, so I'm asking here.

I'm looking for a way to do a time delay. What I mean by this is where
the
code outputs a line to the screen (console app) and then waits say... 5
seconds and then prints out the next line.

Is this possible? Thanks in advance

Yes, something like

cout << "a line" << endl;
time_t t1 = time(0), t2;
do
t2 = time(0);
while (_difference_in_seconds_(t1, t2) < 5);
cout << "the next line" << endl;

Now, you will somehow have to implement _difference_in_seconds_
function, RTFM about 'localtime' function.

If you don't mind to be OS-specific, you can probably use some kind
of OS-specific way to introduce a delay. You will have to ask in
a newsgroup dedicated to your OS (whatever that is).

Victor
 
A

ajk

Hey all,

Im fairly new with C++ and i havent been able to find an answer anywhere
else so far, so I'm asking here.

I'm looking for a way to do a time delay. What I mean by this is where the
code outputs a line to the screen (console app) and then waits say... 5
seconds and then prints out the next line.

Is this possible? Thanks in advance

yes it is possible but is not a part of the C++ language - if you want
to have more general help I would suggest a forum like
microsoft.public.vc.language.

now to your question: in windows you can use the Sleep function
to suspend execution of the current thread.

// void Sleep( DWORD milliseconds );

Sleep( 5000 ); // wait 5 seconds

hth/ajk
 
S

Sargo

Sargo said:
Hey all,

Im fairly new with C++ and i havent been able to find an answer anywhere
else so far, so I'm asking here.

I'm looking for a way to do a time delay. What I mean by this is where the
code outputs a line to the screen (console app) and then waits say... 5
seconds and then prints out the next line.

Is this possible? Thanks in advance

I should also mention that this delay should delay only the function that
requires it and not pausing the entire program itself. I forgot to mention
that. Sorry.
 
J

Jerry Coffin

[ ... ]
while (_difference_in_seconds_(t1, t2) < 5);
cout << "the next line" << endl;

Now, you will somehow have to implement _difference_in_seconds_
function, RTFM about 'localtime' function.

To me, it seems like difftime would be considerably more useful than
localtime.
 
V

Victor Bazarov

Sargo said:
I should also mention that this delay should delay only the function that
requires it and not pausing the entire program itself. I forgot to
mention
that. Sorry.

C++ virtual machine works in such way that the program is the _single_
sequence of statements executed _strictly_ in order. So, when it is time
to _execute_ your pause the whole virtual machine is going to delay any
further execution.

If you would like to discuss _multithreading_, try comp.programming.threads
or the newsgroup for your OS.

Victor
 

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,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top