Calculate Monthly Mortgage

I'm having an issue with my calculations or maybe how I have my class objects setup. I am not receiving any errors and the program runs fine, but it produces a monthly payment of 6.25519e-007.

I cannot figure out what the problem is for the life of me, any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #ifndef LOAN_H
#define LOAN_H
using namespace std;

    //Base class
class Loan
{
    public:
//        Loan();
//        virtual ~Loan();

            //function to get total borrowed amount (b)
        float getLoanAmount()
        {
            cout << "Enter The Principle Loan Amount: " << endl;
            cin >> loanAmount;
        }

            //function to get and store the annual interest rate (r)
        float getInterestRate()
        {
            cout << "Enter The Annual Interest Rate: " << endl;
            cin >> annualInterest;
        }

        float getTermYears()
        {
            cout << "Enter the Number of Term Years for the Loan: " << endl;
            cin >> termYears;
        }


        float loanAmount;       //holds the principle amount borrowed
        float annualInterest;     //holds the annual interest rate input
        float termYears;        //hold number of years desired for the term on loan

};

#endif // LOAN_H


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef MORTGAGE_H
#define MORTGAGE_H
#include "Loan.h"
using namespace std;

    // Derived class of Loan class
class Mortgage : public Loan
{
    public:
        Mortgage() : mPayment(){
        }

//        Mortgage()

                //function to hold the monthly interest calculation
       void getMonthlyInterest()
        {
            monthlyInterest = annualInterest / 12;

        }

                //function to get the number of months/term for the loan
        void calcMonths()
        {
            numMonths = termYears * 12;

        }
                //function holds the temporary exponent for payment calculations
        void getTempExponent ()
        {
            tempExponent = powf((1.0 + monthlyInterest), numMonths);
        }
                //function performs the calculations for monthly pay
        float calcPayment ()
        {
            return (loanAmount * ((monthlyInterest * tempExponent) / (tempExponent - 1.0)));
        }

        float displayPayment(float mPayment)
        {
            cout << "The Monthly Mortgage Payment is: " << mPayment;
        }



    private:
        unsigned short numMonths;       //stores the number of payments for the loan
        float tempExponent;             //holds temporary exponent for use in monthly payment calculation
        float mPayment;                  //holds the monthly payment for the said loan
        float monthlyInterest;


};

#endif // MORTGAGE_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>
#include "Loan.h"
#include "Mortgage.h"

using namespace std;

int main()
{

    Loan L_amt, I_rate, T_yr;
    float loanAmount;
    float annualInterest;
    float termYears;
    loanAmount = L_amt.getLoanAmount();
    annualInterest = I_rate.getInterestRate();
    termYears = T_yr.getTermYears();
    cout << endl << endl;




    Mortgage monthpay;
    float mPayment;
    mPayment = monthpay.calcPayment();
    monthpay.displayPayment(mPayment);



    return 0;
}
You aren't actually saving any variables to monthpay, your Mortgage class. You can check this yourself by doing something like:

 
cout << monthpay.loanAmount << "\n";


It will return either zero, or effectively zero (10e-7). The reason for this is that you're declaring many different classes: L_amt, I_rate, T_yr, monthpay. You should only have to (and really, only should) declare a single Mortgage class monthpay. Since Mortgage is derived from Loan, you can set all the loan variables in monthpay. Here's what I would do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream>
#include <cmath>
#include "Loan.h"
#include "Mortgage.h"

using namespace std;

int main()
{
Mortgage monthpay;              //You may wish to rename this given the following changes
monthpay.getLoanAmount(); //This sets the loan amount of the mortgage
monthpay.getInterestRate(); //This sets the interest rate of the mortgage
monthpay.getTermYears();    //...

cout << endl << endl;

float mPayment;
mPayment = monthpay.calcPayment();
monthpay.mPayment = mPayment; 
monthpay.displayPayment(mPayment);

return 0;
}


The last few lines are a little bit awkward, and only necessary because of the way you've written the calcPayment() function. IMO, instead of returning the value in calcPayment, just write this in your calcPayment function:

1
2
3

mPayment = (loanAmount * ((monthlyInterest * tempExponent) / (tempExponent - 1.0)));


Then, you can write
 
monthpay.displayPayment(); //change the displayPayment() function to have no arguments, and have it cout the mPayment variable stored in the Mortgage 


Hope this helps.



I made all the suggested changes, but I am now returning "nan" for the monthly payment.
Here's my edited source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef MORTGAGE_H
#define MORTGAGE_H
#include "Loan.h"
using namespace std;

    // Derived class of Loan class
class Mortgage : public Loan
{
    public:
        Mortgage() : mPayment(){
        }

                //function to hold the monthly interest calculation
       void getMonthlyInterest()
        {
            monthlyInterest = annualInterest / 12;

        }

                //function to get the number of months/term for the loan
        void calcMonths()
        {
            numMonths = termYears * 12;

        }
                //function holds the temporary exponent for payment calculations
        void getTempExponent ()
        {
            tempExponent = powf((1.0 + monthlyInterest), numMonths);
        }
                //function performs the calculations for monthly pay
        float calcPayment ()
        {

            mPayment = (loanAmount * ((monthlyInterest * tempExponent) / (tempExponent - 1.0)));
        }

