Write a C++ program to compute a monthly payment for a mortgage loan. Your program will
ask the user to enter annual interest rate, length of the mortgage, and the amount of the principal.
Then compute the monthly payment according to the information that the user entered. Use the
following formula to compute the monthly payment:
M = P(i / (1 – (1 + i) t))
Where
M is the monthly payment,
P is the principal value,
i is the monthly interest rate,
t is the length of the mortgage in months.
Output the monthly payment including the information the user entered by both displaying on the
screen and to a file. Try to make the input/output from your program correspond as closely as
possible to the sample input/output below.
Here is my code:
- Code: Select all
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main ( )
{
float i; //interest
int t; //loan years
double p; //principal
float m; //monthly payment
cout<<"***This program calculates a monthly payment for a loan***"<<endl;
cout<<"\nEnter the annual interest -->";
cin>>i;
cout<<"\nEnter the number of years for the loan -->";
cin>>t;
cout<<"\nEnter the principal amount of the loan -->";
cin>>p;
//Below is the formula to compute the monthly payment.
//Formula is M = P(i / (1 – (1 + i) - t))
m = p* i/1*-(pow((1+i),-t));
//The output that will appear on the screen
fstream project6; //fstream to write to file
cout<<"\n****************************************************"<<endl;
cout<<"* A MORTGAGE LOAN CALCULATOR *"<<endl;
cout<<"****************************************************"<<endl;
cout<<"\nThe annual interest rate of your mortgage is "<<i<<"%"<<endl;
cout<<"The length of your mortgage is "<<t<<" years"<<endl;
cout<<"The principal amount of your mortgage is $"<<p<<endl;
cout<<"Your monthly payment is $"<<m<<endl;
project6.open("h:\\project6.txt",ios::out); //written file location flash drive
//change cout to project6 to write file
project6<<"****************************************************"<<endl;
project6<<"* A MORTGAGE LOAN CALCULATOR *"<<endl;
project6<<"****************************************************"<<endl;
project6<<"\nThe annual interest rate of your mortgage is "<<i<<"%"<<endl;
project6<<"The length of your mortgage is "<<t<<" years"<<endl;
project6<<"The principal amount of your mortgage is $"<<p<<endl;
project6<<"Your monthly payment is $"<<m<<endl;
project6.close();
cin.get(); cin.get();
return 0;
}
I played with the formula to calculate the monthly payment and it works if I chnage i +1 to just i. But i need to add 1 to the interest. I dont know what I am doing wrong. Is the formula wrong?
Everything else works great the program writes the output to a notepad file in my flash drive. I just cannot get to formula to compute

