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:
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace 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?
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?).
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?
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...
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?
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.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace 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.
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...
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace 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.
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
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace 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>
usingnamespace 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. :)
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.