Thread: clock

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    65

    clock

    I want to make a clock in realtime, but every time I print out a new time it never updates, how come?

    Timer.h
    Code:
    #ifndef _TIMER_
    #define _TIMER_
    
    #include <ctime>
    
    
    class Timer {
        clock_t counter;
    public:
        Timer(): counter(0) {};
    
        bool elasped(clock_t ms)
        {
            clock_t tick = std::clock();
    
            if(tick - counter >= ms)
            {
               counter = tick;
        return true;
    		}
    		return false;
    	}
    };
    #endif
    main.cpp file
    Code:
    int main()
    {
    	Timer timer;
    	time_t now;
    	time(&now);
    
    	while(true)
    	{
    		if(timer.elasped(1000))
    			cout << ctime(&now);
    	}
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    You're never updating the variable now inside the while loop. Do something like...
    Code:
    	while(true)
    	{
                    time(&now);
    		if(timer.elasped(1000))
    			cout << ctime(&now);
    	}

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    What exactly did time(&now) do in the while(true) ??

    because it works.

    Thx scwizzo

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    ctime() just converts a time that was already retrieved to a string. time() retrieves that time. So essentially you were retrieving the time once, then printing the same thing over and over. Putting it in the while loop makes it gather a new time every pass of printing it.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Update the variable now, as scwizzo noted you did not do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Aaah I see, thanks for all the answers =)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to:
    1. Update now AFTER the time has elapsed.
    2. Spell elaPSed correctly.
    3. Use CLOCKS_PER_SEC to calculate milliseconds, as "clock()" may return something that isn't milliseconds.

    Further, if you want to wait for a second, you should probably not "eat" all the CPU time for that period of time. Sleep() in Windows will wait for a specific number of milliseconds, and usleep() in Linux/Unix will wait for a specific number of microseconds.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    aight matsp.

    but try for yourself with the spelling thingy.

    when printing out timer. the compiler suggest elasped, so.. not my fault ;D

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by And3rs View Post
    aight matsp.

    but try for yourself with the spelling thingy.

    when printing out timer. the compiler suggest elasped, so.. not my fault ;D
    That's because elasped is defined with the wrong name in the timer class itself. Fix that, and it should fix the rest.

    I know my spelling is not perfect (mostly because my fingers do not do what I think they are doing), but having incorrect spelling for names in your program just makes it really annoying, because I would write "timer.elapsed", and then wonder why I get a compiler error.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Ah yeah, I saw where my typo was now.

    Thanks for the correction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. Clock Troubles
    By _Nate_ in forum C Programming
    Replies: 22
    Last Post: 06-19-2008, 05:15 AM
  4. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  5. using clock()
    By sl4nted in forum C Programming
    Replies: 8
    Last Post: 11-09-2006, 07:16 PM