Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,628 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,042 people online right now. Registration is fast and FREE... Join Now!




c++ Calculator Class OOP

 
Reply to this topicStart new topic

c++ Calculator Class OOP, Help with code

clinpharm1
post 4 Apr, 2006 - 08:24 AM
Post #1


New D.I.C Head

*
Joined: 5 May, 2005
Posts: 3

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);

}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 4 Apr, 2006 - 10:18 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


In your main function, you declare Selection as an integer variable...you are then trying to perform a comparison on that variable, but the variable has been given no value.
User is offlineProfile CardPM

Go to the top of the page

clinpharm1
post 4 Apr, 2006 - 01:15 PM
Post #3


New D.I.C Head

*
Joined: 5 May, 2005
Posts: 3

Thanks Amadeus, I added a value. Selection = 0; the same thing occurs with integer i so I said i=0; The program begins to run then stops and I get this error.
"Unhandled exception at 0x0041f669 in Wk3MortCalc.exe: 0xC0000005: Access violation reading location 0x33463048."

Is this a memory error and, if so, where might it be coming from?
User is offlineProfile CardPM

Go to the top of the page

bluesuus
post 4 Apr, 2006 - 11:12 PM
Post #4


D.I.C Head

Group Icon
Joined: 26 Dec, 2005
Posts: 57



Dream Kudos: 25
My Contributions


what exactly are you trying to do.You declare variables 'Selection' and 'i' in your class.and also you are declaring them in main.Then you are making comparison with the variables in main which are always zero because they never change.What i think you needed to do was add set and get methods to the class so that you can access the private variables 'Selection and 'i' and then make the comparison like

if ( myMortgage.getSelection() ==1 ) and so on

Also, i think you should ask the user to input his Selection after he has inputted the amount because thats the only way selection can change and also the if Statement.

i think ill compile it later cuz right now im in a class but try and see what i am trying to say weda it makes any sense
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:38AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month