# How do I separate my mortgage program into methods?

My instructor commented that my program should have methods and not all be in main. I was wondering if someone could clarify what this means and how I should approach changing my program.

My code is below.
Thanks!

/*  This program will accept the loan amount, The term of the loan, and the
interest rate and display the monthly payment to the screen. The user
will then be offered the choice of running another loan or quitting.
@author Jon Schab
@version 2.0
Changes for this version - Program altered to accept loan terms rather
than having them hard coded as in the previous version.
*/

//#include <iostream>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main()
{

//declare variables

double loanAmount; // loan amount
double interestRate; // interest rate
double interestRateMonthly; // monthly interest rate
int termYears; // term of loan in years
int termMonths; // term in months
double monthlyPayment; // monthly payment
char again = 'N'; // NOT char again = Y;

do
// Loop to allow the user another calculation
{
// reset fields for anpother mortgage
loanAmount = 0.00;
termYears = 0;
interestRate = 0.00;

//display questions and recieve input from user.
cout << endl; // add blank line before title.
cout << "\t\t\tJon's Mortgage Calculator" << endl << endl; // Calculator Title

while(loanAmount <= 0.00) //check for positive value
{
cout << "\n\n \t\t\t Loan amount: \$"; // ask user for loan amount
cin >> loanAmount;

}

while(termYears <= 0) //check for positive value
{
cout << "\n      \n \t\t\t Loan Term In Years: "; // ask user for loan term
cin >> termYears; // capture loan term
}
while(interestRate <= 0) //check for positive value
{
cout << "\n\n \t\t\t Yearly Interest Rate: "; // ask user for interest rate
cin >> interestRate; // capture interest rate
}

//calculate term in years and monthly interest rate
interestRateMonthly = ((interestRate / 12) / 100);
termMonths = (termYears *12);

//calculate mothly payment
monthlyPayment = (loanAmount *interestRateMonthly) / (1 - pow(1.0 / (1.0 +
interestRateMonthly), termMonths));

//display terms and monthly payment to the screen.
cout << "\n\n \t\t\t Monthly Payment \$" << monthlyPayment << endl << endl;

// Offer the user another calculation
cout << (
"\n\n\t Enter Y or y to calculate another mortgage, any other key to quit  ");
again = _getch(); // capture response for another calculation
system("cls"); // clear the screen
}
while (again == 'Y' || again == 'y'); // End of loop for another go.

system("PAUSE");
return 0;
}
#include <stdlib.h>

###### Who is Participating?

Commented:
So, you can for example:

create a method (function) to take care of the getting the input. This would do the prompting to the user and check to make sure that the input has been checked for errors and such.

Next, you could create a method (function) to perform the calculations and lastly you could create a function that will format and print the information.

Does this help?
0

Commented:
I think your instructor is trying to get you to break up program into logical blocks and create methods to represent them.
By doing so, you program will be easier to read and maintain.

0

Commented:
Just think of what your program does, then think of how you can split that up into several "steps" and "modules", and put each of those in a separate function.

Take a look at this tutorial about functions :

http://www.cplusplus.com/doc/tutorial/functions.html
0

Author Commented:
I think I understand what needs to be accomplished. I have no experience with functions, but gave a couple a try. The printtitle function I added works fine. I then attempted to put in a function that gets the loan amount. I have an error when t runs. Can anyone give me a clue how to correct this? Also, should I be writing a separate function to get each piece of data, or should I be getting all 3 at once? If that is the case, any pointers on hpow I would do that?
Thanks!

/*  This program will accept the loan amount, The term of the loan, and the
interest rate and display the monthly payment to the screen. The user
will then be offered the choice of running another loan or quitting.
@author Jon Schab
@version 2.0
Changes for this version - Program altered to accept loan terms rather
than having them hard coded as in the previous version.
*/

//#include <iostream>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

void printtitle ()
{
//Print blank line followed by title
cout << endl; // add blank line before title.
cout << "\t\t\tJon's Mortgage Calculator" << endl << endl; // Calculator Title

}

double getloanamount ()
{
double l;
while(l <= 0.00) //check for positive value
{
cout << "\n\n \t\t\t Loan amount: \$"; // ask user for loan amount
cin >> l;

}
return l;
}

int main()
{

//declare variables

double loanAmount; // loan amount
double interestRate; // interest rate
double interestRateMonthly; // monthly interest rate
int termYears; // term of loan in years
int termMonths; // term in months
double monthlyPayment; // monthly payment
char again = 'N'; // NOT char again = Y;

printtitle ();
loanAmount = getloanamount ();
do
// Loop to allow the user another calculation
{
// reset fields for anpother mortgage
loanAmount = 0.00;
termYears = 0;
interestRate = 0.00;

//while(loanAmount <= 0.00) //check for positive value
//{
//cout << "\n\n \t\t\t Loan amount: \$"; // ask user for loan amount
//cin >> loanAmount;
//
//}

while(termYears <= 0) //check for positive value
{
cout << "\n      \n \t\t\t Loan Term In Years: "; // ask user for loan term
cin >> termYears; // capture loan term
}
while(interestRate <= 0) //check for positive value
{
cout << "\n\n \t\t\t Yearly Interest Rate: "; // ask user for interest rate
cin >> interestRate; // capture interest rate
}

//calculate term in years and monthly interest rate
interestRateMonthly = ((interestRate / 12) / 100);
termMonths = (termYears *12);

//calculate mothly payment
monthlyPayment = (loanAmount *interestRateMonthly) / (1 - pow(1.0 / (1.0 +
interestRateMonthly), termMonths));

//display terms and monthly payment to the screen.
cout << "\n\n \t\t\t Monthly Payment \$" << monthlyPayment << endl << endl;

// Offer the user another calculation
cout << (
"\n\n\t Enter Y or y to calculate another mortgage, any other key to quit  ");
again = _getch(); // capture response for another calculation
system("cls"); // clear the screen
}
while (again == 'Y' || again == 'y'); // End of loop for another go.

system("PAUSE");
return 0;
}

0

Commented:
>> I then attempted to put in a function that gets the loan amount. I have an error when t runs. Can anyone give me a clue how to correct this?

What is the error you get ?

There's one mistake I can already tell you : you need to initialize l to a value <= 0.0 - otherwise the loop will never execute.

>> Also, should I be writing a separate function to get each piece of data, or should I be getting all 3 at once? If that is the case, any pointers on hpow I would do that?

Well, which parts you put in functions is up to you. There's a few things you have to keep in mind though :

1) A function has to be a logical component that performs a certain task (with given input and producing certain output)

2) A good candidate for a function is a task that is generic and probably will be used again.
0

Commented:
It appears as though you have a good start with breaking the problem into methods. When you have completed, it would be great if your main looked something like this:

do
{
// get input values  (create three functions, you asked wether to create one or three)

// call method to perform calculations

// print results
}

Of course you would put your methods where I put the comments.

With a layout like this, you can easily see what is going on and you probably don't have to have many comments here because someone can see what the program is doing from a high level.

Does this help?

It takes time to be able to figure out how to break a problem into methods. I studied books, read other people's code to see how they broke  problems into methods, etc. Experimentation does not hurt either, especially when you are learning.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.