请用c++编写一个能实时显示当前时间的程序
以下是一个用C++编写的能实时显示当前时间的程序:
#include <iostream>
#include <ctime>
int main() {
while (true) {
// 获取当前时间
time_t now = time(0);
tm* currentTime = localtime(&now);
// 输出当前时间
std::cout << "当前时间:" << currentTime->tm_hour << ":" << currentTime->tm_min << ":" << currentTime->tm_sec << std::endl;
// 暂停1秒
sleep(1);
}
return 0;
}
这个程序使用time库获取当前时间,并使用localtime函数将时间转换为tm结构体,然后通过输出语句将当前时间打印出来。程序使用一个无限循环来实现实时显示当前时间,并使用sleep函数暂停1秒钟,以保证每秒更新一次时间
原文地址: http://www.cveoy.top/t/topic/hXHI 著作权归作者所有。请勿转载和采集!