试图计算两次之间的差值,不会减去 [英] Trying to calculate difference between two times, won't subtract

查看:214
本文介绍了试图计算两次之间的差值,不会减去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What my program does is that it first prints the current time, then the user presses enter. Afterwards it prints out the time again and calculates how long the user waited to press enter.

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

I can't get my time to subtract. I got the code for printing the local time from another question in 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;

这篇关于试图计算两次之间的差值,不会减去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