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 h
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,<ime);//convert in local time and store in ltime
FileTimeToSystemTime(<ime,&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;
}
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).
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.
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;
}
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.
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);
}