        float displayPayment()
        {
            cout << "The Monthly Mortgage Payment is: " << mPayment;
        }


        float mPayment;                  //holds the monthly payment for the said loan

    private:
        unsigned short numMonths;       //stores the number of payments for the loan
        float tempExponent;             //holds temporary exponent for use in monthly payment calculation
        float monthlyInterest;


};

#endif // MORTGAGE_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cmath>
#include "Loan.h"
#include "Mortgage.h"

using namespace std;

int main()
{


    Mortgage monthpay;
    monthpay.getLoanAmount();
    monthpay.getInterestRate();
    monthpay.getTermYears();
    cout << endl << endl;

    float mPayment;
    mPayment = monthpay.calcPayment();
    monthpay.mPayment = mPayment;
    monthpay.displayPayment();

    return 0;
}
Make these changes to your main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    Mortgage monthpay;
    monthpay.getLoanAmount();
    monthpay.getInterestRate();
    monthpay.getTermYears();
    cout << endl << endl;

    //float mPayment    don't need this line
    //mPayment = monthpay.calcPayment() don't need this
    monthpay.calcPayment();     //Added this line
    //monthpay.mPayment = mPayment;  don't need this
    monthpay.displayPayment();

    return 0;
}


That SHOULD fix it. The issue was that you don't have a return value for the calcPayment() function (since that's what I suggested). Setting mPayment = monthpay.calcPayment() therefore didn't set it to what was intended. With the reworking of your code, what calcPayment is doing is setting the value of mPayment INSIDE the Mortgage class. Then, you can display that internal variable with monthpay.displayPayment();.

If you're doing this for a class/because you really want to learn C++, I recommend you read further into class usage. Seems like you're trying to grasp the idea, but using it in the wrong way/in a round-about way.
Last edited on
So I made some changes to cleanup main, but I am still having problems getting my calculations correct. In my Mortgage.h file I set all the variables in the calcPayment function to cout so I could test and see which variables are giving me trouble. When I did that I see that the variables monthlyInterest and numMonths are not calculating correctly, and I think that is throwing the rest of the calculations off.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Mortgage : public Loan
{
    public:
        Mortgage() : mPayment()
        {

        }

                //function to hold the monthly interest calculation
        void calcMonths()
        {
            numMonths = termYears * 12;

        }
        void getMonthlyInterest()
        {
            monthlyInterest = annualInterest / numMonths;

        }

                //function to get the number of months/term for the loan
                //function holds the temporary exponent for payment calculations
        void getTempExponent ()
        {
            tempExponent = pow((1.0 + monthlyInterest), numMonths);
        }
        
                //function performs the calculations for monthly pay
        float calcPayment ()
        {
            mPayment = (loanAmount * ((monthlyInterest * tempExponent) / (tempExponent - 1.0)));
        }

        void displayPayment()
        {
            cout << "The Monthly Mortgage Payment is: " << mPayment << endl;
        }


        float mPayment;                  //holds the monthly payment for the said loan

//    private:
        float numMonths;       //stores the number of payments for the loan
        float tempExponent;             //holds temporary exponent for use in monthly payment calculation
        float monthlyInterest;


};

#endif // MORTGAGE_H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Loan
{
    public:

            //function to get total borrowed amount (b)
        void getLoanAmount()
        {
            cout << "Enter The Principle Loan Amount: " << endl;
            cin >> loanAmount;
        }

            //function to get and store the annual interest rate (r)
        void getInterestRate()
        {
            cout << "Enter The Annual Interest Rate: " << endl;
            cin >> annualInterest;
        }

        void getTermYears()
        {
            cout << "Enter the Number of Term Years for the Loan: " << endl;
            cin >> termYears;
        }


        float loanAmount;       //holds the principle amount borrowed
        float annualInterest;     //holds the annual interest rate input
        float termYears;        //hold number of years desired for the term on loan

};

#endif // LOAN_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{


    Mortgage monthpay;
    monthpay.getLoanAmount();       //This sets the loan amount of the mortgage
    monthpay.getInterestRate();     //This sets the interest rate of the mortgage
    monthpay.getTermYears();        //This sets the number of years for the loan term
    cout << endl << endl;

    monthpay.calcPayment();
    monthpay.displayPayment();

    return 0;
}
Last edited on
Those variables aren't being calculated. The way you've written your Mortgage class, the user has to explicitly call the monthpay.calcMonths() function for that variable to be set. So, the most direct way of fixing this is put that line in your int main() program. I do have a suggestion to change this structure though. The user should only have to enter the number of term years; once the term years are fixed, the number of months is obviously all fixed by the simple relation months = years * 12. You should make two quick changes:

1) Make the numMonths variable part of the Loan class, rather htan the Mortgage class. This is because if you have other types of Loans, they will of course also run for a certain number of Months. The number of months in the loan is not unique to a mortgage.

2) In your getTermYears() function, add on numMonths = TermYears*12 at the end of it. What this does is it makes it so that the user doesn't have to manually set the number of months by calling getNumMonths(), as mentioned above.

Topic archived. No new replies allowed.