Making a timer...

This is a discussion on Making a timer... within the C++ Programming forums, part of the General Programming Boards category; Hi, I wqas wondering, how exactly are timers made in C++, and how are they manipulated so that they restart ...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question Making a timer...

    Hi, I wqas wondering, how exactly are timers made in C++, and how are they manipulated so that they restart after a certain amount of time...

    I hope some1 out there can help...

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the only way I really know how depends on a CPU-intensive loop and I would never recommend it to anybody, but:

    Code:
    time_t begin,length,end;
    begin=clock();
    end=begin;
    while(end-begin<length)
    {
        end=clock();
    }
    then you take in length (milseconds?) from the user...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    is that the only code thats needed, arent there any header files needed to make it work, or did u just not bother putting them in...

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I didn't bother putting them in... you need <ctime>, and like I said, you need the user to put in the amount of time they want to wait... or have it passed in somehow... but here's another link to help you out if you want to go that way...

    http://cppreference.com/stddate.html

    there are also some non-standard methods to actually put your program's thread into a sleep (look around for sleep()), but like I said, they're non-standard. the pro to those is that they don't eat up cpu cycles...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Thank u, all i need to do now is to get use to the using the time functions, and I should be able to put it in when ever I like, cheers for the help...


  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    np... but use it sparingly, because like I said it's very CPU intensive... and as a side note, it makes for a good function...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    There is more information in the Programming FAQ.

    If you're using Windows, be aware that the time can be inaccruate when timing milliseconds. The results are returned with millisecond precision, but the variables are updated only (about) every 50 milliseconds. And with multitasking, the timing can be off even more.

  8. #8
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    k, ill keep that in mind when I make my programs, by the way, do u have any ideas on how to make the clock start on the user's input, not when the program starts. I tried this code below, but it didn't work, any ideas???

    (The program is just an example)

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int num;
        
        cout << "Please press '1' to start the clock" << endl;
        cin >> num;
        if(num == 1)
        {
        while(num = 2)
        {
        time_t begin,length,end;
        begin=clock();
        end=begin;
        cout << clock() << endl;
        }
        }
        else
        {
            cout << "Please press '1'" << endl;
            int main();
        }
        system("Pause");
        return 0;
    }

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You might want to check your program logic. Specifically, that while loop looks a little out of place.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #10
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    I admit, it is a bit shabby, but it was rushed and my compiler doesn't pick a fight with it, so for now its ok... but how can I make the timer start on the user's input, and not when the program starts. Also, is there a way to get the computer to read off the clock so that when it gets to a certain value, it outputs something, because I was thinking of having it output random strings after a random number of seconds...

    I a tad stuck rite now, help would be greatly valued...


  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    cout << "enter whatever" << endl;
    cin >> whatever;
    //get current time here---call it startTimer
    //do something here
    //get current time here---call it stopTimer
    //subtract startTimer from stopTimer
    //see if difference between the two exceeds your time limit or do whatever else you want with it.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    no, you can't have the user start the clock... the clock() function by definition returns how long the program's been running - you just have to pick up the start time and monitor the end time until the difference is the amount of time you wanted to wait...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    The function clock() returns a clock_t type, not time_t. This code will only both clock_t and time_t are typedef'd to the same type.

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you're right, but in this case it really doesn't matter all that much... you could use an unsigned int and get the same result...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I think time_t may be defined as a struct, in which case the code would not work. Although time_t is usually nothing more than the number of seconds since 1978(or some date like that), after 2030, or so, the number of seconds will overflow. Hence when determining the number of seconds between two time_t's, use one of the special purpose functions designed for this, such as difftime
    Last edited by okinrus; 09-03-2004 at 12:57 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  4. Making a Timer
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2002, 01:58 AM

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