c++ * / decimal issues

Pretty much all i need to know is how and why when i do my calculation for monthly interest it does not use decimals to a high value such as 10. I have tried using setprecision and it did absolutely nothing, i can only get that to change my output values. Anyways the answer is 332.14 due to setprecision(2) but MY answer is 324.68 which is basically .014/.430 and 1 more decimal place is .0143/4307 which gives you .033202 * 10000 to give you ALMOST the correct answer. so why and how can i fix this so i can get more precise on the decimals to get the proper answer


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

using namespace std;

int main()
{
double years, L, annual, payment, X, total, interest, R, N, SOB;
string name;

cout << "Please enter, in order, your name, how much your loan was in USD,\nyour annual interest rate, and how many years you have had this loan? ";
cin >> name >> L >> annual >> years;

R = annual / 12.0; // monthly interest

SOB = R / 100.0; // problem causer

N = 12.0 * years; // total number of months

X = 1.0 + SOB; // easier to type

payment = L * (SOB * (X) * pow (X , N)) / ((X) * pow (X , N) - 1.0); // formula for monthly payment

total = N * payment; // total amount paid

interest = total - L; // total interest after loan is paid off

cout << "Name: " << setw(27) << name << '\n';
cout << fixed << showpoint << setprecision(2); // show decimals for 2 decimal points
cout << "Loan Amount: " << setw(15) << " $ " << L << '\n';
cout << noshowpoint << setprecision(0); // remove the decimals for the next 2 lines
cout << "Monthly Interest Rate: " << setw(7) << R << "%" << '\n';
cout << "Number of Payments: " << setw(11) << N << '\n';
cout << fixed << showpoint << setprecision(2); // bring em back
cout << "Monthly Payment: " << setw(10) << " $ " << payment << '\n';
cout << "Total Amount Paid: " << setw(9) << " $ " << total << '\n';
cout << "Interest Amount: " << setw(10) << " $ " << interest << endl;
cout << endl; // blank line
// >>>> insert random note here <<<<
return 0;
}
http://imgur.com/iV1G2e6 might be better for you to look at, thanks!
[code]"your code goes here"[/code]

Your formula is incorrect. Better variable names would make it easier to spot


1
2
3
4
Payment = Loan interest (1+interest)^n / [ (1+interest)^n - 1 ] //https://en.wikipedia.org/wiki/Mortgage_loan#Principal_and_interest

Payment = Loan interest (1+interest) (1+interest)^n / [ (1+interest) (1+interest)^n - 1 ] //what you are doing
Payment = Loan interest (1+interest)^(n+1) / [ (1+interest)^(n+1) - 1 ] //simplified 
so you are making one extra payment

Instead of (X)*pow (X,N) is simply pow(X,N)
Thanks a lot! I thought what i was doing was taking X and then stating that i wanted to raise it by the N power not multiply X by X^N. I thought pow worked differently than how it does :( i spent like 2 hours trying to figure out what the hell was wrong with it lol thanks again!
Topic archived. No new replies allowed.