Thread: Real Time Clock

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Real Time Clock

    Hi

    I have a program that will give you the static time from the system

    here is the code

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
      time_t current;
    
      current = time(0);
    
      if (current != static_cast<time_t>(-1))
      {
        cout<< ctime(&current) <<flush;
      }
    }
    the only thing i need that i can seem to do is to keep it running contantly in the background so it tells the correct time all the time

    can anyone help?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what exactly do you want to do? You obviously need a loop in your program. And perhaps it's a good idea to put a sleep in there as to not use up 100% of the CPU time and end up with a clock that is scrolling past so quickly that you can't read it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i just want a real time clock that tells the time just like the system clock

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and I'm asking you what part of producing such a thing that you are unable to do. I could of course write the whole thing for you (probably quicker than I could play twenty questions with you to figure out what you need to understand), but it would not serve as a good learning experience for you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i am finding it hard for the clock to tick, because at the moment the time is just static, i dont understand how to get it to keep changing to the correct time (if that makes sence)

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The posted code contains nothing to repeat the output, so I expect it to output one time value. You need some sort of loop to repeat it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    if i put a loop round it wont it just put the new time underneith the old time?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes. That would be a good start, don't you think.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    ok - ill have a go at it

  10. #10
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    one more question whilst we are on the subject of my clock - do you know how i can get to time to output on a lable?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean "label" (or lable)?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    label as it is going on a front end of a display rather and in a command prompt window

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean the title-bar at the top of the window, yes? You can set that, but it's non-trivial. [Of course, for a "command prompt", you could use a little bit of creativity in a batch-script to make "title" change the name.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    well i could have it there i guess but i meant in a text label that you can input in borland for example if you had a addition game you would have 2 edit boxes to put the numbers in, a button to do the calculation and then the label would output the result the lable where the result is i want to be my time.
    Last edited by peckitt99; 11-06-2007 at 07:57 AM.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you are talking about a windowed application, yes?

    In which case, you'd not be using cout and such, I would presume.

    If you are writing a windows app, then I would expect to make it real simple and just use a timer to redraw some text (and check the time inside the redraw function, just to make it simple). But whole setup of a windows app is quite different from a console app.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Niche Question: real time stock quotes
    By keira in forum C Programming
    Replies: 0
    Last Post: 03-08-2008, 11:06 AM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  3. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM
  4. Real time issues
    By athos in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2003, 02:19 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM