So my assignment is to go a little farther in this Mortgage calculator. by the way thank you for all the help thus far. I am trying to get this right, but can seem to get the math right.
Here is the assignment:
Write the program as a procedural C++ program. Calculate and display the mortgage payment amount using the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage as input by the user. Then, list the loan balance and interest paid for each payment over the term of the loan. On longer-term loans, the list will scroll off the screen. Do not allow the list to scroll off the screen, but rather display a partial list and then allow the user to continue the list. Allow the user to loop back and enter new data or quit. Insert comments in the program to document the program.
I have the code built, it compiles, but half way through the list it shows "0", and i dont know where i am messing that up. Can you please help!!
Thanks
#include <math.h>
#include <iostream>
using namespace std;
int main () {
// defines varibles
double principle = 0.0;
double interest = 0.0;
double years = 0.0;
float total = 0.0;
char quit;
do {
do {
// user input
cout << "Enter interest rate:";
cin >> interest;
cout << endl;
if (! cin) {
cout << "\nInvalid Interest...Please enter valid interest rate ";
interest = 0;
cin.clear();
cin.ignore(512, '\n');
}
cout << "You entered: " << interest << "%" << endl;
}while (interest < 0.001);
do {
cout << "Enter Total years: ";
cin >> years;
cout << endl;
if (! cin) {
cout << "\nInvalid Years...Please enter valid years ";
years = 0;
cin.clear();
cin.ignore(512, '\n');
}
cout << "You entered: " << years << " years" << endl;
}while (years < 0.001);
do {
cout << "Enter Loan amount: $";
cin >> principle;
cout << endl;
cout<<"\n";
if (! cin) {
cout << "\nInvalid loan amount...Please enter valid loan amount ";
principle = 0;
cin.clear();
cin.ignore(512, '\n');
}
cout << "You entered: $" << principle << endl;
}while (principle < 0.001);
double monthlyInterest = (interest / 100) / 12;
int term = years * 12;
total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term)); //total for mortgage
//double total = principle*(interest/1200)/(1-pow(1+(interest/1200),-1*years*12)); // amoritization formula
double loanAmnt = principle;
cout << "For a $"<<loanAmnt<<" loan your payment is $" <<total<<endl<<endl;
double monthlyInt = principle * (interest/1200);
double principalPaid = total - monthlyInt;
double balance = principle-total;
for (double i=0; i<years*12;i++)
{
double monthlyInt=balance*(interest/1200);
double principalPaid=total-monthlyInt;
if (balance<1.00)
balance=0.0;
//CHANGED
else
balance -= total ;
//CHANGED
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";
cout << "To continue please press enter button.";
getchar(); // pauses the program and allows you to view the output
}
// user can continue in a loop or quit
//output
cout<<"\n";
cout<<"To continue press C then enter.\n";
cout<<"If you wish to quit press Q then enter.\n";
//user input
cin>>quit;
cout<<"\n";
}
while((quit!='q') && (quit!='Q')) ;
cout<<"Thank you for trying my simple mortgage calculator\n";
return 0 ;
}