Hello. I am having trouble debugging my code. I have a struct and a function to compute the time difference entered in HH:MM:SS format. My code is:
const int hourConv = 3600; // used to get total hours from total seconds
const int minConv = 60;
struct MyTime {
int hours, minutes, seconds;
};
MyTime *determineElapsedTime(const MyTime *time1, const MyTime *time2)
{
long timeOneSec = time1->hours*hourConv + time1->minutes*minConv + time1->seconds;
long timeTwoSec = time2->hours*hourConv + time2->minutes*minConv + time2->seconds;
long ans = timeTwoSec - timeOneSec;
cout << ans;
MyTime *timeDiff;
timeDiff->hours = ans / hourConv;
timeDiff->minutes = ans % hourConv / minConv;
timeDiff->seconds = ans % hourConv % minConv;
return timeDiff;
}
I believe the problem to be with the 2nd to last line:
timeDiff->seconds = ans%hourConv%minConv;
since when i comment that line out,
I do not get a segmentation fault error. But I don't understand why
that line is invalid. Any help would be appreciated. Thanks!