problem with geting difftime

hi all
why line 42 will give me Error: what im doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
void difc();
int m;
struct tm * timeinfos;
ofstream dif("e:\\dif.txt");
int main()
{
    cout<<"press 1 for Dif:";
    int x;
    cin>>x;
    if(x==1){
        void difc();
        return 0;
    }
    time_t rawtime;

    time (&rawtime);
    timeinfos = localtime (&rawtime);
   cout<<"Current local time and date:"<<asctime(timeinfos)<<endl;
   dif.open("e:\\dif.txt");
   dif<<asctime(timeinfos);
   dif.close();



}
void difc(){
    time_t rawtime;
    struct tm * now;
    double secs;
    time (&rawtime);
    now = localtime (&rawtime);
    fstream dif;
   dif.open("e:\\dif.txt");
   dif>>timeinfos->tm_hour;
   dif>>timeinfos->tm_min;
   dif>>timeinfos->tm_sec;
    dif.close();
 secs=difftime(now,mktime(&timeinfos));
    cout<<secs;
}

in this program first want u to get an int if is 1 first will get current time and will go for txt file and opened and take time that saves last time
in line 42 i Get if int is not 1 will get current time and save it into txt file for later that you want get difftime between this and another time.(difftime that i say i mean only Seconds)
what im doing wrong?
Last edited on
> why line 42 will give me Error

mktime() has to be given a pointer to tm or tm*; &timeinfos is a pointer to pointer to tm ie. tm**

It is much simpler to write the time_t value into the file and read it later as a time_t.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;

void difc() ;

const char* const path = "e:\\dif.txt" ;

int main()
{
    cout << "press 1 for Dif: ";
    int x;
    cin >> x;

    if( x == 1 ) difc() ;

    else
    {
        time_t rawtime = time(0) ;

        const tm* timeinfos = localtime ( &rawtime );
        cout << "Current local time and date: " << asctime( timeinfos ) << endl;

        ofstream dif(path);
        dif << rawtime << '\n' ;
    }
}

void difc()
{
    time_t now = time(0) ;

    ifstream dif(path);
    time_t earlier = 0  ;
    dif >> earlier ;

    const double secs = difftime( now, earlier );
    cout << "elapsed: " << secs << " seconds.\n" ;
}
It looks to me like you are calling mktime incorrectly.
Try correcting the second parameter by declaring struct tm timeinfos; instead of struct tm * timeinfos;
This will fix line 42, but break lines 38-40 until you change the ->s to .s on those lines.

Reference: http://www.cplusplus.com/reference/ctime/mktime/
Topic archived. No new replies allowed.