Thread: About the clock

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    4
    Rep Power
    0

    About the clock


    im trying to make the digital clock 'move' in C++

    so, how do you make the same output refresh every second?

    will this slow down the computer a lot?
    is there another way to make the clock 'move'?

    thanks in advance.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    You haven't provided enough information for us to offer you any advice. By 'move' I presume you want to update the screen. You didn't specify whether you are using a graphical clock or simply a text window. Graphical output is highly OS and library depandant, we need to know what you are using. As for performance, if you use threading and sleep in between updates, you will probably have no significant problems. If you use a tight timing loop you could cause the machine to act as if it is hung.

    It depends!

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    4
    Rep Power
    0
    #include <time.h>
    #include <stdio.h>
    #include <conio.c>
    #include <conio.h>

    int main(void)
    {
    time_t t;
    struct tm *now;
    time(&t);
    now = localtime(&t);
    printf("The date is %d:%d:%d\n", now->tm_mday + 1, now->tm_mon + 1, now->tm_year + 1900);
    printf("The time is %d:%d:%d according to your computer's clock\n", now->tm_hour, now->tm_min, now->tm_sec);
    system("pause");
    return 0;
    }


    its a digital clock, but the readings wont move unless i exit the program, recompile and run it again.
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    how about throwing a while() loop around the code that calculates the time? i dont mean to be rude, and apologize if i sound that way, but a google for 'C tutorial' would help you out much more than we can. Flow control is one of the things you first learn in programming.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    4
    Rep Power
    0
    its ok...

    i was just trying to find out if there are other ways to do the same thing.
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    Try this (works in Windows):

    Code:
    #include <time.h> 
    #include <stdio.h> 
    #include <windows.h>
    
    int main(void){ 
        time_t t; 
        struct tm *now; 
        int i, charsWritten;
    
        while (true){
            time(&t); 
            now = localtime(&t); 
            charsWritten = printf("The time/date is %02d.%02d.%4d", now->tm_mday + 1, now->tm_mon + 1, now->tm_year + 1900); 
            charsWritten += printf(" %02d:%02d:%02d", now->tm_hour, now->tm_min, now->tm_sec); 
            Sleep(500);
            //erase what has been printed
            for (i=0; i<charsWritten; i++) {
                printf("\b");
            }
        }
        return 0; 
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    4
    Rep Power
    0
    is it possible to make the clock run automatically (not in a loop) so that u can use the program while the clock runs at the bottom right hand side?
  14. #8
  15. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    You can use threads, but I suggest you simply drag the window down out of the way and continue using the computer. I presume you are using some multi-tasking operating system, not something like DOS. More than likely your OS desktop already has a clock on it somewhere.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo