constant updating

This is a discussion on constant updating within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program to display the time of day. Although basic functionality is already done, I'd like the clock ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    44

    constant updating

    I'm writing a program to display the time of day. Although basic functionality is already done, I'd like the clock to keep updating until a user keystroke. That's why I put in the indefinite while loop.

    Here's the source (though it may only work with windows):

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    int main()
    {
          while(true)
          {
              time_t now;
              time(&now);
              cout<<now<<" = ";
              unsigned int clock = now - 18000; //18000 could be any midnight
              unsigned int hour = (clock / 3600) % 24;
              unsigned int minute = (clock % 3600) / 60;
              unsigned int second = clock % 60;
              cout<<hour % 12<<":";
              if(minute < 10)
              {
                  cout<<"0";
              }
              cout<<minute<<":";
              if (second < 10)
              {
                  cout<<"0";
              }
              cout<<second;
              if(hour >= 12)
              {
                  cout<<" P.M."<<endl;
              }
              else
              {
                  cout<<" A.M."<<endl;
              }
              system("cls");
          }
          system("pause");
          return 0;
    }
    Is there a way to make the loop terminate on any keystroke?
    Is there a better way to make it update so it won't appear flashing?
    And...
    Is there a better way to clear the screen? (other than system("cls"), which I believe is OS specific.)

    Edit: Oh yeah, if you wish to understand my clock, 18000 can be the time_t value of any 12:00 AM, as shown by the comment I just added.
    Last edited by Wick; 08-18-2003 at 04:40 PM.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    To do the update, all you have to do is add a cout<<"\r" instead of system("cls"). That will make the cursor go back to the beggining of the line, and overwrite anything. I would recomend printing a few spaces after the AM or PM because when you change from 59 minutes to 0 you'll end up with a double M at the end. That is, unless you print double zeroe's.
    Don't worry too much about the system("cls"). There are some ways to clear the screen that are compiler specific and I think the time functions are windows only anyway.
    Enjoy
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    Ok, that answers the clearscreen question. Thanks.

    Yes, the if statements take care of printing double zeros:
    Code:
    if(minute < 10)
              {
                  cout<<"0";
              }
              cout<<minute<<":";
              if (second < 10)
              {
                  cout<<"0";
              }
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    Please forgive the bump. I really could use help on this.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Originally posted by adrianwx
    I have an example of blocking a continuous loop waiting for keyboard or mouse events in a console in part 5 of my tutorial here . If you're working in real 16 bit DOS, then you'll need to find appropriate API routines.
    I'm not sure if it will help in your case, but his tutorials are definitely worth a look...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 09:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21