Time limit

This is a discussion on Time limit within the C++ Programming forums, part of the General Programming Boards category; Hi! This is what i want to do: - Create a countdown - When the countdown reaches 0, something should ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    Time limit

    Hi!

    This is what i want to do:
    - Create a countdown
    - When the countdown reaches 0, something should happen

    So, the actual problem is how to create a countdown.

    I really appreciate help!

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Include <ctime>.

    Set the start time like:
    Code:
    unsigned int stime=clock();
    Then check if the time limit is passed (in a loop of course):
    Code:
    if(clock() > stime+time_limit)

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    thats a polling method with all the polling disadvantages.
    according to your os you can also use a system timer to call back a function after a time duration.

    real time clock (rtc) on linux or
    windows multimedia timer on ... you guess it ... windows does the job

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Check the FAQ
    Don't quote me on that... ...seriously

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    No, you've all misunderstand me. I mean like the countdown starts at 20 seconds then goes to 0. It has nothing to do with the actual clock.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    void count_down(unsigned secs)
    {
        for (unsigned i = secs; i; --i) {
            std::cout << i << '\n';
            Sleep(1000);
        }
    }
    Like that?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by Livijn View Post
    No, you've all misunderstand me. I mean like the countdown starts at 20 seconds then goes to 0. It has nothing to do with the actual clock.
    You obviously haven't explored what the clock function does. It DOES NOT return the current time. It returns the number of *milliseconds since your program has started. It's heavily relied upon by programs that require timing.

    * this is actually designated by CLOCKS_PER_SEC which is commonly 1000
    Don't quote me on that... ...seriously

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by Livijn View Post
    No, you've all misunderstand me.
    dont think so.



    Quote Originally Posted by Livijn View Post
    I mean like the countdown starts at 20 seconds then goes to 0. It has nothing to do with the actual clock.
    thats exactly what the wmm-timer can provide. but high resolution/accuracy and without polling your cpu to death

    http://msdn2.microsoft.com/en-us/library/ms712704.aspx

    http://msdn2.microsoft.com/en-us/library/ms712713.aspx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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