time counter

Started by
8 comments, last by jarod83 21 years, 11 months ago
How do you use make pauses in C++: if you for instance need to make a pause on 3.33 seconds or something like that. ANd what header and functions should I use. I would like to se an example if possible. thx
Advertisement
It depends on your system architecture and language.

In windows, you can include <windows.h> and then have access to the function
timeGetTime(); which returns the time your system has been up in ms. Dump that into a long (or other such variable) and check it to see if it has increased by 3330 ms (3.33 seconds).

There is also a function in <windows.h>, Sleep(int x), which should sleep you for x seconds. (you could use 3.)

In unix/linux there is a function in <unistd.h> called sleep(int x), whereby the system will do nothing for x seconds. You could also use usleep(int x) (same header) and it will cause the system to sleep for x microseconds.

(There may or may not be a usleep function in windows.)

Cheers!
--
Andrew T. Rich


[edited by - taratr98 on May 6, 2002 11:13:18 AM]
Thanks a loot for you help Andrew T. Rich

I''m still not sure where those functions come from, what headerfile do I need to include to be able to use the functions you suggested?

can you make an example where the for loop prints "hello" every 0.33 second?

I need this so that I can make a physical simulation.

thx again!
First off, my original post was screwed up. I tried to put in the various header files, but I was trying to use regular less-than and greater-than symbols, and it was interpreting those as HTML tags. I fixed my post up there so it properly shows the include files.

Tell me what platform (Win32 or Linux/Unix) you''re using and I''ll write up a quick piece of example code.

-ATR-
generally at the bottom of function documentation in the VC++ documentation it tells you what to include.

in this case indexing to timeGetTime in the help we see at the very bottom:
Header: Declared in mmsystem.h.
Import Library: Use winmm.lib.

dont forget the lib file if you go this route =) that ones not included into a project by default.

is it best? ive used it before. its a low ramp technique give it a whirl. theres other ways. prolly better ways but none that will get you moving on a time controlled loop as fast.
I am using Windows 2000.
I am using Windows 2000.
I am using Windows 2000.
I am using Windows 2000.
quote:Original post by taratr98
There is also a function in , Sleep(int x), which should sleep you for x seconds. (you could use 3.)


Just a minor note, the value you send to Sleep() must be in milliseconds, not seconds. For example, Sleep(4500) will sleep for 4.5 seconds.



"If people are good only because they fear punishment and hope for reward, then we are a sorry lot indeed." - Albert Einstein

This topic is closed to new replies.

Advertisement