Home > database >  this code gets printed multiple times after every increment on my mac . I cannot use <conio.h>
this code gets printed multiple times after every increment on my mac . I cannot use <conio.h>

Time:03-21

Made a digital clock using C program : This is a simple digital clock i made and the issue i am having is that every time the time gets incremented the printf gets printed . Since i am on mac i cant use <conio.h> .

My expectation : I want the program to display a single printf and the increment happens in a single printf instead of new printf every time the time changes.

#include<stdio.h>
#include <unistd.h>
#include <stdlib.h >
int main()
{
    int h, m , ;
    int d=1;
    printf("Enter the time : ");
    scanf("%d%d%d", &h,&m,&s);

    if(h>12 || m>60 || s>60){
        printf("ERROR!!");
        exit(0);
    }
    while(1){
        s  ;
        if(s>59){
            m  ;
            s=0;
        }
        if(m>59){
            h  ;
            m=0;
        }
        if(h>12){
            h=1;
        }
       printf("\n Clock ");
        printf(" d:d:d",h, m ,s);
        sleep(1);
    } 
    return 0;
}   

CodePudding user response:

Update : I did change in printf and i got what i was looking for

Before update :

printf("\n Clock ");
       fflush(stdout);
       printf(" d:d:d\r", h,m,s); 
       printf("\r");
       sleep(1);

After update :

printf("\r Clock  %2u:u:u", h, m ,s);
        fflush(stdout);
        sleep(1);
    } 

CodePudding user response:

You need more thorough error checking on the time entered (what if the user enters negative values, or minutes or seconds equal to 60); and to get the time to print on a single line, you need to replace the \n with a \r, and to flush stdout.

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

int main(void)
{
    unsigned h, m, s;

    printf("Enter the time : ");
    scanf("%u:%u:%u", &h,&m,&s);

    if(h > 12  ||  h < 1  ||  m > 59  ||  m < 0  ||  s > 59  ||  s < 0) {
        printf("ERROR!!");
        exit(0);
    }
    while (1) {
        if (  s > 59) {
            s = 0;
            if (  m > 59) {
                m = 0;
                if(  h > 12)
                    h = 1;
            }
        }
        printf("\r Clock  %2u:u:u", h, m ,s);
        fflush(stdout);
        sleep(1);
    }
    return 0;
}

To get a clock that is more robust and with more precise timing, based on your system's real-time clock, try this:

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

int main(void)
{
    time_t now;
    time_t last_now = -1;
    struct tm *tod;
    char outbuf[32];

    while (1) {
        now = time(NULL);
        if (now != last_now) {
            last_now = now;
            tod = localtime(&now);
            strftime(outbuf,sizeof outbuf,"%l:%M:%S %P",tod);
            printf("\r Clock  %s", outbuf);
            fflush(stdout);
        }
    }
    return 0;
}

Look at the man pages for time, localtime, and strftime to see how this works, but basically it gets the time from the system clock, and if it has changed since the last time it was printed, format it and print it.

  • Related