Thread: What do you think of this digital clock?

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    What do you think of this digital clock?

    Any things done wrong in it? Any way to do it better?
    I did it in about 5 minutes or so out of bordem.
    It's not necessarily a typical programming question.

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <windows.h>
    
    int clrscr()
    {
        COORD coordScreen = { 0, 0 };
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConSize;
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
        return 0;
    }
    
    int wait_a_moment ( int seconds ) 
    {
        time_t hold_time;
        clock_t endtime = clock() + seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
        hold_time=time(NULL);
        clrscr();
        printf(ctime(&hold_time));
        return 0;
    }
    
    int full_screen()
    {
        keybd_event(VK_MENU,0x38,0,0);
        keybd_event(VK_RETURN,0x1c,0,0);
        keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
        keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);
        return 0;
    }
    
    int kill_cursor()
    {
        CONSOLE_CURSOR_INFO cci;
        cci.dwSize = 1;
        cci.bVisible = FALSE;
        SetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &cci );
        return 0;
    }
    
    int main()
    {
        time_t hold_time;
        clrscr();
        full_screen();
        kill_cursor();
        hold_time=time(NULL);
        printf(ctime(&hold_time));
        while(wait_a_moment(1) == 0);
        return 0;
    }
    Last edited by Shadow; 05-08-2002 at 07:13 PM.
    The world is waiting. I must leave you now.

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    wow really nice, I should use this as a proof that boredom is a good thing, it leads to creativity and solutions!
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Hey Shadow, that looks pretty neat to me!

    Only one small thing, my compiler (Borland 5.5) doesn't like function prototypes with no args, you have to use func(void). So I had to add the void's to your functions before I could compile it.

    Good stuff though, I like the fullscreen bit, that's the kinda code I can have fun with!

    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Ok, so i'll void all my functions that don't have arguments.
    I "believe" that all of the functions in that program are all parts of other projects I'm working on for myself.
    I was just browsing through my files and threw a couple of functinos together and BOOM!...digital clock.
    The world is waiting. I must leave you now.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Not bad, though you could gain a little bit more freedom in how you represent the clock by using a tm struct instance instead of just a time_t variable:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    
    void gotoxy ( int x, int y )
    {
      COORD coord;
      CONSOLE_CURSOR_INFO c = { 1, 0 };
      coord.X = (short)x;
      coord.Y = (short)y;
      SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
      SetConsoleCursorInfo ( GetStdHandle ( STD_OUTPUT_HANDLE ), &c );
    }
    
    int main ( void )
    {
      struct tm *t;
      time_t now;
      for ( ; ; ) {
        now = time ( NULL );
        t = localtime ( &now );
        gotoxy ( 0, 0 );
        printf ( ( t->tm_sec % 2 ) ? "%d:%d" : "%d %d", 
                 t->tm_hour - 12, t->tm_min );
        Sleep ( 1000 );
      }
      return 0;
    }
    >my compiler (Borland 5.5) doesn't like function prototypes with no args
    For good reason, for compatibility with K&R C, ANSI C allows function parameters to be declared empty. Note that an empty parameter list does not mean no parameters like it does in C++, that's what void does. In C an empty list means that there may be arguments, but the compiler doesn't know how many or of what type.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. Clock Troubles
    By _Nate_ in forum C Programming
    Replies: 22
    Last Post: 06-19-2008, 05:15 AM
  4. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  5. Wav file to digital value conversion
    By RpiMatty in forum C++ Programming
    Replies: 1
    Last Post: 12-23-2001, 06:42 AM