Section 5 - Mortgage Take 4
5B.5.1 Cleaning It All Up
We had the following remaining problems in our mortgage calculator:
- We were doing output directly in the main() rather than delegating that to the sayGoodbye() method
- The result may not have displayed the correct number of fractional digits to the right of the decimal point.
Now that we know how to fix all this, we will give our final rendition of the mortgage calculator. Here it is. Look for and understand the changes:
#include <iostream> #include <string> #include <cmath> using namespace std; // method prototypes void stateInstructions(); void getInput(); double computeMonthlyPayment(); void reportResults(double result); // global variables shared by more than one method double dblPrincipal, dblRate, dblYears; int main() { double answer; stateInstructions(); getInput(); answer = computeMonthlyPayment(); reportResults(answer); } // gives an overview to user void stateInstructions() { string instructions; instructions = "\nThe following program will calculate the \n" "monthly payment required for a loan of D dollars \n" "over a period of Y years at an annual \n" "interest rate of R%.\n"; cout << instructions; } // gets input void getInput() { string prompt; // get principal prompt = "\nEnter amount of the loan. (only use numbers, \n" "please, no commas or characters like '$')\n" "Your loan amount: "; cout << prompt; cin >> dblPrincipal; // get interest prompt = "\nNow enter the interest rate (If the quoted rate is 6.5%, \n" "for example, enter 6.5 without the %.)\n" "Your annual interest rate: "; cout << prompt; cin >> dblRate; // get length of loan prompt = "\nEnter term of the loan in years: "; cout << prompt; cin >> dblYears; } // computes and returns answer double computeMonthlyPayment() { double dblTemp, dblPmt, dblMoRt, dblMonths; // convert years to months dblMonths = dblYears * 12; // convert rate to decimal and months dblMoRt = dblRate / (100 * 12); // use formula to get result dblTemp = pow(1 + dblMoRt, dblMonths); dblPmt = dblPrincipal * dblMoRt * dblTemp / ( dblTemp - 1 ); // now that we have computed the payment, return it return dblPmt; } // displays results and sign off void reportResults(double result) { string signoff; signoff = "\nThanks for using the Foothill Mortgage <. \n" "We hope you'll come back and see us again, soon.\n\n"; cout.setf(ios::fixed); cout.precision(2); cout << "Your Monthly Payment: " << result << endl; cout << signoff; }
Look at main(). It is almost perfect. We have renamed sayGoodbye() to reportResults() which has the goodbye chatter combined with the display of the payment. The method calls cout.setf() and cout.precision() are used to control the number format of our output.
We only wish we could get rid of the global variables and pass them between getInput() and computeMonthlyPayment() as parameters somehow. But that won't work yet (why?). We'll have to save that for another day. That's all for this lesson.
Prevent Point Loss
- Separate output and calculation. Methods that do calculations should not do input or output. Methods that do input or output should not calculate (except possibly, that input methods might do some range checking on the values entered by the user). (2 - 3 point penalty)
- Do not use globals. Whenever possible, use parameter passing and functional returns to communicate data between functions (methods); do not use global variables. In your assignments, globals will never be necessary as a means of passing information back-and-forth to functions, and you will always lose points if you use them to do so. (2 - 4 point penalty)
- Make sure your method signature matches the spec. Don't assume that the method does not take parameters just because we talk about it informally with empty parentheses, as in adder(). Find the official signature, like double adder(double a, double b), and write to that spec. Even one missing or incorrectly typed parameter will result in a large point loss since this renders your method useless to others in the group -- none of their code will work unless your method signature matches the design spec. (5 - 10 point penalty)