Thread: c++ console project how to set value in a class

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    c++ console project how to set value in a class

    hi I am stuck at this point in my project I hope someone can help!!
    I am trying to set a value for
    Code:
    MortgageSchedule::MortgageSchedule(Mortgage m)
    {
    	return ;
    }
    The whole code is below
    Code:
    // Filename: MortgageSchedule.cpp
    // This contains stubs for the class methods.
    #include "MortgageSchedule.h"
    
    #include <cmath>
    #include <iostream>
    #include <iomanip>
     
    using namespace std;
    #include <fstream>
    
    MortgageSchedule::MortgageSchedule(Mortgage m)
    {	
    	return ;
    }
    void MortgageSchedule::setFileName(String s)
    { 
    	return;
    }
    
    void MortgageSchedule::displayOnConsole(void)
    { 
    	Monthly_Payment=MyMortgage.fixedMonthlyPayment();
    	Mortgage_Length=(MyMortgage.mortgageTerm()*12);
    	switch(Choice)
    	{
    	case 'Y':
    	case 'y':
    		break;
    		
    		//End of each Year remaining loan remaining to pay-off
    
    		cout<<setw(65)<<"Yearly Summary Report\n"
    			<<setw(60)<<"--------------------------------\n"
    			<<setw(10)<<"Yearly Payment"
    			<<setw(20)<<"Remaining Principal"
    			<<setw(20)<<"Yearly Interest\n";
    
    		for(int i=1; i<= Mortgage_Length; i++)
    		{
    			MyMortgage.makeMonthlyPayment();
    			
    			if((Mortgage_Length%i)==0)
    			{
    			
    				cout<<setw(9)<<"$"<<(MyMortgage.originalLoan()-MyMortgage.remainingPrincipal())
    					<<setw(19)<<"$"<<MyMortgage.remainingPrincipal()
    					<<setw(19)<<"$"<<MyMortgage.interestPaidSoFar();
    			}//end if
    			
    		}//End for
    
    		break;
    
    	case 'M':
    	case 'm':
    		//Monthly Payments
    		//What amount went towards principal per month.
    		//How much went towards interest per month.
    
    		cout<<setw(65)<<"Monthly Summary Report\n"
    			<<setw(60)<<"--------------------------------\n"
    			<<setw(10)<<"Monthly Payment"
    			<<setw(20)<<"Principal Deducted Per Month"
    			<<setw(20)<<"Interest Paid-off Per Month\n";
    
    		for(int i=1; i<= Mortgage_Length; i++)
    		{
    			cout<<setw(9)<<"$"<<Monthly_Payment
    				<<setw(19)<<"$"<<(Monthly_Payment-MyMortgage.monthlyInterest())
    				<<setw(19)<<"$"<<MyMortgage.monthlyInterest();
    			
    			//Payment
    			MyMortgage.makeMonthlyPayment();
    
    		}
    
    		break;
    	
    	default :
    		//Summary of Total Amount paid by the time Mortgage is paid-off.
    		//Summary of Interest that went towards payments.
    
    		for(int i=1; i<= Mortgage_Length; i++)
    		{
    			Total_Amount_Paid += Monthly_Payment;
    			
    			MyMortgage.makeMonthlyPayment();//not saved in variable
    			Check_Total_Interest_Paid += MyMortgage.monthlyInterest();
    		}//End for
    
    		Total_Interest_Paid=MyMortgage.interestPaidSoFar();
    
    		cout<< setw(65) << "Full Mortgage Summary Report\n"
    			<< setw(60) << "----------------------------------------\n"
    			<< setw(10)<<"Total Amount Paid"<<setw(20)<<"Total Interest Paid\n"
    			<< setw(9) <<"$"<<Total_Amount_Paid
    			<< setw(19) <<"$"<<MyMortgage.interestPaidSoFar()
    			<< setw(19) <<"check$"<<Check_Total_Interest_Paid;
    		break;
    	
    	return;
    } 	 
    void MortgageSchedule::writeToFile(void) 
    {	
    	return;
    }
    void MortgageSchedule::setPeriod(char c)
    {//vk modified
    	Choice = c;
    	return;
    }
    THIS IS THE MAIN FILE--THIS FILE WAS GIVEN TO ME
    Code:
    //assign3main_2.cpp
    #include <iostream>
    using namespace std;
    #include "mortgage.h"
    #include "MortgageSchedule.h"
    #include <conio.h>  //For getche() function
    #include "..\include\str.h"
     
    #include <conio.h>
    #include <fstream>
    // trial main for assignment 3
    
    int main()
    {
    	int choice;
    	double ourLoan, ourRate; int ourDuration;
    	cout << "\nInput loan, rate, and term separated by blanks --> ";
    	cin >> ourLoan >> ourRate >> ourDuration;
    	const Mortgage ourM(ourLoan, ourRate, ourDuration);//Husband and wife take out a joint mortgage.
    	MortgageSchedule wife(ourM);	// The wife's payment schedule
    	MortgageSchedule husband(ourM); // The husband's payment schedule
    
    	//Wife wants to control the format and output
    	cout << "\nMenu ";
    	cout << "\n\t1 for summary"
    		 << "\n\t2 for yearly"
    		 << "\n\t3 for monthly\n";
    	cout << "\n\nEnter 1 2 or 3 -->";
    	cin >> choice;
    
    	switch (choice)
    	{
    	case 1: 
    		wife.setPeriod('s');
    		break;
    	case 2:
    		wife.setPeriod('y');	
    		break;
    	case 3: 
    		wife.setPeriod('m');
    		break;
    	default: cout << "\n\n********* INCORRECT CHOICE INPUT ********************"; break;
    	}//endswitch
    	
    	int toMonitor;
    	
    // The wife can choose whether to display the result on the monitor or save to a disk file.
    	cout << "\nEnter 1 to display on the monitor and 0 to save to disk file ->";
    	cin >> toMonitor;
    
    	if (toMonitor)
    		wife.displayOnConsole();
    	else	
    	{
    		String fname;
    		cout << "\nEnter name of file --> ";
    		cin >> fname; // Clears carriage return from previous toMonitor input.
    		cin >> fname;
    		cout << "\nYou have entered -->" << fname;
    		wife.setFileName(fname);
    		wife.writeToFile();
    	}
    	// Husband sees yearly schedule on Console no matter what the wife has decided.
    	husband.setPeriod('y');
    	husband.displayOnConsole(); 
    	cout << "\nEnd of program....\n";
    	return 0;
    }
    Code:
    //Filename: MortgageSchedule.h
    
    #ifndef MortgageSchedule_H
    #define MortgageSchedule_H
    #include "mortgage.h"
    #include "..\INCLUDE\str.h"
    
    class MortgageSchedule {
    public:
     
    	void setPeriod(char c);
    	// y  yearly display
    	// m   monthly display
    	// s	summary display
    	// default: summary display
    	
    	void setFileName(String s);
    	// Sets diskfile name for output.
    
    	void displayOnConsole(void); 
    	// Writes the schedule to the console
    	// Period must be set by a previous message, 
    	// otherwise the a default schedule is used.
    
    	void writeToFile(void);
    	// Same as displayToConsole
    	// Except results are written to a 
    	// disk file which is set by a previous setFileName message.
    	// In case no previous message is sent. Output goes to "practice.txt".
    
    	String restrieveDiskFileName(void)const;
    		 
    	MortgageSchedule(Mortgage anyMortgage);
    
    private:
    	Mortgage MyMortgage;
    	/* You do this part*/
    	char Choice;
    	double Total_Amount_Paid_So_Far;
    	double Total_Amount_Paid;
    	double Total_Interest_Paid;
    	double Check_Total_Interest_Paid;
    	double Monthly_Payment;
    	double Mortgage_Holder;
    	
    	int Mortgage_Length;
    };
    #endif
    Code:
    //Filename: mortgage.h
    //Abbreviate for the tutorial
    
    #ifndef MORTGAGE_H
    
    #define MORTGAGE_H
    
    class Mortgage
    
    {
    public:
    // Modifiers
    	void makeMonthlyPayment(void);
    // Accessors
    	double rate(void) const;
    		// Returns the yearly interest rate
    	double originalLoan(void) const;
    	double remainingPrincipal(void) const;
    	double fixedMonthlyPayment(void) const;
    		//Returns the fixed monthly payment.
    	double monthlyInterest(void) const;
    		//Returns the interest due that month.
    	double interestPaidSoFar(void) const;
    		//Accumulates the amount of interest paid.
    	int mortgageTerm(void) const;
    		// Returns the total number of years of the mortgage
    	double marketValue(void) const;
    	//Backbone members
    	Mortgage(double principal,double yearlyInterestRate, int lengthOfTermInYears);
    	Mortgage(void);
    	~Mortgage(void);
    private:
    	double monthlyPayment;
    	double initialLoan;
    	double remainingLoan;
    	double monthlyInterestPayment;  // This varies month to month.
    	double totalInterest;	// Accumulates the interest paid so far.
    	double monthlyInterestRate;
    	int term;
    	double computeMonthlyPayment(void);
    	static const int DEFAULT_TERM;
    	static const double DEFAULT_LOAN;
    	static const double DEFAULT_RATE;
    
    };
    #endif
    Code:
    // Filename: mortgage.cpp
    // Abbreviate to work with Tutorial
    #include "mortgage.h"
    #include <math.h>
    const int Mortgage::DEFAULT_TERM = 30;
    const double Mortgage :: DEFAULT_LOAN = 40000.00; 
    const double Mortgage :: DEFAULT_RATE = 0.10;
    void Mortgage::makeMonthlyPayment(void)
    {
    	double interestDue, paymentToPrincipal;
    
    	interestDue = monthlyInterestRate * remainingLoan;
    
    	paymentToPrincipal = monthlyPayment-interestDue;
    
    	remainingLoan = remainingLoan - paymentToPrincipal;
    
    	monthlyInterestPayment = interestDue;
    
    	totalInterest += interestDue;
      	return;
    }
    double Mortgage::rate(void) const
    {
    	return 12 * monthlyInterestRate;
    }
    double Mortgage::originalLoan(void) const
    {
    	return initialLoan;
    }
    double Mortgage::remainingPrincipal(void) const
    {
    	return remainingLoan
    }
    double Mortgage::marketValue(void) const
    {
    	return initialLoan - remainingLoan;
    }
    double Mortgage::monthlyInterest(void) const
    {
    	return monthlyInterestPayment;
    }
    double Mortgage::interestPaidSoFar(void) const{
    	return totalInterest;
    }
    double Mortgage::fixedMonthlyPayment(void) const
    {
      	return monthlyPayment;
    }
    
    int Mortgage::mortgageTerm(void) const
    {
      	return term;
    }
    Mortgage::Mortgage(double principal,double yearlyInterestRate,
    
    					int lengthOfTermInYears)
    
    {
        term = lengthOfTermInYears;
    
        initialLoan = principal;
    
        remainingLoan = principal;
    
        monthlyInterestRate = yearlyInterestRate/12;
    
    	 monthlyPayment = this->computeMonthlyPayment();
    
    		 //Interest payments commence with monthly payments.
    
        monthlyInterestPayment = 0;
    
        totalInterest = 0;
    	return;
    
    }
    Mortgage::Mortgage(void)
    {
        term = DEFAULT_TERM;
    
        initialLoan = DEFAULT_LOAN;
    
        remainingLoan = DEFAULT_LOAN;
    
        monthlyInterestRate = DEFAULT_RATE/12;
    
        monthlyPayment = this -> computeMonthlyPayment();
    
    		//Interest payments commence with monthly payments.
    
        monthlyInterestPayment = 0;
    
        totalInterest = 0;
        return;
    }
    Mortgage:: ~Mortgage(void)
    {
      	return;
    }
    
    double Mortgage::computeMonthlyPayment(void)
    {
    	double temp1,temp2,temp3;
    	double answer;
    	int n = term*12;
    	temp1 = (1+monthlyInterestRate);
    	temp2 = pow(temp1,n);
    	temp3 = monthlyInterestRate/(temp2-1);
    	answer = temp3 * temp2 * initialLoan;
    	return answer;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Mortgage MyMortgage;
    I see there is now that member variable in MortgageSchedule. That wasn't there in your other thread. Now all you have to do is assign m to MyMortgage in your constructor.

    If something is confusing in the explanation please feel free to ask for clarification rather than starting a new thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. How to set win32 console back to the first line?
    By dxfoo in forum Windows Programming
    Replies: 6
    Last Post: 03-08-2008, 12:47 PM
  3. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM