24 hour date format confusing

Guys this program is supposed to do this but I got as far as calculating the difference in hours then got mixed up on how to format to 24 hour time:

Write a program that determines the difference between two times in 24h00 format. The program must ask the user to enter two times in 24h00 format, convert the two times to seconds, get the difference, and display the result in 24h00 format again. The 24h00 format time should be input as hh mm ss, and displayed as hh:mm:ss, e.g. 13 hours, 12 minutes and 34 seconds will be input as 13 12 34 and displayed as 13:12:34.
The program must check which time is the smaller time before subtracting – the user may enter the times in any order. Verify that the times entered are legitimate times, using the assert macro, e.g. 23 65 01 is not a valid time

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
  #include <iostream>
using namespace std;
#include <iomanip>
#include <time.h>
#include <stdio.h>
#include <cassert>

int getdifference(int firstTime, int secondTime)
{

int firstTime_secs  = (firstTime/100) * 3600;
int secondTime_secs = (secondTime /100) * 3600;

if (firstTime > secondTime)
    return (firstTime_secs - secondTime_secs);
else if ( secondTime > firstTime)
    return (secondTime_secs - firstTime_secs);

}


int main()
{
int firstTime, secondTime, timeDifference;

cout << "Please enter the first time  in 24hour format (e.g. HH:MM:SS ): " ;
cin >> firstTime;
assert (firstTime >= 0 && firstTime <= 2400);
cout << "Please enter the second time  in 24hour format (e.g. HH:MM:SS ): " ;
cin >> secondTime;
assert (secondTime >= 0 && secondTime <= 2400);
assert (firstTime % 100 < 60 && secondTime % 100 < 60);

timeDifference = getdifference(firstTime , secondTime);
cout << " Difference in time between the two values in 24hour format is :" << " " << timeDifference /3600 <<endl;

return 0;
}
Remember that there are only 24 hours in a day, numbered 0..23.

Any time you get a number from the user, its hour must be in that range, inclusive.

Any time you are doing calculations, it does not have to be.

Any time you provide a number, you must take the remainder of dividing by 24.

So: 13 + 19 (1pm + 19 hours) = 32 o'clock.
32 % 24 (remainder of 32/24) = 8 o'clock.


Next, remember that there are 60 minutes in an hour, and 60 seconds in a minute.

7 hours * 60 min/hr * 60 sec/min = 25200 seconds.

To convert back, remember to use your remainder operations. For example, 123 seconds is:
123 sec % (60 sec/min) = 3 sec.
123 sec / (60 sec /min) = 2 min.
Therefore, 123 seconds --> 2 minutes and 3 seconds.

Likewise, N % (60 sec/min * 60 min/hr) --> minutes.

Etc.

Hope this helps.
Topic archived. No new replies allowed.