Mr chinpansee
Mr chinpansee

Reputation: 375

How to make number clock in C work

If you run this, a numeric clock will open. It shows hours, minutes and seconds and highlights the current time.

What I want now is for it to run. When a second of time is added the highlighted number should move one to the right. Obviously.

I just don't know how to do this, any help please?

Here is the code I have right now:

#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <unistd.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>

#define WHITE 15

int main()
{

    int uren, minuten, seconden;
        time_t ltime;
        struct tm *Tm;

        ltime   = time(NULL);
        Tm      = localtime(&ltime);

        uren        = Tm->tm_hour;
        minuten     = Tm->tm_min;
        seconden    = Tm->tm_sec;

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    printf("Uren:\n");
    int i;
    for (i = 0; i < 24; i++) {
        if(uren == i){
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
            printf("%i ", i);
        }else {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            printf("%i ", i);
        }

    }

    printf("\n\nMinuten:\n");
    int j;
    for(j = 0; j < 60; j++) {
        if(minuten == j){
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
            printf("%i ", j);
        }else{
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            printf("%i ", j);
        }
    }

    printf("\n\nSeconden:\n");
    int k;
    for(k = 0; k < 60; k++) {
        if(seconden == k){
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
            printf("%i ", k);
        }else{
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            printf("%i ", k);
        }
    }

    return 0;
}

Upvotes: 0

Views: 366

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129374

So, there are three steps, I think you are wanting to do:

  1. Repeat the output when the time changes.
  2. Go back to top of screen.
  3. Not use every ounce of CPU power in the process.

Step 1 involves adding a while-loop around the whole code.

Step 2 involves using the SetConsoleCursorPosition functon.

Step 3 involves calling Sleep(250) or something like that [you don't want to sleep a whole second, but a long enough time that the code doesn't use up all the CPU just to check every millisecond if the time has changed). You should probably also check if the current time is the same as last time, and not print it then.

I personally would simplify all the output as well, by moving the printf("%i", i); (etc) out of the if/else - after all, it's the same both sides. In fact, I'd have a variable set to the colour, and then call both SetConsoleTextAttribute and printf after the if, just set the colour = WHITE or colour = RED in the if/else statements.

Upvotes: 2

Related Questions