My current homework assignment is to code a mortgage calculator that will allow the user to input the appropriate information and return the monthly payment, the interest paid and the principal paid. An amortization schedule for the entire lenghth of the loan is also required.
I have managed to get my loop counter to step through each month for the entire life of the loan. My problem is that instead of the program also stepping through each payment, it repeats the same payment information and balance for the complete loan. The balance is never decreased by the amount of each montly payment. I have traced the problem down to something in my loop counter. My problem is that I cannot find what in my loop counter is wrong. all my math calcuations are correct and the montly payment is accurate. So I believe the problem has to be in my loop. I just need some help finding it. I have enclosed my code. Please bear in mind that even though i have placed my code in the proper code tags some of the formating may still be off.
// simple mortgage calculator
// programmer - Ronald Scurry
// class - PRG410
// facilitator - Jane Wu
#include <iostream>
#include <cmath> // must include for pow function
using std::cout;
using std::cin;
using std::endl;
int main ()
{
// defines varibles
double loanAmt = 0.0;
double interest = 0.0;
double term = 0.0;
char quit;
do
{
// user input
cout << "Enter interest rate:" << endl;
cin >> interest;
cout << endl;
cout << "Enter term in years:" << endl;
cin >> term;
cout << endl;
cout << "Enter Loan amount:"<< endl;
cin >> loanAmt;
cout << endl;
cout<<"\n";
double monthlyPmt = loanAmt*(interest/1200)/(1-pow(1+(interest/1200),-1*term*12)); // amoritization formula
double loanAmnt = loanAmt;
cout << "For a "<<loanAmnt<<" loan your payment is " <<monthlyPmt<<endl<<endl;
double monthlyInt = loanAmt * (interest/1200);
double principalPaid = monthlyPmt - monthlyInt;
double balance = loanAmt-monthlyPmt;
for (double i=0; i<term*12;i++)
{
double monthlyInt=balance*(interest/1200);
double principalPaid=monthlyPmt-monthlyInt;
if (balance<1.00)balance=0.0;
cout<<" New loan balance for month "<<i+1<<" is "<<balance<<endl<<endl;
cout<<" Your interest paid for this month is " <<monthlyInt<<endl<<endl;
cout<<" Your Principal paid for this month is " <<principalPaid<<endl;
cout<< "\n";
getchar(); // pauses the program and allows you to view the output
}
// user can continue in a loop or quit
//output
cout<<"\n";
cout<<"If you wish to continue press C then the enter key on your keyboard.\n";
cout<<"If you wish to quit press Q then the enter key on your keyboard.\n";
//user input
cin>>quit;
cout<<"\n";
}
while((quit!='q') && (quit!='Q'));
cout<<"Thank you for trying my simple mortgage calculator\n";
}