|
Hi, I am new at this and am working on a C++ class assignment. I did this program as a procedural program in a previous class. Now I need to have it be object oriented by creating Classes. THIS IS HOMEWORK, so I am not looking to cheat, just get some direction. The code builds but I get a warning "warning C4700: local variable 'Selection' used without having been initialized". I have had a number of these warnings as I have worked on this. When I try to run the code I get "Run-Time Check Failure #3 - The variable 'Selection' is being used without being defined." I am sure there are a number of issues but "I can't see the forest for the trees". Can someone look at it and give me some feedback. I am placing the code here as well as attaching it as a notepad document. Thank you,
//*****************************************************************************
// Mortgage Calculator Program ** **
// This program simply calculates what the monthly payment of a mortgage **
// would give any inputted Loan Amount, and the choice of payment options of **
// 7 years at 5.53 percent, 15 years at 5.5 percent, or 30 years at 5.75 **
// percent. In addition, the program will show the amortization schedule of **
// those selected payment options. **
//*****************************************************************************
#include <cmath> // For calculation functions
#include <iostream> // This is a preprocessing directive
#include <iomanip> // Input & output maniplator
using namespace std;
class mortgageCalculator //specification file for class mortgage calculator
{
public:
void acceptMortgageInfo(); //function to accept mortgage info
void displayMortgageInfo(); //function to display mortgage info
private: //Variable declarations
double Amount; // Assign loan amount in dollars
int i; // Variable for array components
int Selection; // Variable for menu option choosing
char letter; // Assign character to end the program
};
void mortgageCalculator::acceptMortgageInfo(void)
{
//*******************************************************
// Prompts for loan amount from user, then displays a **
// menu option to choose which teams and interest rate **
// are acceptable for the loan amount. Holds all **
// variables for Loan Amount, Annual Interest, and **
// Terms of Years in assigned variable names **
//*******************************************************
cout << "Enter Loan Amount in U.S. Dollars: "; // Ask for loan amount
cin >> Amount; // Get loan amount
cout << endl;
//**************************************************************************
// Breakdown terms of years into months, calculates the monthly rate of **
// interest, and calculates, loan, rate and terms to find mortgage payment**
//**************************************************************************
}
void mortgageCalculator::displayMortgageInfo(void)
{ double yRate[3] = {5.35, 5.50, 5.75}; // Array assign to yearly interest rate
int Terms[3]= {7, 15, 30}; // Array assigned to terms of loan in number of years //int i;
int Months = Terms[i] * 12; //Holds number of months for payment
double mRate = yRate[i] / (12*100); //Holds amount of interest per month
double mPayment = Amount * (mRate/(1-pow(1+mRate,-Months))); //Holds amount for monthly payment
// ******************************************************************************** ****
//Displays the monthly payment from the calculated figures given and choosen by user**
// ******************************************************************************** ****
cout << "You selected " << Terms[i] << " year terms, " << endl;
cout << "with the interest rate of " << yRate[i] << "%" << endl;
cout << "The monthly payment would be " << "$" << mPayment << endl; //Prints Monthly payment
// ******************************************************************************** ****
//Ask if user would like to see an amortization schedule of how the monthly payments**
//will be divided to payoff the interest and the principal loan amount uttil done **
// ******************************************************************************** ****
cout <<"\nWould you like to view the amortization schedule? 'Y' or 'N'"; //Ask for character
cin >> letter; // Get character
cout << endl;
//*******************************************************************************
//Calculates the monthly payment to principal mortgage amount, and interest for**
//each month of payment. In addition, this section will display the principal **
//and interest payment per month, then decrease the balance amount until **
//balance is zero. **
//*******************************************************************************
double NewBalance = Amount; //Assign loan amount to new balance
int PaymentNumber = 0; //Set payment number count
int count = 0; //Assign pause counter
//*************************************************
//Display headings for the amortization schedule **
//*************************************************
cout <<"\nAmortization Schedule" << endl;
cout <<"_____________________" << endl;
cout <<"\nPayment \tMonthly \tPrincipal \tInterest \tCurrent" << endl;
cout <<"Number \tPayment \tPayment \tPayment \tBalance" << endl;
cout <<"======= \t======= \t======== \t======== \t=======" << endl;
while (letter != 'n' && PaymentNumber != Months) { //Begin embedded while loop
//**************************************************************************
//Calculate amounts and begin displaying figures for amortization schedule**
//**************************************************************************
double mInterestPayment = NewBalance * mRate;
double PrincipalPayment = mPayment - mInterestPayment;
NewBalance = NewBalance - PrincipalPayment;
PaymentNumber++; //Start number of months for amortization schedule
count++; //Start counter for displaying specific number of payments
cout.setf(ios::fixed); //Set fixed decimal place on dollar amounts
//*************************************************************************
//Outputs every twentieth calculated figure for the amortization schedule**
//*************************************************************************
cout << PaymentNumber << setprecision(2) <<"\t\t" <<
+mPayment << setprecision(2) <<"\t\t" <<
+PrincipalPayment << setprecision(2) <<"\t\t" << mInterestPayment <<
setprecision(2) <<"\t\t" << NewBalance << endl;
if (count==20){
system("pause");
count = 0;
}
} //ends embedded while loop
}
int main()
{
//Declare and initialize variables
mortgageCalculator myMortgage;
double yRate[3] = {5.35, 5.50, 5.75}; // Array assign to yearly interest rate
int Terms[3]= {7, 15, 30}; // Array assigned to terms of loan in number of years int Selection; int i; if (Selection == 1) i=0; else if (Selection == 2) i=1; else if (Selection == 3) i=2; else if (Selection >= 4) return 0;
int Months=Terms[i] * 12; //Holds number of months for payment
double mRate = yRate[i] / (12*100); //Holds amount of interest per month double Amount=0; double mPayment = Amount * (mRate/(1-pow(1+mRate,-Months))); //Holds amount for monthly payment
char escape;
//Display program title
cout << "Mortgage Calculator" << endl;
//Program loops to give user option of recalculating
do {
//Collect mortgage information from User
myMortgage.acceptMortgageInfo();
myMortgage.displayMortgageInfo();
//Ask the user if they would like to recalculate another mortgage
cout << "Press 'q' to quit or any other key to calculate another mortgage" << endl;
cin >> escape;
} while (escape != 'q' && escape != 'Q');
//Program will continue until user selects to quit with the q key
return(0);
}
|