Objectives
- Gain experience with reference parameters
- More practice designing and coding programs built out of functions
- Learn how to use input redirection to test programs
Program description
When complete, this lab’s program should do the following:
- Read in two times (a start time and wait time, both using a 24-hour clock)
- Calculate when the wait is over
- Convert that time to a 12-hour clock and display it
Setup
Update the source code for your labs repo by running the following commands in the terminal:
$ cd labs
$ git pull
January 26 update: If you haven’t run the steps to fix your labs configuration, now is a good time. Follow the instructions here .
Steps
Open
by_ref.cpp,by_ref.h, andmain.cpp. Copy the function header forget_timefrom the header file toby_ref.cppand implement a function that reads a time entered in the formatHH:MMand updates 2 reference parameters - the hours and the minutes. You can assume the data is always in the correct format.To test this write a simple test driver
mainprogram (inmain.cpp) that writes a prompt, calls the function, and prints out the time. Don’t worry about formatting - just use a simplecout.Modify
mainso it also prompts for a waiting time, calls theget_timefunction to read the wait time, and prints it out (mostly to confirm things are working).
Now implement the function
calc_end_time. This function receives 4 input parameters (hours and mins for start time and wait time) and updates 2 reference parameters (hours and mins for the end time). The time returned must be valid in 24 hour time (e.g. if you start at 23:30 and wait 2:15, the end should be 1:45).This is the function that is assessed for the lab! From now on, this yellow box highlights the assessed code.
Modify
mainto callcalc_end_time(and print out the end time after the call)Implement the function
convert_12hour, which is given a reference to the hours in 24-hour format. This function should update the hours parameter to represent the time in a 12-hour format. It should also update a parameter which will contain'A'or'P'for AM or PM respectively.Hint: think about how you would cycle around the clock - the modulo operator might be handy! You will need an
ifstatement for the AM/PM part though.Modify main to call
convert_12hourand print out the result as follows:Finish at 7:25PMTo test your complete program, create a text file called
time.txt. It should just contain 2 lines as follows:17:45 1:40Use this file as the source of input (rather than having to enter it from the keyboard) by using a feature of Linux called input redirection. This is a useful way to saving test input, so that you don’t need to retype it each time you run a program.
To use input redirection, run your program as follows:
$ ./main < time.txt
Lab Assessment
As in the previous lab, there is a unit test associated with this lab. To run it, cd up a directory to your top-level labs repository and recompile the test program:
$ cd ..
$ make lab=by_ref
This will both compile and run the test program. Note that this has also changed from last week, as I realized that there are more problems caused by trying to run the tests for all labs at once.
January 26 update: If you haven’t run the steps to fix your labs configuration, now is a good time. Follow the instructions here .