problems with math?

Im new to c++ and ive been working on this assignment but my math always comes out horribly wrong. Ive followed the instructions from the book step by step but i still seem to be missing something... why is my math off so much?

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main ()
{
double loanAmmount;

double interestRate = .05;

int numberOfYears;

cout << "Loan Ammount: ";

cin >> loanAmmount;

cout << "Number of Years: ";

cin >> numberOfYears;

cout << "" << endl;

cout << "Interest Rate" << setw(25) << "Monthly Payment" << setw(17) << "Total Payment" << endl;

do {

double monthlyPayment = loanAmmount * interestRate / (1 - 1 / pow(1 + interestRate, numberOfYears * 12));

double totalPayment = monthlyPayment * numberOfYears * 12;

cout << interestRate * 100 << "%" << setw(22) << "$" << monthlyPayment << setw(12) << "$" << totalPayment << endl;

interestRate += .00125;
}
while (interestRate < .08125);


system ("PAUSE");

return 0;
}
Yeah... um... you need to add a decimal point and at least one zero to all the constant integers in that program (1 becomes 1.0, 12 becomes 12.0, etc) except the ones in setw(), else chaos will ensue.

-Albatross
Last edited on
That didnt seem to change anything my, monthly amounts still seem to be 4x what they should be
Maybe this.

 
double monthlyPayment =( loanAmmount * interestRate) / (1 - (1 / pow(1 + interestRate, numberOfYears * 12) ) );


Give hierarchy with use of parenthesis but Albatross made a good point. Integer divided by integer will give you an integer value. Integer divided by a real number will give you a real number.

I haven't compiled the program but see if that works.
pedestrian,
That helped somewhat but the math is still off i dont know where im going wrong
Can you give me a loan amount and year with the correct values so I can test it?
10000 for loan amount
and 5 years
values should start @

interest monthly total
5% 188.71 11322.74


thats what it should start at.
Thanks for helping
Topic archived. No new replies allowed.