c++ - 试图计算两次之间的差异,不会减去

标签 c++ time

我的程序所做的是首先打印当前时间,然后用户按下回车键。之后它再次打印出时间并计算用户等待按下回车键的时间。

我没时间做减法。我从 stackoverflow 的另一个问题中得到了打印本地时间的代码。

#include <iostream>
#include <ctime>
using namespace std;

void main()
{
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "Current local time and date: %s", asctime (timeinfo) );

    cout << "Press enter" << endl;
    cin.ignore();

    time_t rawtime2;
    struct tm * timeinfo2;

    time ( &rawtime2 );
    timeinfo2 = localtime ( &rawtime2 );
    printf ( "Later local time and date: %s", asctime (timeinfo2) );

    printf ( "You took %s", ( asctime (timeinfo2) - asctime (timeinfo) ) ); //math won't work here
    printf ( " seconds to press enter. ");
    cout << endl;
}

最佳答案

cout << "Elapsed time: " << rawtime2 - rawtime << " seconds" << endl;

关于c++ - 试图计算两次之间的差异,不会减去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15858552/

相关文章:

java - 如何将 currentTimeMillis 转换为可读的日期格式?

json - 使用按钮更新用户位置并快速绘制路线

Python代码,控制台文本的时间延迟

c++ - 不能在类外定义类函数

c++ - 将 n 个 char* 复制到另一个里面

c++ - 带 ESP8266 的 PMS5003 - 许多校验和错误

java - 将数据库时间转换为 IST 日期时间

javascript - 将像素转换为分钟

c++ - C++ 未处理的异常

c++ - 错误弃用了从字符串常量到 'char*' 的转换