CISC1600: Computer Science I Lab 5

Purpose: In this lab assignment of CIS1600, we will practice the design and implementation of functions in the setting of a simple time checking and difference calculation.

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:

  1. Prompt the user to enter time 1 in 12-hour am-pm format
  2. Read in the time (read the hour, min, second, am/pm/midnight separately)
  3. (Optional) Check if the user input time is valid or not, if not, display an error message and exit the program.
    	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"
    	
  4. Convert the 12-hour am-pm clock time format to military clock and output the result
    Please review Military Time Chart to see how to convert 
    military time to 12-hour am-pm clock time. 
    
  5. Prompt the user to enter time 2 in 12-hour am-pm format, read in the time, and convert it to military clock and output the result
  6. Compare the two times and report the result following the formats shown in the example below, time1 (in mulitary format) is before/the same as/after time2 (in mulitary format):
  7. Report the difference between the two times in seconds, e.g.,
    	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

#include 
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... 

}

...
You are required to design and implement the following functions:
  1. A function that converts the hour in 12-hour am/pm format into the hour in military format
  2. A function that check if two times (in military format) are the same or not
  3. A function that check one time is before the another time or not (both times are in military format)
  4. A function that calculates the difference between two military times in seconds
    		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
    		}
    		
Coding Style Requirement:
  1. Please indent your code in as required.
  2. Use descriptive names for variables
  3. Use blank lines to separate the code into appropriate blocks
  4. Be sure to include comments to help others understand your program, and help yourself think!

How to submit

Please submit your program (which has to be named lab5.cpp) to the following webpage:

Lab5 submission link.