Question: Write a C++ program that takes two different train departure times (where 0 is midnight, 0700 is 7:00 a.m., 1314 is 14 minutes past 1:00
Write a C++ program that takes two different train departure times (where 0 is midnight, 0700 is 7:00 a.m., 1314 is 14 minutes past 1:00 pm, and 2200 is 10 pm) and prints the difference between the two times in hours and minutes. Assume both times are on the same date and that both times are valid. For example, 1099 is not a valid time because the last two digits are minutes, which should be in the range of 00 through 59. 2401 is not valid because the hours (the first two digits) must be in the range of 0 through 23 inclusive. Do not error check. Additionally 5 would be 5 minutes after midnight and 923 would be 9:23 am. If train A departs at 1255 and train B departs at 1305 the difference would be 0 hours and 10 minutes. Here are some sample dialogs.
---------------Run 1 ----------------- Train A departs at: 1305 Train B departs at: 1255 Difference: 0:10
---------------Run 2 ----------------- Train A departs at: 59 Train B departs at: 101 Difference: 0:2
---------------Run 3 ----------------- Train A departs at: 0 Train B departs at: 2359 Difference: 23:59
---------------Run 4 ----------------- Train A departs at: 2359 Train B departs at: 1 Difference: 23:58
Notes:
- Use the free function abs(int) to return the positive difference between two integers. For example, abs(102 - 400) is 298
- Do NOT use ANY if-else statements please
- The first input may be earlier, the same, or later than the 2nd input
- Valid inputs include 1, or 01, or 001, or 0001 for for 00:01 am, 222 for 2:22 am, and 2345 for 11:45 pm
- You don't need to error check the input times. Assume input represents a valid time, which could be 0, 111, or 2359 for example
- We will not test your code with invalid input such as 2400 or -999
- Suggestion: Start by computing both departure times as minutes after midnight
- 2245 / 100 is 22
- 2245 % 100 is 45
- abs(a-b) returns the positive difference between a and b
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
