Thread: Writing a clock...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    Writing a clock...

    Alright, so, after messing around with Dev-C++, I've managed to come up with a simple clock:
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        unsigned short int hours = 0;
        unsigned short int minutes = 0;
        unsigned short int seconds = 0;
        unsigned short int exit = 0;
        string input;
        
        cout<<"Display seconds?\nY/N: ";
        cin>>input;
        cin.ignore();
        
        cout<<"\n\nPlease set the time (hours)\nTime: ";
        cin>>hours;
        cin.ignore();
        
        cout<<"\n\nPlease set the time (minutes)\nTime: ";
        cin>>minutes;
        cin.ignore();
        
        while (exit == 0)
        {
              system("cls");
              
              if (input == "y")
              {
                          if (minutes < 10)
                          {
                                    if (seconds < 10)
                                    {
                                                cout<<"\n\nCurrent time: "<<hours<<":0"<<minutes<<":0"<<seconds;
                                    }
                                    
                                    else
                                    {
                                                cout<<"\n\nCurrent time: "<<hours<<":0"<<minutes<<":"<<seconds;
                                    }
                          }
                          
                          else
                          {
                                    if (seconds < 10)
                                    {
                                                cout<<"\n\nCurrent time: "<<hours<<":"<<minutes<<":0"<<seconds;
                                    }
                                    
                                    else
                                    {
                                                cout<<"\n\nCurrent time: "<<hours<<":"<<minutes<<":"<<seconds;
                                    }
                          }
                          if (seconds >= 60)
                          {
                                    minutes++;
                                    seconds = 0;
                          }
                          
                          if (minutes >= 60)
                          {
                                    hours++;
                                    minutes = 0;
                                    seconds = 0;
                          }
                          if (hours >= 13)
                          {
                                    hours = 1;
                                    minutes = 0;
                                    seconds = 0;
                          }
                          
                          seconds++;
                          Sleep (1000);
                          
              }
              
                        if (input == "n")
              {
                        
                          if (minutes < 10)
                          {
                                      cout<<"\n\nCurrent time: "<<hours<<":0"<<minutes;
                          }
                          
                          else
                          {
    
                                      cout<<"\n\nCurrent time: "<<hours<<":"<<minutes;
                          }
                          
                          if (seconds >= 60)
                          {
                                    minutes++;
                                    seconds = 0;
                          }
                          
                          if (minutes >= 60)
                          {
                                    hours++;
                                    minutes = 0;
                                    seconds = 0;
                          }
                          if (hours >= 13)
                          {
                                    hours = 1;
                                    minutes = 0;
                                    seconds = 0;
                          }
                          
                          seconds++;
                          Sleep (1000);
              }
        }
        return 0;
    }
    So, it works all fine and dandy, but I'd like to know what could be improved.
    (also, is it possible to preformat integers to show 01 instead of 1?)

    And yes, I'm definitely not very far along, but writing simple applications amuses me :P

    What I'm asking is what would somebody more experienced do differently to achieve the same purpose?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    What about delay in processing? It will take extra milliseconds to process the clock maybe longer if you have other stuff going on. It would add up to be a really inaccurate clock I imagine. I couldn't tell you how to deal with this but it is what it is.
    My computer is awesome.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I couldn't tell you how to deal with this but it is what it is.
    One possible way:
    GetTickCount at the beginning of the program...
    +1000 on each iteration - to calculate next wake up time
    GetTickCount before Sleep to calculate current Time
    Make Sleep with difference between the currentTime and Next WakeUp time instead of 1000
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You need to include the header file <string> to use C++ strings.

    Also, the variable exit might be better off as a bool. Not that your program ever exits.

    Also, couldn't you put that if(input=="y") only around the code that actually deals with seconds instead of duplicating lots of code?

    If I was writing a clock program like that, I'd use the time() or clock() function from <ctime> -- but perhaps that just takes the fun out of it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    Also, the variable exit might be better off as a bool. Not that your program ever exits.
    It would also be better off not being called "exit." That's the name of a standard library function. By declaring a variable called exit you can never call exit() from inside main(). Instead call it "do_exit" or somesuch.

    EDIT: Well, I suppose you could call it as ::exit() instead, but it's still confusing.

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 program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM