Using functions to calculate periodic payments

Pages: 12
Yea you should make an article about c++ programing it would be alot better then the examples that i have downloaded in the past
To be honest, I have no idea what I'm doing. I'm taking this class online through my community college, and all I've got to learn from are these crappy powerpoints my teacher made, and every couple of weeks I'll get an assignment that says "Read this powerpoint, write this program" And up until now I've somehow figured out how to do the assignment, but this one has got me completely stumped. It was due Friday and I cannot for the life of me figure out what to do, so I've come here hoping someone can point me in the right direction..

So if this program shouldn't have a "for" loop what should it be and why? And I thought exp(x, y) was a function?
Last edited on
I can not acces that presentation, if u could send it to me in an e-mail, I maybe could help you.
Or just say what you should do, or what that program you should write needs to do?
Yea I realized the powerpoint can't be accessed unless you're logged in as a student so I took it out of my post. But thanks anyway.

The program has to prompt the user to input

1. a loan amount,
2. an interest rate on the loan,
3. how many payments will be made per year to repay the loan,
4. how many years the loan is for,
5. how many payments have been already made.

And then outputs the periodic payment and remaining unpaid balance.

I was given two equations to use in my program, which you can find in the original post of this thread. I'm supposed to use at least two functions in my program, and I thought I was using at least one with exp(x, y) but apparently I'm not.

So if you have any hints, advice, etc. as to what I should do, please let me know.
Last edited on
When they say use two functions, I think they mean create two new functions.

I recommend having one function solve for R, and the other for LPrime! Just remember to declare your variables outside of any functions (outside of main).

If you don't know how to write a function (just in case), try:
http://cplusplus.com/doc/tutorial/functions/

My function have no arguments, and return void, but that's what I'd do.

Be sure to declare your function before main().

EDIT: -Albatross
Last edited on
Alright, I've rewritten my program. Here's what I've got now:

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
56
57
58
59
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double functionOne (double R, double L, double i, double r, double m, double t)
{
	i = (r / m);
	R = ((L * i) / (1.0 - pow((1.0 + i), (-1.0 * m * t))));
	return (R);
}

double functionTwo (double LPrime, double R, double i, double m, double t, double k)
{
	LPrime = R * (1.0 - pow((1.0 + i), (k - (m * t))) / i);
	return (LPrime);
}

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
	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;

	R = functionOne (R, L, i, r, m, t);
	LPrime = functionTwo (LPrime, R, i, m, t, k);

	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;
}


I'm still getting $600 for the periodic payment and a strange answer for the unpaid balance using the same input from before. But I think I'm at least starting to use functions right... right?
Last edited on
The syntax is all correct.

In function two, you're missing this: i = (r / m);

This post is prone to editing.

-Albatross

Last edited on
Added i = (r / m); to functionTwo, now I'm getting $600 for both answers.
Well, it's better, though still something is extremely wrong. For k = 0, L should equal LPrime... what's going on?

Maybe... that's it, I'm pulling out my compiler...

EDIT:
1.0 - pow((1.0 + i), (k - (m * t))) is the part that's giving you trouble. When you know what part's giving you trouble, try isolating it in another function. In this case, replace this and its cousin with another function. The code looks like this:

1
2
3
4
5
double calcFactor (float ker)
{
	double ipp = i + 1.0;
	return (1.0 - pow(ipp, ker - (m * t)));
}


It's only rarely that I give out a near-complete solution like that, but I personally wasn't 100% sure what to do here either.

-Albatross

P.S.- The formulae you got are wrong. If your annual percentage rate = 0...
Last edited on
About the APR = 0, I've thought about that too. I'm just using the formulas I was given. I was going to cross that bridge when I came to it, i.e. after I figured out how to make this thing work in the first place. Any idea what to do about that?

I was thinking of making a.. loop? (I'm trying to get all this lingo down) and do something like

 
if r = 0;


Can't I do something like that?
Last edited on
"number of times the interest is compounded per year"

is that the same thing as number of payments per year?

Edit: Am I on the right track of what you wanted me to 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
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double calcFactorFuncOne (double calcFactorOne, double k, double i, double m, double t)
{
	double ipp = i + 1.0;
	calcFactorOne = (1.0 - pow(ipp, k - (m * t)));
	return (calcFactorOne);
}

double calcFactorFuncTwo (double calcFactorTwo, double i, double m, double t)
{
	double ipp = i + 1.0;
	calcFactorTwo = (1.0 - pow(ipp, (-1.0 * m * t)));
	return (calcFactorTwo);
}
//20
double functionOne (double calcFactorOne, double R, double L, double i, double r, double m, double t)
{
	i = (r / m);
	R = ((L * i) / calcFactorOne);
	return (R);
}

double functionTwo (double calcFactorTwo, double LPrime, double R, double i, double r, double m, double t, double k)
{
	i = (r / m); //30
	LPrime = (R * (calcFactorTwo) / i);
	return (LPrime);
}

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 40
	double i; 
	double k; //number of payments made
	double R; //periodic payment
	double LPrime = 0.0; //unpaid balance after making k payments
	int exit;

	cout << "Please enter the loan amount." << endl;
	cout << "$";
	cin >> L;
	cout << " " << endl; //50
	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; //60
	cout << "Please enter the number of payments that have been made." << endl;
	cin >> k;

	R = functionOne (calcFactorFuncOne, R, L, i, r, m, t);
	LPrime = functionTwo (calcFactorFuncTwo, LPrime, R, i, r, m, t, k);

	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;
}


Namely was I supposed to create two new functions (calcFactorFuncOne and calcFactorFuncTwo) and use them in my other two functions? Also, where the two new functions are used in main(), I don't think I did right. Or did I?

I'm getting two errors:

1. error C2664: 'functionOne' : cannot convert parameter 1 from 'double (__cdecl *)(double,double,double,double,double)' to 'double'

2. error C2664: 'functionTwo' : cannot convert parameter 1 from 'double (__cdecl *)(double,double,double,double)' to 'double'

Note that I haven't tried changing the formulas yet.

Also, I only just noticed the compiler tells you exactly which line each error is on *facepalm* Is there any way to count how many lines I have, other than doing it myself?
R = functionOne (calcFactorFuncOne, R, L, i, r, m, t);
There's no calcFactorFuncOne in main.It's a function name,but it shall be a variable name.
Your code looks quite messy.

Also,
Just a reminder about previous posts
1
2
float number = 5.3f ; //Initialization of float
double dNum = 5.3 ; //Initialization of double 

Last edited on
Topic archived. No new replies allowed.
Pages: 12