Functions and Errors

In this class, you will begin by correcting errors in an existing program. You will then modify the program by introducing a new variable to an existing function. Finally, you will make some generalizations about a number of similar functions.  

The program addtime.cpp  is supposed to add two time values (minutes and seconds) and output the result in hours, minutes, and seconds. However, the program has both syntax errors (found by the compiler) and logic errors (not found by the compiler). When you open the file in your editor, fill in your name and date in the comment section at the top of the program in the space provided (put this information at the top of every program you write). 

Correcting Syntax Errors

Frist try to compile this program.  It should not compile because it has some syntax errors. You should correct all syntax errors before continuing to the next part.  But be careful of cascading errors!  Often times, the first syntax error in your program causes other spurious syntax errors.  To avoid this trap, practice fixing one error at a time (i.e., fixing the first syntax error, then recompiling the program, etc.).

Correcting Logic Errors

When you have corrected all the syntax errors, your program will compile and run.  Again, be careful: a program that compiles is not necessarily a program the works correctly.  If a program compiles and runs, but does not produce the correct output, then the program has a logic error. In this part, you will find and correct the logic error so the program runs correctly.

The first step to finding logic errors is to write test cases to verify that your program behaves like you think it should.  For this program, and all future assignments, you will be asked to turn in tests that verify your program works. To test your program, you should think of (but not simply enter) all the possible different data your program may encounter. To create tests for your program, you should group similar data into a single test case, and pick a candidate data value that represents that test case. For this program, the following cases are suggested:
both times are zero minutes and zero seconds (empty case)
the two times' minutes added together sum to less than one hour (standard case)
the two times' minutes added together sum to more than one hour (overflow case)
the two times' minutes added together sum to exactly one hour (boundary case)
the two times' seconds added together sum to less than one minute (standard case)
the two times' seconds added together sum to more than one minute (overflow case)
the two times' seconds added together sum to exactly one minute (boundary case)
 

You should make a separate call to addTime in the function testAddTime for each test case (the first one is done for you). Additionally, you should print out the time value you expect to result from adding your two values. To better help you discover the logic error, you should try to identify what part of the code corresponds to each test case.

At this point, you should know which case causes the incorrect output from your program.  You should now fix the logic error before continuing.  To fix the error, do not rewrite the program, but rather make as few changes as possible to correct the program.

Variables

This part requires you to use an additional local variable in the function addTime. You should declare a new variable in the function to avoid repeated calculations in the formulas.  To do this, follow these steps:
  1. Look at the function addTime and find repeated calculations in the equations.
  2. Declare a new variable to store the calculation so you only have to do it once.
  3. Replace the expression in the equations with your variable.
Compile and run your program on all of your test cases.  Make sure your output matches the output you got in the previous part of the lab (this verifies that you did not introduce any new errors while making this change to your program).

Different Calculations?

Add two new functions to your program (if you do not know the conversion rates, you should look them up on the Internet or other resource):
  1. AddDistance: adds together feet and inches and outputs the result in miles, feet, and inches
  2. AddMoney: adds together English shillings and pence and outputs the result in pounds, shillings, and pence
Additionally, you will need to modify main to to prompt for additional data and call your new functions.  It is suggested you compile, run, and test your program after completing each function.  You need only provide test cases in your test file for the cases where the sum overflows each category.  Make sure you write the correct pre- and post-conditions for your functions.

Once you have completed and tested all three functions, discuss their similarities and differences in your README file.  From your observations, can you write a single function that handles all three cases?  Why or why not?