Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to make the clock refresh instead of it repeating every second

  1. May 2, 2013 #1
    I did the following program

    Code (Text):
    #include<stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main (void) {
    int sec,min,h;
    printf("please enter the the time in the following format hh:mm:ss :");
    scanf("%d:%d:%d",&h,&min,&sec);
    for (  ;  ;  ) {
     sleep(1);// a delay function which delays by a second
     sec=sec+1;
    if ( sec == 60 ) {
    sec=00;
    min=min+1;
    }
    if ( min== 60 ) {
    min=00;
    h=h+1;
    }
    if ( h==24 ) {
    min=00;
    sec=00;
    h=00;
    }
    printf("The time is %02d:%02d:%02d\n",h,min,sec);
    }

    }
     
    it gives my the following
    Code (Text):
    The time is 23:59:57
    The time is 23:59:58
    The time is 23:59:59
    The time is 00:00:00
    The time is 00:00:01
    The time is 00:00:02
    The time is 00:00:03
    The time is 00:00:04
    The time is 00:00:05
     
    so how can I make it only appear in one line that updated automatically ?
     
  2. jcsd
  3. May 2, 2013 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Try replacing \n with \r. If that doesn't work, try writing some \b characters to backspace over the time.
    http://en.wikipedia.org/wiki/Escape_sequences_in_C

    There are "better" ways to solve the problem than those ideas, but I'm guessing you are just starting to learn programming, so try the simple things forst!
     
  4. May 2, 2013 #3
    I tried both , I got no display at all :P
    yah the simpler the better since that i'm a beginner
     
  5. May 2, 2013 #4

    Mark44

    Staff: Mentor

    Show us what you tried. I'm sure we can set you straight.

    BTW, your statement
    Code (Text):
     sec = 00;
    just sets sec to 0. There's no point in having the additional 0, and there is actually a good reason NOT to include it, as the compiler interprets this as an octal (base-8) number.
     
  6. May 2, 2013 #5

    rcgldr

    User Avatar
    Homework Helper

    If you're going to use "\r", use it at the beginning of your output:

    printf("\rThe time is %02d:%02d:%02d",h,min,sec);
     
  7. May 2, 2013 #6
    printf("The time is %02d:%02d:%02d\b",h,min,sec)
    printf("The time is %02d:%02d:%02d\b\b\b\b\b\b\b\b\b",h,min,sec)
    printf("The time is %02d:%02d:%02d\r",h,min,sec)
    printf("The time is %02d:%02d:%02d\r\n",h,min,sec)
     
  8. May 2, 2013 #7
    printf("\rThe time is %02d:%02d:%02d",h,min,sec);
    just tried that now
    didnt work :(
     
  9. May 2, 2013 #8

    rcgldr

    User Avatar
    Homework Helper

    What compiler and operating system are you using? This method works for Windows dos console windows.

    You can try the backspace method (...\b\b\b...) posted above next.
     
  10. May 2, 2013 #9
    cygwin (gcc) , win 8
     
  11. May 2, 2013 #10

    rcgldr

    User Avatar
    Homework Helper

    You could try microsoft visual c++ express (it's free).
     
  12. May 2, 2013 #11
    in the exam I have to be using the same compiler :(
     
  13. May 2, 2013 #12

    rcgldr

    User Avatar
    Homework Helper

    In that case try the backspace method. By any chance does gcc default to unicode (16 bit characters) instead of ascii (although either should work)?
     
    Last edited: May 2, 2013
  14. May 2, 2013 #13
    printf("The time is %02d:%02d:%02d\b",h,min,sec);
    printf("\bThe time is %02d:%02d:%02d",h,min,sec);
    printf("The time is %02d:%02d:%02d\b\b\b\b\b\b\b\b\b",h,min,sec)

    this doesnt work
     
  15. May 2, 2013 #14

    rcgldr

    User Avatar
    Homework Helper

    try:

    Code (Text):

        printf("The time is         ");   /* print 9 spaces after is */
     
    only do that printf one time, before your loop, then try:

    printf("\b\b\b\b\b\b\b\b%02d:%02d:%02d",h,min,sec)
     
  16. May 2, 2013 #15
    didn't work
    Code (Text):
    please enter the the time in the following format hh:mm:ss :23:10:30
    blank
     
    the code
    Code (Text):
    #include<stdio.h>
    #include <time.h>
    void delay ( int seconds );
    int main (void) {
    int sec,min,h;
    printf("please enter the the time in the following format hh:mm:ss :");
    scanf("%d:%d:%d",&h,&min,&sec);
    printf("The time is         ");   /* print 9 spaces after is */
    for (  ;  ;  ) {
     delay(1);// a delay function which delays by a second
     sec=sec+1;
    if ( sec == 60 ) {
    sec=00;
    min=min+1;
    }
    if ( min== 60 ) {
    min=00;
    h=h+1;
    }
    if ( h==24 ) {
    min=00;
    sec=00;
    h=00;
    }
    printf("\b\b\b\b\b\b\b\b%02d:%02d:%02d",h,min,sec);
    }

    }
    void delay ( int seconds )
    {
        clock_t endwait;
        endwait = clock () + seconds * CLOCKS_PER_SEC ;
        while (clock() < endwait);
    }

     
     
  17. May 2, 2013 #16

    rcgldr

    User Avatar
    Homework Helper

    So what does the display look like after a few loops? Are you sure your program is being compiled? Try deleting the program's .exe file and recompiling to make sure it's making a new program .exe file.
     
    Last edited: May 2, 2013
  18. May 2, 2013 #17

    Mark44

    Staff: Mentor

    The backspace character backs over the last digit that was printed, like this:
    The time is 3:45:1
    The above will back over whatever the last character that was printed on the line.
    The above will back over 9 characters of whatever was printed, erasing the time that was just printed. Notice that rcgldr had the backspace characters first, not last, in the format string.
     
  19. May 2, 2013 #18
    this is what i get
    [Broken]
     
    Last edited by a moderator: May 6, 2017
  20. May 2, 2013 #19

    rcgldr

    User Avatar
    Homework Helper

    the first printf should start with a \n to get to the next lne:

    Code (Text):

        printf("\nThe time is         ");   /* print 9 spaces after is */
     
    After this the backspace method should work

    Code (Text):

        printf("\b\b\b\b\b\b\b\b%02d:%02d:%02d",h,min,sec);
     
    or the earlier suggestion of starting with \r:

    Code (Text):

        printf("\rThe time is 02d:%02d:%02d",h,min,sec);
     
    Optionally, instead of for( ; ; ), use while(1):

    Code (Text):

        while(1){
    /* ... */
        }
     
    In delay, there could be a wrap around issue. To avoid this, you need to use subtract:

    Code (Text):

    void delay ( int seconds )
    {
        clock_t start;
        start = clock();
        while((clock() - start) < (seconds * CLOCKS_PER_SEC));
    }
     
     
  21. May 2, 2013 #20
    tired the backspace and /r
    it just gave my an extra empty row
    it seems that this compiler doesnt like anything
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: How to make the clock refresh instead of it repeating every second
  1. How does software make? (Replies: 18)

  2. How to make decoder (Replies: 19)

Loading...