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
     
  22. May 2, 2013 #21

    rcgldr

    User Avatar
    Homework Helper

    I'm running out of ideas. How are you opening up a dos console window? Is this a dos console window created by gcc? If so, try compiling your program, but to run it, do whatever it takes in windows 8 to run a normal dos console window. For Windows XP, it's start, all programs, accessories, command prompt. Then change the drive letter if needed (if you have multiple partitions and/or drives) and use "cd \" to get to the root directory, then "cd ..." to get to your program directory.

    You could also try pressing alt-<enter> to go into full screen mode before running your program, but that's unlikely to help.
     
    Last edited: May 2, 2013
  23. May 2, 2013 #22

    jim mcnamara

    User Avatar

    Staff: Mentor

    Hmm. I think we are confusing things.

    ANSI escape sequences control consoles like cygwin - the output, and do cursor positioning. This is what the bash command "clear" does.. so if you embed exactly what clear send to the terminal, an escape sequence that does what clear does, you will print in the very same place -- the cursor in first column, first row. On a cleared screen. Each and every time. This is example code to do that - the rest of the time stuff DOES NOT do what you seem to need. I just took shortcuts to get something for you to learn from.

    See this first before trying the code:
    http://en.wikipedia.org/wiki/ANSI_escape_code

    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <time.h>

    // program == ctime.c  tested in cygwin on Windows 7.
    // example usage to run for 32 seconds: ./ctime 32

    int main(int argc, char **argv)
    {
       time_t lt=time(NULL);
       int limit=(argc>1)?  atoi(argv[1]): 10; // default = 10 seconds, use command line to change;
       char clear[12]={0x0};
       // this next line makes 2 escape sequences -
       // #1 place the cursor at 1;1 sequnce is:  <escape>[1;1H
       // #2 clear everything on the screen. sequence is:  <escape>[J  

       sprintf(clear, "%c[1;1;H%c[J", 27 ,27);  // 27 is the esc character
       printf("%s%s\n", clear, ctime(&lt) );      // first time display
       while(limit--)           // loop "limit" times
       {
          sleep(1);             // take a nap
          lt=time(NULL);                // get epoch seconds - seconds since Jan 1 1970
          printf("%s%s\n", clear, ctime(&lt) );   // print cuted-up time and date
       }
       return 0;          // this function is "int main", so: return an int.
    }

     
     
  24. May 2, 2013 #23

    rcgldr

    User Avatar
    Homework Helper

    Is there a reason for using %c with a value of 27, versus using \x1b?

    Code (Text):

        sprintf(clear, "\x1b[1;1;H\x1b[J");  // hex 1b is the esc character
     
     
  25. May 2, 2013 #24

    jim mcnamara

    User Avatar

    Staff: Mentor

    Answer: yes. escape ASCII 27 == 1b && the OP is a beginner. When I taught this stuff a LONG time ago, new students did better with decimal numeric values to begin with. Kinda like ducking trigraphs until the last possible moment. The utility of either one is not always immediately obvious.

    I assume students are the same now. YMMV. My reasons for the post were: the \b\b\b\b\b approach appeared to be confusing the OP, the OP was ostensibly supposed to be using the standard library time api. Including time.h seemed perfunctory. Like doing something "because".

    You were doing a really good job with your efforts, but sometimes you have to punt and try another way.
     
    Last edited: May 2, 2013
  26. May 3, 2013 #25

    rcgldr

    User Avatar
    Homework Helper

    Not really, I wasn't aware that the cygwin console window doesn't handle backspace (hex 08) or carriage return (hex 0d) like a conventional dos console window, or aware that the assignment was to get the program to work with the cygwin console window, as opposed to just using cygwin gcc to compile programs and then to run those programs using a standard dos console window.

    Does cygwin also support the old DOS VESA standard (the BIOS INT 10H "calls")? Microsoft's Virtual PC supports it, and there may be some other DOS "emulators" that support it.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook