Instruction: We will write a program that prompts the user to enter two time in the 12-hour am-pm clock time format, and then converts the two times to military format, and reports the difference between the first time and the second time, in the amount of seconds passed from time 1 to time 2. Please name your source code as lab5.cpp.
Your program works as follows:
A time is valid if the hour is between 1 and 12, and the minute is between 0 and 59, and the second is between 0 and 59. And the string is "am" or "pm" or "midnight"
Please review Military Time Chart to see how to convert military time to 12-hour am-pm clock time.
The difference between 13:12:23 and 14:00:00 is 2857 seconds.
An example run of your program is as follows, where the user inputs are underlined, and the program output are shown in regular format:
$ ./a.out Please enter time 1 in 12-hour am/pm format (HH:MM:SS am/pm/midnight):01:12:23 pm This is 13:12:23 in military format. Please enter time 2 in 12-hour am/pm format (HH:MM:SS am/pm/midnight):02:00:00 pm This is 14:00:00 in military format. 13:12:23 is before 14:00:00. The difference between 13:12:23 and 14:00:00 is 2857 seconds.
Note: whem output a time, you need to have two digits for hour, min and second, e.g., 13:05:01. You can refer to this code example to learn how to display the leadning 0.
Program Structure Requirement: In your program
#includeYou are required to design and implement the following functions:using namespace std; /* Declare all functions here */ int ToMilitaryHour (int hr, string am_pm); ... int main() { } /* Define all your functions here */ //based upon the am_pm, return the corresponding hour in military format //e.g., if hr is 2, am_pm is "pm", then return 14, as 2pm is 14 in // military format int ToMilitaryHour (int hr, string am_pm) { //body of ToMilitaryHour... } ...
The difference betweeh h1:m1:s1 and h2:m2:s2 is (h2-h1)*60*60+(m2-m1)*60+(s2-s1) The function shall return the absolute value of the above.Note, you can use the abs function defined in cmath header.
#include <iostream> #include <cmath> using namespace std; int main() { int num=-10; int result=abs(num); //result will be 10 }
How to submit
Please submit your program (which has to be named lab5.cpp) to the following webpage: