For homework I need to make a program that reads in two times (in 24 hour time) and prints the time elapsed.
The way I'm working this out is as follows...
12 : 00 : 00 (First time)
13 : 30 : 05 (Second time)
12 * 60 * 60 = 43,200 (12 hours in seconds )
(13 * 60 * 60) + 30 * 60 + 25 = 48,605 (second time in seconds)
48,605 - 43,200 = 5405 (first time minus second time)
Now to get the time elapsed...
5405 / 60 / 60 =
1.501388889 (Store the integer part as hours elapsed)
0.501388889 * 60 =
30.08333334 (Store the integer part as minutes elapsed)
0.08333334 * 60 =
5.0000004 (Store integer part as seconds elapsed)
--------------------------------------------------------------------------------
I hope I made that clear, I put in bold the parts that represent the elapsed time. I've tested this out on lots of times so I'm pretty sure this calculation works.
Anyway This is the code I'm using for the calculation, and also where the problem is, it is so close to working, but for some reason it's always 1 second off! I have no idea why it's doing this, and I'm hoping someone here might be able to help.
void elapsedTime( double elapse )
{
double hours;
double minutes;
double seconds;
modf( (modf( ( modf( ( elapse / 60 / 60 ), &hours ) ) * 60, &minutes ) ) * 60, &seconds );
cout << "Hours: " << hours << "\tMinutes: " << minutes << "\tSeconds: " << seconds;
getch();
}
Here's (
http://www.cplusplus.com/reference/clibrary/cmath/modf/) info on the modf function for those of you that don't know, it just splits the fractional part and the integer part of a number.
To be honest I'm not expecting someone to read though all this, let alone solve my problem, but if anyone does I'd be really thankful, and sorry if any of this is hard to read/unclear, I'm in a bit of a rush...