Hi everyone, I am in the process of writing my last program for my Introduction to C++ class. I have to write a program what will allow a end user to input an amount of a mortgage, from there, I have to get a monthly payment from the following choices which I have to use an array:
- 7 year at 5.35%
-15 year at 5.5%
-30 yea at 5.75%
I am currently reading about arrays, I figured I have to set a two-dimensional array, here is the code I have so far:
//************************************************** *****
//Program: Calculations Payments
//Purpose: To calculate a mortgage monthly payment by the user entering input
// and to have an array of years and Interest rates. This program will list the loan balance as well as the
// interest paid for each payment over the term of the loan.
// In addition, this program will allow the user to enter new data if they wish or to just quit the program.
//Programmer: Cameron Faldeti
//Date: 11/13/06
//School: University of Phoenix
//Class: POS 440: Introduction To C++
//Instructor: John Williams
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath> //Header file in order for a user to calculate math formulas
#include <string> //C++ header file for handling strings
using namespace std;
int main()
{
//Variable declarations
double LoanAmount; //Loan amount entered by the user in order to calculate a mortgage monthly payment
int term; //Years entered by user to use for calculating the monthly payment
double InterestRate; //Interest Rate in percentage used for the loan amount
double MortgageMonthlyPayment;
int NumOfPayments;
double LoanBalance;
int MonthlyMortgagePayment;
double Principle;
double InterestPaid;
char quit;//Allows users to quit the program if they wish
quit = 'G'; //If users enter G, they will have the option to continue
quit = 'g'; //If users enter g, they will have the option to continue
const char NL='\n'; //Originally from Joe Morton
const char TAB='\t'; //Line originally done by Joe Morton
int showlist=0; //To display the list needed in the program
char showmore;
double userEntry(); //Function for getting user input to display the mortgage monthly payment and to display any input errors if
//user enters the wrong kind of data. Original function by programmer Thomas Grzywacz.
//Stores Interest Rate and terms in arrays
double LoanArray[3][3] = {{7, 15, 30}, {5.35, 5.5, 5.75}};
//Allow user to loop back and enter new data or to just quit the program
do
{
cout << fixed << setprecision(2);
cout << "Enter a loan amount: " << "(no commas are allowed in the loan amount)";
LoanAmount = userEntry();
cout << "Loan amount is: $ " << LoanAmount << endl;
//Mortgage monthly calculation
MortgageMonthlyPayment = (LoanAmount* pow((1+ InterestRate/100/12),term*12)* InterestRate/100/12)/((pow((1+InterestRate/100/12),term*12))-1);
cout << "Mortgage monthly payment is: $ " << MortgageMonthlyPayment << endl;
//Lists loan balances and interest paid for each payment of the loan terms
//Lists loan balances and interest paid for each payment of the loan terms
NumOfPayments = term * 12; //Assigns a value to variable NumOfPayments
for(MonthlyMortgagePayment=1; MonthlyMortgagePayment<=NumOfPayments; ++MonthlyMortgagePayment)
{
//For finding the principle and Interest Paid
InterestPaid = LoanAmount * (InterestRate/1200);
Principle = MortgageMonthlyPayment - InterestPaid;
LoanBalance = LoanAmount - Principle;
if (LoanBalance <0)
LoanBalance = 0;
LoanAmount = LoanBalance; //This ends the payments to be made
//Displays a listing of loan balances and Interest paid
if (showlist == 0)
{
cout<< left <<setw(17)<<"Loan balance"<<TAB<<"Interest Paid"<<NL;
cout<< left <<setw(17)<<"---------------"<<TAB<<"-------------"<<NL<<NL;
}
cout<<LoanBalance<<TAB<<TAB<<InterestPaid<<NL;
++showlist;
//To see if the user wants to continue
if (showlist==12)
{
cout<<"Enter 'G' to go on," <<" 'N' for new data," <<" 'Q' to quit>";
cin>>showmore;
if ((showmore=='G')||(showmore=='g'))
showlist=0;
else if ((showmore=='N')||(showmore=='n'))
break;
else if ((showmore=='Q')||(showmore=='q'))
return 0;
}
}
cout << "Press N if you want to enter new data, or press 1 or press 2 to quit the program." << "Choice is: " << endl; //Gives users a chance to continue or quit
cin >> quit;
}while(quit!= '1'&& quit != '2');
system("PAUSE");
return 0; //End of program
}
//Function to find if user entered the data right. Returns userNumEntry original code by Thomas Grzywacz
double userEntry()
{
double userNumEntry = 0; //set user entry value to 0
double testEntry; //Used to test for any data that is entered that are not numbers
bool done = false; //Used to test if user is finished or not
string str = "Data you entered is invalid. \nPlease try again using numbers! "; //Program error message
do
{
cin >> userNumEntry; //Variable for user entry
done = true;
testEntry = cin.peek(); //Peek for entry streams
if (testEntry !=10||userNumEntry <=0) //if testEntry == 10 clear
{
cin.clear(); //value other than 10 is not correct
cin.ignore(200, '\n'); //Ignores the next 200 values until a newline occurs
userNumEntry = 0; //Resets user entry to 0
system("cls"); //clears the screen of any clutter
cout << str << endl; //Outputs program error message
done = false; //Makes done falise to continue do loop
}//end if
}//ends do
while (!done);
return userNumEntry; //value is returned to the main program
}//ends the function.
Any direction will be greatly appreciated.