Every x Seconds?

This is a discussion on Every x Seconds? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone How do I make my program do something every second, like add one to a number. I have ...

  1. #1
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29

    Every x Seconds?

    Hi everyone

    How do I make my program do something every second, like add one to a number. I have tried Sleep() and this does not work. I am using code blocks on windows with GNU GCC complier

    Thanks,
    Robin

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Post something you tried.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    sleep();
    Sleep();

    I think that is it, sorry

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    1) Sleep() in it's various forms has a parameter.
    2) where is your counter / and the output (printf, cout or whatever)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    um...
    All i need is an example like this:

    int counter = 0;
    (every second)
    system("CLS");
    counter = counter +1;
    cout << counter;

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Well unless you also output a newline, you won't see much. Most I/O is buffered.

    You could do
    cout << counter;
    cout.flush();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    So could i have an example please

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    You have all you need to make another effort yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    It really depends on how you want the program to act. Sleep pauses the entire thread. So if you want your program to do nothing for a full second, then sleep is the way to go. If you want to interrupt it every second and have it do something, then that is a different issue all together.

    Lookup the data on sleep, in windows, it takes a measurement of milliseconds. (The windows.h sleep, there is a gnu sleep that I believe takes a measurement of seconds).

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Note that sleep is not a way to keep time. If you want your periodic events to over time maintain an average time of one second, then you should query the system time to determine how long to sleep untill the next second, instead of using a hard value. The system time is not precise, but it accurate over time.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    Quote Originally Posted by Salem View Post
    You have all you need to make another effort yourself.
    Infact I dont!
    all the flush does is make a new line, i could just use "endl" and this does not help me with the everty second. What does flush have to do with every second actions

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    What part of Sleep() has gotten you confused?

    Sleep( forSomePeriodOfTime );

    You've even got a link to the manual page now, click on it and start reading.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User bobbinator's Avatar
    Join Date
    Jul 2009
    Posts
    29
    Sorry salem,

    I have found out my problem with sleep, i forgot it was a capital S and in windows.h
    But this only stops the program, at least its a start

    Sorry for any incoveniance

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,024
    It "pauses" the program, yes. Now all you need is some logic--an algorithm, so to speak, on how to make the program do what you want. That's the easy part.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running a function for a period of seconds
    By Akkernight in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2009, 12:41 PM
  2. Problem with reboot(), takes a few seconds before rebooting
    By galapogos in forum Linux Programming
    Replies: 5
    Last Post: 12-01-2008, 01:21 PM
  3. How to build a timer that counts the seconds elapsed?
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-28-2007, 01:26 PM
  4. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 09:39 AM
  5. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 09:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21