Updating ctime

The following is what I have come up with regarding a small program that should show the current time as it updates every second. However, it is just showing static time. I previously added a counter to line 16 to make sure it was updating properly and it is, but after removing it, the time is still static.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
  #include <ctime>
  using namespace std;
 
  int main() {
  
    time_t current = time(0);

    tm *ltm = localtime(&current);
    
    while (true){
      cout << "\r" << 1900 + ltm->tm_year;
      cout << "-" << 1 + ltm->tm_mon;
      cout << "-" << ltm->tm_mday;
      cout << " " << 1 + ltm->tm_hour << ":";
      cout << 1+ ltm->tm_min << ":";
      cout << 1 + ltm->tm_sec << flush;
      sleep(1);

    }
}
closed account (E0p9LyTq)
Why not use the time functions the C++ <chrono> library provides?
I have to use ctime. Also, just realized l left some parts out of the loop p;
Topic archived. No new replies allowed.