Using functions to calculate periodic payments

Pages: 12
I have to write a program that prompts the user to input a loan amount (L), an annual percentage rate (r), the number of payments to be made in a year (m), the number of years the loan is for (t), and how many payments have already been made (k). The program then needs to calculate and output the periodic payment (R) and the unpaid balance after k payments (LPrime). Here are the equations I am given:

R = (Li)/(1 - (1 + i)^(-mt) where i = (r / m)

LPrime = R((1 - (1 + i)^-(mt-k)) / i)



Here is my program so far:

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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()

{
	float L; //loan amount
	float r; //annual percentage rate
	float m; //number of payments per year
	float t; //how long the loan is for in years
	float i; 
	float k; //number of payments made
	float R; //periodic payment
	float LPrime = 0; //unpaid balance after making k payments
	int exit;

	cout << "Please enter the loan amount." << endl;
	cout << "$";
	cin >> L;
	cout << " " << endl;
	cout << "Please enter the annual percentage rate." << endl;
	cout << "%";
	cin >> r;
	cout << " " << endl;
	cout << "Please enter the number of payments to be made per year." << endl;
	cin >> m;
	cout << " " << endl;
	cout << "Please enter the number of years the loan is for." << endl;
	cin >> t;
	cout << " " << endl;
	cout << "Please enter the number of payments that have been made." << endl;
	cin >> k;
			
	for (R = 0; LPrime <= L; LPrime--)
	{
		i = (r / m);
		R = ((L * i) / (1 - pow((1 + i), (-1 * m * t))));
		LPrime = ((R * (1 - pow((1 + i), (-1 * m * t * -k))) / (i)));
		break;
	}
	
	cout << " " << endl;
	cout << "The periodic payment is $" << R << "." << endl;
	cout << " " << endl;
	cout << "The unpaid balance is $" << LPrime << "." << endl;
	cout << " " << endl;
	cout << "Type 'exit' to exit." << endl;
	cin >> exit;
	
}


The program runs pretty well, but I'm getting some pretty wild results for the periodic payment and unpaid balance. Sometimes it gives me answers of -1.#IND. Can someone try to spot where I went wrong and let me know so I can fix it?
Last edited on
That's not a warning. That's an error.

You used LPrime in your for loop without defining what it is explicitly, and your compiler isn't happy about it.

I guess you intended zero, however you need to explicitly state that it's zero BEFORE you run any comparisons, else your compiler will curse you out for it (gee, can't they fix such things on their own?).


Please don't modify posts that much.... please?

-Albatross
Last edited on
So change line 37 from

 
for (R = 0; LPrime <= L; LPrime--)


to

 
for (LPrime = 0; LPrime <= L; LPrime--)


The same thing seems to be happening when I run the program.
Because now R is undefined. :)

Try defining LPrime and R outside of the loop, and eliminating your initialization options that are inside the loop.

LPrime = 0.0; R = 0.0; for (; LPrime <= L; LPrime--)

-Albatross
Ok, I've changed line 37 from

 
for (LPrime = 0; LPrime <= L; LPrime--)


to

 
LPrime = 0.0; R = 0.0; for (; LPrime <= L; LPrime--)


The program will run, but when I put in 1000 for the loan amount, 7.2 for the apr, 12 for the number of payments per year, 5 for the number of years, and 0 for payments made, it says the periodic payment is $600 and the unpaid balance is $0, which are both wrong, so I'm guessing my math is wrong somewhere, but I can't find where. I just copied the equations from my book into the program. Any idea where I'm wrong?
All well, I guess my hint was a bit too obscure.

Everywhere where you have an integer, it needs to also have a decimal point and something to the right of the decimal point (another integer). Else when the program tries to do something with that integer, there will be a problem that the program will automatically handle by setting the integer to zero. That's a simplification, but...

-Albatross
So change

 
float LPrime = 0;


to

 
float LPrime = 0.0;


I did that and I'm getting the same results. Unless you meant for me to do something else..
I said all the integers. That includes your ones and minus ones. :)

EDIT: Are you sure your exponent is right at line 41?

-Albatross
Last edited on
*facepalm* duh..

Well now that I've done that, I'm getting two warnings that both say "conversions from 'double' to 'float', possible loss of data" But I'm pretty sure I don't have the word 'double' anywhere in this program.

Edit: About the exponent, I was going to ask you the same thing.. I'm still trying to get used to how you are supposed to put in algebra in C++.. I'm guessing it's wrong?
Last edited on
pow() takes a double value as its input, but will also accept float. Don't worry about it.

Your exponent should be more like (k - m * t), if I understood the initial formula correctly.

-Albatross
Changed line 41 to

 
LPrime = ((R * (1.0 - pow((1.0 + i), (k - m * t))) / (i)));


And I'm getting an error that says "'pow' : 6 overloads have similar conversions"
Okay... weird. It seems that even gcc says that it should be okay, but ISO C++ hates it for some reason.
Here's the code to replace that line. It should work, but if it doesn't then this is one messed up language for dealing with x87 (floating point) instructions.


LPrime = R * (1.0 - pow(i++, (k - (m * t))) / i);
i--;


Guess what? It doesn't work properly.

-Albatross
Last edited on
Here is my 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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()

