Why am I getting inf for my output?

.
Last edited on
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.
Aren’t only the loan, rate, and years initialized to zero in the constructor?
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.
Indeed, term is not initialized. My mistake. Thanks ne555.
Perhaps:

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
#include <iostream>
#include <cmath>
using namespace 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 {
		const auto 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

Topic archived. No new replies allowed.