?
Solved

How do I separate my mortgage program into  methods?

Posted on 2007-09-30
6
Medium Priority
?
195 Views
Last Modified: 2010-04-02
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>

0
Comment
Question by:ghost8067
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19988523
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
 
LVL 8

Accepted Solution

by:
Anthony2000 earned 1000 total points
ID: 19988528
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1000 total points
ID: 19989224
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:ghost8067
ID: 19989937
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 19990116
>> 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
 
LVL 8

Expert Comment

by:Anthony2000
ID: 19991635
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question