Update time on screen

I have some code and i want to be able to update the time it displays so it looks like the clock is ticking without having to quit and run the program again. Also can you explain the code, i didnt write it, i found it on a website under examples. I dont quite fully understand it, i understand what the output does but i dont understand what its doing to get it. Also can you please explain it in lamens terms please. Thanks.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    time_t current = time(0);
    cout << ctime(&current)<< endl;
}
time_t is a type used to hold time values. Usually represents the number of seconds since 1970.
time(0) returns the current time as a time_t object.
ctime() converts the parameter to a human readable string representing the time stored.
If you just output current you'd get some huge number.
You could just put the whole thing in a loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
while(1)
{
    time_t current = time(0);
    cout << ctime(&current)<< endl;
}
}

This will constantly print the current time.
Is there any way to make it so it doesnt make 100 lines a second, can it be made to be just one line and it updates it constantly?
Read up on clearing the command prompt:
http://www.cplusplus.com/forum/beginner/3304/
I dont want to use winapi or the other method he mentioned though.
If you want to do anything except display the time, you will need to use the Windows API to reposition the text cursor in the display window.

If the only thing you are doing is displaying and updating the time, use "\r" to cause the cursor to go back to the beginning of the line.

Also, watch your indentation:

1
2
3
4
5
    while (1)
    {
        time_t current = time(0);
        cout << "\r" << ctime(&current) << flush;
    }

This will, of course, always run until the user presses ^C.

Good luck!
What i want is the time to be on one line, and it updates the time to show it ticking, i want basically what its doing now except i want it to look like its only one line.
Read my response again, dude.
I tried that though but it just kept making a bunch of lines with updated text, i swore there was a way to do it, without Winapi, cause i know how to do it using system("cls"); but i dont want to use it :(
@Ch1156

Try it this way.
1
2
3
4
5
6
7
8
9
// At the top of your program.. And don't forget to #include < ctime>
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}


1
2
3
4
5
6
 while (1)
    {
        time_t current = time(0); // Inside of int main()
        gotoXY(35,21);
        cout << "The time is : " << ctime(&current) << flush;
    }
That is, regrettably, using the WinAPI, which TC stated they did not want to use.
@Duoas
ctime returns a string followed by a newline. http://www.cplusplus.com/reference/clibrary/ctime/ctime/

This is ugly but it works...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <time.h>
#include <string>
using namespace std;

int main()
{
	while(1)
	{
		string s;
		time_t current = time(0);
		s = ctime(&current);
		s[s.length()-1] = '\r';
		cout << s;
	}
	return 0;
}
Last edited on
Regrettably, I did not check to make sure that ctime() works the way it is presented. It produces a string with a '\n' newline at the end. You need to strip that before printing.

1
2
3
4
5
6
    while (1)
    {
        string s = ctime(&current);
        s.resize( s.length() - 1 );
        cout << "\r" << s << flush;
    }

These things count. (Sorry.)
ok awesome, thanks for all the help guys.
Topic archived. No new replies allowed.