1

Can some one tell me how do i display real time in c++. what i mean is that while the program is running you can see the seconds and or minutes counting down like a real clock hanging on the wall

this is what I have:

int main ()
{
  time_t rawtime; //creates and object of the built in time function
  struct tm * timeinfo; //no idea what this do

  time ( &rawtime ); //gets the time from the computer
  timeinfo = localtime ( &rawtime ); //store that time here

  //it displays current date and time except time is frozen and not real time
  cout<< "Current local time and date: "<<asctime (timeinfo)<< endl; 


  system("pause");
  return 0;

}
RvdK
  • 19,580
  • 4
  • 64
  • 107
T T
  • 27
  • 1
  • 2
  • 7
  • Also: you can use C++11's time classes. Plus one comment - use one way of putting spaces in your code (I advise: space before and after every operator, except of `&` used as "address of", no space after and before brackets - but that's purely individual. Anyway, keep using consistent style). – Griwes Mar 18 '12 at 15:29

6 Answers6

3

Not in C++ (in C/Win32) but works.

#include <stdio.h>
#include <windows.h>


int _tmain(int argc, _TCHAR* argv[])
{
    SYSTEMTIME stime;   //structure to store system time (in usual time format)
    FILETIME ltime;     //structure to store local time (local time in 64 bits)
    FILETIME ftTimeStamp;
    char TimeStamp[256];//to store TimeStamp information
    while (true){
    ////Prepare data needed to output the time stamp:
    GetSystemTimeAsFileTime(&ftTimeStamp); // Gets the current system time
    FileTimeToLocalFileTime (&ftTimeStamp,&ltime);//convert in local time and store in ltime
    FileTimeToSystemTime(&ltime,&stime);//convert in system time and store in stime

    sprintf(TimeStamp, "%d:%d:%d, %d.%d.%d \r",stime.wHour,stime.wMinute,stime.wSecond, stime.wDay,stime.wMonth,stime.wYear);
    printf(TimeStamp);

    Sleep(1000);
    }

    system("pause");
    return 0;
} 
SChepurin
  • 1,814
  • 25
  • 17
1

Add a system("cls");

Like this:

 time_t rawtime;
 struct tm* timeinfo;
 while(true)
 {
 system("cls");
 time(&rawtime);
 timeinfo=localtime(&rawtime);
 cout<<"Time : "<<asctime(timeinfo);
 Sleep(1000);
 }
picsoung
  • 6,314
  • 1
  • 18
  • 35
  • Welcome to StackOverflow! While this might answer the question, it's recommended you add some context to your answer. – picsoung Apr 26 '18 at 18:09
1

Some basic C++ will come in a long way: www.cplusplus.com

int main ()
{
    time_t rawtime; //creates and object of the built in time function
    struct tm * timeinfo; //no idea what this do

    while (true)
    {
        time( &rawtime ); //gets the time from the computer
        timeinfo = localtime( &rawtime ); //store that time here

        //it displays current date and time except time is frozen and not real time
        cout<< "Current local time and date: "<<asctime (timeinfo)<< endl; 

        sleep(1000); //1 second sleep
    }
    system("pause");
    return 0;
}
RvdK
  • 19,580
  • 4
  • 64
  • 107
  • 5
    No, no, no, and one more time: no! Don't use www.cplusplus.com, use cppreference.com as reference and some good book as learning tool, but never learn anything from cplusplus.com, which is known as one of the worst possible sites to learn anything from. – Griwes Mar 18 '12 at 15:26
  • Ok @PoweRoy that works except it keeps rewriting the time how do i get it to do all of that in one line..please? I hope am not asking for too much – T T Mar 18 '12 at 15:37
  • 1
    @Griwes - I've not been on there, but I could do with cheering up. Is there any particular section of that site I could get a laugh from? – Martin James Mar 18 '12 at 15:37
  • Ok @PoweRoy that works except it keeps rewriting the time how do i get it to do all of that in one line..please? I hope am not asking for too much – T T Mar 18 '12 at 15:56
  • `sleep(1000)` sleeps for 1000 seconds on a POSIX-ish machine. It may work as described on Windows. – Jonathan Leffler Mar 18 '12 at 16:02
  • yeah it but what i want to know now is how to replace the text each time...the '\r' not returning to the beginning of the line it keep giving me this: time is: 08:12 time is: 08:13 time is: 08:14 etc I jus want a single line – T T Mar 18 '12 at 16:08
  • ok how do i output that in one single line instead of a new line everytime it loops – T T Mar 18 '12 at 16:29
1

Try this:

while (true) {
    std::cout << '\r'; // return to the beginning of the line
    getAndPrintTime(); // do what you do now, but don't write endl
}

Assuming that you want to keep overwriting the same place in the terminal, two simple things to use are '\r' for carriage return, and '\b' for backspace (if you want to back up a character rather than a whole line).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

Below is the a function within a program of mine that displays the current day of week, time (hh:mm) and date (dd/mm/yyy). When I used the SYSTEMTIME struct, I realized the time displayed was four hours too fast, so I resorted to this method. Hope this helps. Intended for Windows users...

void time()
{
    cout << "The current date is: ";
    system("date/t");
    cout << "The current time is: ";
    system("time/t");
    cout << "Time zone: ";
    system("tzutil /g");
    cout << endl;
}

NOTE: This works by querying your system for the date, time, and timezone. Most seasoned programmers do not recommend utilizing the system() tool, but that is ultimately up to you.

c0d3r
  • 1
  • 1
0

The standard C++ language did not have any notion of time till the latest C++11 standard published in 2011, and rarely implemented. On Linux, you could consider using GCC 4.6 or 4.7 which implements most of it.

(older C++03 gives you <ctime>)

Otherwise, time is given by operating system specific libraries and system calls (such as gettimeofday and clock_gettime on Linux and Posix)

If you have a fully C++11 conformant implementation of the latest C++ standard (which may be unlikely, in particular on Windows), you could use the <chrono> standard header.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • no i do not have that new software..i was hoping there is another way around this – T T Mar 18 '12 at 15:28
  • On Windows or on any system without the latest C++11 ``, you have to use *system specific* functions to query the time. And you did not mention any operating system in your question! – Basile Starynkevitch Mar 18 '12 at 15:36
  • 3
    Your initial statement 'Standard C++ had no notion of time until C++11' is a gross overstatement. The C-based `` and `` headers provided support for time in C++. – Jonathan Leffler Mar 18 '12 at 16:04