getMonthlyPayment() uses term, which is set by getTerm() which is never called.
term was initialized to zero in your constructor, therefore you're dividing by zero in getMonthlyPayment(). That's a no-no.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
indeed, term is not 0, it is uninitialised
still an error that you need to fix
having to call function foo() before bar() is error prone, consider changing your design.
for example, you could use a constructor that takes parameters and compute your values there.
#include <iostream>
#include <cmath>
usingnamespace std;
class Mortgage
{
private:
double loan {}, rate {};
int years {};
public:
Mortgage(double l, double r, int y) : loan(l), rate(r), years(y) {}
double calcTerm() const { return pow((1.0 + rate / 12.0), 12.0 * years); }
double calcTotal() const { return calcMonthlyPayment() * years; }
double calcMonthlyPayment() const {
constauto t {calcTerm()};
return (loan * (rate / 12.0) * t) / (t - 1.0);
}
};
int main() {
cout << "Let's calculate the monthly payment on your mortgage.\n";
double l {};
cout << "Please enter dollar loan amount: ";
while ((cin >> l) && l < 0) {
cout << "Please enter a positive loan amount: ";
cin.clear();
cin.ignore(1000, '\n');
}
double r {};
cout << "Please enter the interest rate of the loan as a decimal: ";
cin >> r;
int y {};
cout << "Please enter the number of years of the loan: ";
cin >> y;
const Mortgage m1(l, r, y);
cout << "Your monthly payment amount is: " << m1.calcMonthlyPayment() << '\n';
cout << "The total amount paid at the end of the loan period is: " << m1.calcTotal() << '\n';
}
Let's calculate the monthly payment on your mortgage.
Please enter dollar loan amount: 100000
Please enter the interest rate of the loan as a decimal: .06
Please enter the number of years of the loan: 30
Your monthly payment amount is: 599.551
The total amount paid at the end of the loan period is: 17986.5