2

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.

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;
}
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
im dumb
  • 151
  • 1
  • 4
  • 9

3 Answers3

4
cout << "Elapsed time: " << rawtime2 - rawtime << " seconds" << endl;
jspcal
  • 50,847
  • 7
  • 72
  • 76
4

printf ( "You took %s", ( asctime (timeinfo2) - asctime (timeinfo) ) ); //math won't work here

No. The difference between two char*s doesn't make any sense in this context. You really just need to take the difference of rawtime and rawtime2, which are already in whole numbers of seconds.

Also, you should not mix printf() and std::cout in code as it is not idiomatic and makes code harder to read. As such, perhaps something like this would be better...

#include <ctime>
#include <iostream>

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

    time(&rawtime);
    timeinfo = localtime(&rawtime);
    std::cout << "Current local time and date: " << asctime(timeinfo);
    std::cout << "Press enter\n";

    std::cin.ignore();

    time_t rawtime2;
    struct tm* timeinfo2;

    time(&rawtime2);
    timeinfo2 = localtime(&rawtime2);
    std::cout << "Later local time and date: " << asctime(timeinfo2);

    time_t const timediff = rawtime2 - rawtime;
    std::cout << "You took " << timediff << " seconds to press enter.\n";
}

Note:

main() should return int, not void.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 1
    On the contrary, the difference between two pointers is both well-defined and useful. For example, subtracting the beginning of a string from the return value from `strchr` to get the index where the match was found. It simply isn't the right operation for this task. – Ben Voigt Apr 07 '13 at 05:36
0

This is a somewhat cleaner solution, the main difference is using int main() and return 0; at the end, main should never return void, it's a bad programming practice.

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

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

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    cout <<  "Current local time and date:  " <<  asctime (timeinfo) ;

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

    time_t rawtime2;
    struct tm * timeinfo2;

    time ( &rawtime2 );
    timeinfo2 = localtime ( &rawtime2 );
    cout <<  "Later local time and date:   " <<  asctime (timeinfo2) << endl;

    int prinAll = rawtime2 - rawtime;

    cout <<  "You took "  << prinAll << " seconds to press enter" << endl; //math     won't work here

    cout << endl;

    return 0;
}
Petzey
  • 11
  • 1
  • 3