CS 1320 (Principles of Algorithm Design I):
Homework #7

Assigned:
November 1, 1999.
Due:
November 8, 1999, at start of class.
Credit:
10 points.

Programming problem: Time sheets

In this homework, as in Homework #5, your mission is to fill in the blanks in an incomplete program.

Problem description

Ms. Ima Notta Late thought she had a cushy job at her local defense contractor. No matter how important her task, security closes the building at 11:59:59 pm every day, so she never had to work into the early morning hours of the next day. Furthermore, she was writing programs for administrative tasks, so she could avoid any tricky moral questions about the importance of protecting our country versus helping kill people.

Her boss, Ms. Gotta Haveit Now, assigned her the task of computing the hours worked by each employee. Under pressure from the Pentagon to better document work hours, she gave Ms. Ima Late three days to write the program. Unfortunately, Ms. Ima Late did not use her middle name enough and failed to complete the task in time. Her boss fired her, but Ms. Late was able to erase her hard disk before she left.

In a bind, Ms. Gotta Haveit Now turns to you to complete the program. She explains that the program's input consists of a file of lines, each listing the employee's Social Security number, starting time, and ending time (listed in military time, of course). The program's output is to be stored in a file having each line list the Social Security number and the total hours, minutes, and seconds worked by that employee that day. As an example of the input the program will read, she gives you last week's timesheets; as an example of output the program should produce, she gives you times she computed by hand. The program should prompt its user for the names of the input and output files.

Although Ms. Ima Late erased her hard disk, technical support was able to extract parts of the file containing her program. Ms. Now gives you these fragments, together with her comments. Since Ms. Late's program had already been approved by government inspectors (on temporary leave to the Pentagon from the Department of Energy public relations division) even though it was not yet finished (!!), Ms. Now requires you to use all the code fragments so she does not have to complete the bureaucratic paperwork again.

P.S. As Ms. Now walks away from your cubicle, you hear her mutter, "I hate reference variables. A mark of a good programmer is to use them only when necessary." Try to write your code in a way that will convince your new boss that you are a good programmer.

What to turn in

Turn in the following:

Hints

One of the easiest ways to subtract times is to convert hours, minutes, and seconds to seconds, subtract seconds, and then convert back to hours, minutes, and seconds. Apparently, this is what Ms. Ima Notta Late intended to do.

Be sure to use all the code given you.

You may also reuse code from any of the examples we have done in class, most of which are available via the sample programs Web page.

Addendum

While you are working madly to complete this project, Ms. Now is going over Ms. Late's code carefully. She discovers that the definition for function readIntInRange will not work on all computer systems, and she tells you to replace this function definition with the following:
bool readIntInRange(ifstream& inStr, int& result, const int min, const int max)
{
	// ">> dec" is needed because some systems otherwise think
	// that an integer starting with 0 is octal
	return ((inStr >> dec >> result) && result >= min && result <= max);
}

(If you haven't already started making changes to the code, you can obtain a revised copy here.)