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

  • Thread starter Thread starter blue_tiger30
  • Start date Start date
  • Tags Tags
    Clock Refresh
AI Thread Summary
The discussion revolves around a C program designed to display a continuously updating time in the format hh:mm:ss. The user seeks to modify the output so that it appears on a single line rather than multiple lines. Suggestions include using carriage return ("\r") and backspace ("\b") characters to overwrite the previous output. However, the user encounters issues with these methods, leading to confusion and no visible output. Participants in the thread provide various coding tips, including correcting the use of octal numbers in variable assignments and exploring ANSI escape sequences for better control over console output. There are also discussions about the compatibility of the code with different compilers and operating systems, particularly Cygwin and Windows. Ultimately, the user expresses frustration and confusion over the implementation, despite receiving multiple suggestions and code examples.
blue_tiger30
Messages
28
Reaction score
0
I did the following program

Code:
#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:
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 ?
 
Technology news on Phys.org
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!
 
I tried both , I got no display at all :P
yah the simpler the better since that I'm a beginner
 
blue_tiger30 said:
I tried both , I got no display at all :P
yah the simpler the better since that I'm a beginner
Show us what you tried. I'm sure we can set you straight.

BTW, your statement
Code:
 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.
 
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);
 
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)
 
printf("\rThe time is %02d:%02d:%02d",h,min,sec);
just tried that now
didnt work :(
 
blue_tiger30 said:
printf("\rThe time is %02d:%02d:%02d",h,min,sec);
just tried that now , didnt work
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.
 
cygwin (gcc) , win 8
 
  • #10
You could try microsoft visual c++ express (it's free).
 
  • #11
in the exam I have to be using the same compiler :(
 
  • #12
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:
  • #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 doesn't work
 
  • #14
try:

Code:
    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)
 
  • #15
didn't work
Code:
please enter the the time in the following format hh:mm:ss :23:10:30
blank

the code
Code:
#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);
}
 
  • #16
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:
  • #17
blue_tiger30 said:
printf("The time is %02d:%02d:%02d\b",h,min,sec);
The backspace character backs over the last digit that was printed, like this:
The time is 3:45:1
blue_tiger30 said:
printf("\bThe time is %02d:%02d:%02d",h,min,sec);
The above will back over whatever the last character that was printed on the line.
blue_tiger30 said:
printf("The time is %02d:%02d:%02d\b\b\b\b\b\b\b\b\b",h,min,sec)
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.
blue_tiger30 said:
this doesn't work
 
  • #18
this is what i get
 
Last edited by a moderator:
  • #19
the first printf should start with a \n to get to the next lne:

Code:
    printf("\nThe time is         ");   /* print 9 spaces after is */

After this the backspace method should work

Code:
    printf("\b\b\b\b\b\b\b\b%02d:%02d:%02d",h,min,sec);

or the earlier suggestion of starting with \r:

Code:
    printf("\rThe time is 02d:%02d:%02d",h,min,sec);

Optionally, instead of for( ; ; ), use while(1):

Code:
    while(1){
/* ... */
    }

In delay, there could be a wrap around issue. To avoid this, you need to use subtract:

Code:
void delay ( int seconds )
{
    clock_t start;
    start = clock();
    while((clock() - start) < (seconds * CLOCKS_PER_SEC));
}
 
  • #20
tired the backspace and /r
it just gave my an extra empty row
it seems that this compiler doesn't like anything
 
  • #21
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:
  • #22
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:[/color]
http://en.wikipedia.org/wiki/ANSI_escape_code

Code:
#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.
}
 
  • #23
jim mcnamara said:
ANSI escape sequences control consoles like cygwin ...
Is there a reason for using %c with a value of 27, versus using \x1b?

Code:
    sprintf(clear, "\x1b[1;1;H\x1b[J");  // hex 1b is the esc character
 
  • #24
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:
  • #25
jim mcnamara said:
You were doing a really good job with your efforts, but sometimes you have to punt and try another way.
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.
 
  • #26
rcgldr said:
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,
...
Does cygwin also support the old DOS VESA standard (the BIOS INT 10H "calls")?

I don't use cygwin (partly because I've never seen a straightforward description of what it really does, apart from "some software package needs it to run in Windows".

But maybe the next step on this voyage of discovery is whether or not Cygwin console terminals support "termcap". If yes, there should be a way to make \r and \b work, even if it's not the default option.
 
  • #27
thanks for all your help , I glade to see a discussion in my thread and a lot of ideas .
I still didn't understand what I should do :)
I got really confused now

rcgldr : here is how I use cygwin , I think it uses the dos console
 
Last edited by a moderator:
Back
Top