Trouble with Loan Payment Calculator Program

I have this homework assignment to create a loan payment calculator and I thought I was going to be able to do it with no issues but everytime I run it it gives me some wacky value.

We have to use this formula for the loan payment function:
Loan Amount = Principle (1+interest)^years)/(1+interest)^years)-1

I'm new to functions and I've referenced everything in the textbook from the functions chapter to try and figure it out but I cant get the thing to work. I must be doing something wrong with my function code but I have literally no clue what it could be.

The teacher gave us the follow values to test our code:

The monthly payment on a 3 year $20000 loan with an annual interest rate of
3.25% is $583.83

When I enter these values I get a strange a hex looking number. I don't know whats wrong :(

The 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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;


double payment(double, double, double);

int main () 
{
// need a loop here!

	double total, principle, interest, years;
	
	cout << "Enter loan amount: ";
	cin >> principle;
	cout << "Enter interest rate: ";
	cin >> interest;
	cout << "Enter number of years to pay back loan: ";
	cin >> years;
	
	payment(principle, interest, years);	
	
	total = payment(principle, interest, years);
	
	cout << "The monthly payment fon a " << years << " year $" << principle << 
	" loan with an annual interest rate of " << interest << " is $" << total;

	return 0;	
}

double payment(double principle, double interest, double years)
{
	double amount, A, P, r, n;
	
	A = amount;
	P = principle;
	r = interest;
	n = years;
	
	A = P * pow(1+r, n) / pow(1+r, n)-1;
	
	return amount;
}
Trace the value of "amount" in your payment function and you'll see that you never assign anything to it. You declare amount on line 34 and don't initialize it. There's no telling what value it holds. In this context, we'll call it "garbage". "amount" holds "garbage".

Then on line 36 you set "A" equal to "garbage". Whatever is in "amount" remains the same.

On line 41 you perform a calculation using variables that have definite values. You take the result from the calculation and overwrite the garbage in "A" with some value.

On line 43 you return "amount" which still contains "garbage".
Oh thanks I edited my code so that A is assigned to amount now! I get 19999 as an answer when it's supposed to calculate to 585.83 still.

The code
1
2
3
4
5
6
7
8
9
10
11
12
13
double payment(double principle, double interest, double years)
{
	double amount, A, P, r, n;
	
	amount = A;
	P = principle;
	r = interest;
	n = years;
	
	A = P * pow(1+r, n) / pow(1+r, n)-1;
	
	return amount;
}

You're still doing effectively the same thing that booradley pointed out.

Line 3: A is an uninitialized variable (garbage).

Line 5: You're assigning garbage to amount.

Line 12: You return garbage.

Topic archived. No new replies allowed.