{
	float L; //loan amount
	float r; //annual percentage rate
	float m; //number of payments per year
	float t; //how long the loan is for in years
	float i; 
	float k; //number of payments made
	float R; //periodic payment
	float LPrime = 0.0; //unpaid balance after making k payments
	int exit;

	cout << "Please enter the loan amount." << endl;
	cout << "$";
	cin >> L;
	cout << " " << endl;
	cout << "Please enter the annual percentage rate." << endl;
	cout << "%";
	cin >> r;
	cout << " " << endl;
	cout << "Please enter the number of payments to be made per year." << endl;
	cin >> m;
	cout << " " << endl;
	cout << "Please enter the number of years the loan is for." << endl;
	cin >> t;
	cout << " " << endl;
	cout << "Please enter the number of payments that have been made." << endl;
	cin >> k;
				
	LPrime = 0.0; R = 0.0; for (; LPrime <= L; LPrime--)
	{
		i = (r / m);
		R = ((L * i) / (1.0 - pow((1.0 + i), (-1.0 * m * t))));
		LPrime = R * (1.0 - pow(i++, (k - (m * t))) / i);
		i--;
		break;
	}
	
	cout << " " << endl;
	cout << "The periodic payment is $" << R << "." << endl;
	cout << " " << endl;
	cout << "The unpaid balance is $" << LPrime << "." << endl;
	cout << " " << endl;
	cout << "Type 'exit' to exit." << endl;
	cin >> exit;
	
}


When I put in

loan amount = $1000
APR = 7.2%
payments per year = 12
loan length in years = 5
payments made = 0

I get $600 for periodic payment and a crazy answer for unpaid balance.
F***! This is why I code in Assembly...

Let's let the compiler have its way. I checked the details and it turns out that the compiler doesn't like floats being used in place of doubles, or vice versa, though it's erratic. Create a double named ipp outside of the for loop, and define it inside the loop after you define i as i +1.0. Use ipp in place of i++ and i +1.0. Also, get rid of that i-- just before the break statement, which by the way shouldn't be there (I just realized).

Then, change all the variables from floats to doubles.

EDIT: I need to learn to search for ALL the errors before posting, not just one... although it does raise my post count...

-Albatross
Last edited on
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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()

{
	double L; //loan amount
	double r; //annual percentage rate
	double m; //number of payments per year
	double t; //how long the loan is for in years
	double i; 
	double k; //number of payments made
	double R; //periodic payment
	double LPrime = 0.0; //unpaid balance after making k payments
	double ipp;
	int exit;

	cout << "Please enter the loan amount." << endl;
	cout << "$";
	cin >> L;
	cout << " " << endl;
	cout << "Please enter the annual percentage rate." << endl;
	cout << "%";
	cin >> r;
	cout << " " << endl;
	cout << "Please enter the number of payments to be made per year." << endl;
	cin >> m;
	cout << " " << endl;
	cout << "Please enter the number of years the loan is for." << endl;
	cin >> t;
	cout << " " << endl;
	cout << "Please enter the number of payments that have been made." << endl;
	cin >> k;
				
	LPrime = 0.0; R = 0.0; for (; LPrime <= L; LPrime--)
	{
		i = (r / m);
		ipp = (i + 1.0);
		R = ((L * i) / (1.0 - pow((ipp), (-1.0 * m * t))));
		LPrime = R * (1.0 - pow(ipp, (k - (m * t))) / i);
		break;
	}
	
	cout << " " << endl;
	cout << "The periodic payment is $" << R << "." << endl;
	cout << " " << endl;
	cout << "The unpaid balance is $" << LPrime << "." << endl;
	cout << " " << endl;
	cout << "Type 'exit' to exit." << endl;
	cin >> exit;
	
}


Did I do that right..?

Now both the periodic payment and unpaid balance are coming out as 600 using the same input from earlier.
Last edited on
you have a break in the for loop but without any condition. That means that the for loop will do just one "spin" and then the program conitnues to the output.

Just tell can the callculation of the loan + interest be done in this way:

Sum=L+( ( (L/100)*r )*t );
Than the periodic payment would be" Sum/(t*m)"

and that would simplify things.

Maybe I did'n

Perhaps I did not fully understand the problem, so if you can clarify

btw you didn't use eny functions in your code
Last edited on
Hey try to shorten up your code you have

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

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()

{
	double L; //loan amount
	double r; //annual percentage rate
	double m; //number of payments per year
	double t; //how long the loan is for in years
	double i; 
	double k; //number of payments made
	double R; //periodic payment
	double LPrime = 0.0; //unpaid balance after making k payments
	double ipp;
	int exit;
      // etc... 

Try to make it in to
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()

{
	double L, r, m, t, i, k, R, LPrime = 0.0, ipp;
	int exit;
// etc... 

don't fully quote me on that info i know that it works for the int case and maybe the float and double cases
Actually, MSkillet, declaring multiple variables per line should work for all types, classes, structs, and unions. For pointers it's a bit more difficult, but it's still possible.

And WahooMan, you really need to get rid of that break statement. It makes the loop redundant. :)

-Albatross
So what i said is correct??? SWEATNESS

XD Albatross you are one hell of a good teacher at this stuff
Hmm... never thought of myself as a teacher. Though now that you mention it maybe I could write an article...

Also, another thing. Usually, I check for syntax errors rather than formulation errors, but now, something doesn't strike me as right about this whole program... why do you have a for loop at all? Without that break statement, it will be infinite. I can't put my finger on it, but... something isn't right.

-Albatrosstudents

EDIT: 111 posts and counting
Last edited on
Pages: 12