Section 5 - Mortgage Take 4

5B.5.1 Cleaning It All Up

We had the following remaining problems in our mortgage calculator:

  1. We were doing output directly in the main() rather than delegating that to the sayGoodbye() method
  2. 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 &lt;. \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

guy with blackboard