How can I pass the value of these variables between functions of the same class?

Write your question here.

I'm working on an assignment for a programming class that I have, which comes with explicit instructions which are as follows :


Write a program to calculate the monthly payment on a loan given the loan amount, interest rate and the number of years to pay off the loan. Then add a function to print an amortization schedule for that loan. Create a class to save the current balance and the rest of the data as private data. Add member functions to make a payment and to print the amortization report.

Use the following class header.



Class LoadData
{
private:
double Bal;
double n;
double i;
double A;

public:
LoanData(double p, double n, double i);
void MakePayment(double a);
void PrintAmortizationSchedule();
void PrintPayOffTimeReport();
}

The Constructor member function
Thea header is:

LoanData(double p, double n, double i);

Where
p = loan amount,
n = number of years to pay off the loan, and
i == yearly interest rate as a percent (like 6 for 6%).

The body of the constructor will do the following:
1. Make the interest into a fraction by dividing i by 100.
2. Make the interest monthly by dividing i by 12;
3. Compute the number of months to pay off the loan by multiplying n by 12.
4. Compute the monthly payment, A, using the formula;

5. Save the balance p in Bal.

Example:

Say the user passes:
Loan amount p = 100000
Loan length n = 30 (in years)
Credit score i = 6

Then compute:
1.
2.
3.
4.
5.

The MakePayment member function
Thea header is:



void MakePayment(double pay);

Where
pay = payment amount.

The body of the function will do the following:
1. Compute and save the updated balance by applying the following formula:



Example:
Say the user makes a payment of $650.00 then




The PrintAmortizationScehdule member function
Thea header is:



void PrintAmortizationSchedule();

The body of the function will do the following:
1. Create a report with the following fields: beginning balance, payment, interest paid, principle paid, and ending balance. Use the following algorithm:

The beginning balance in the first line is the current balance (bal).
while beginning balance > 0
if (i+1)P > A then
Payment = A
else
Payment = (i+1)P
interest paid = iP
principle paid = A-iP
ending balance = (i+1)P - A
beginning balance = ending balance
print all these number on a single line.

Example:

Consider the previous example where:
i = 0.005,
A = $599.00
Bal = $100,000

Now to create a payment schedule we will do the following:

Compute:
Beginning balance is $99,850.00
Interest paid is iP = 0.005(99,850.00) = $499.25
Principle paid is A-iP = 599.55 – 499.25 = $100.30
Ending balance is (1+i)P – A = (1.005)99850.00 – 599.55 = $99,749.70

So the first line will be

$99,850 $499.25 $100.30 $99,749.70

The second line will be:

Beginning balance is $99,749.70
Interest paid is iP = 0.005(99749.70) = $498.75
Principle paid is A-iP = 599.55 – 498.75 = $100.80
Ending balance is (1+i)P – A = (1.005)99749.70 – 599.55 = $99,648.90

And the second line will be:

$99,749.70 $498.75 $100.80 $99,648.90

And so on until (i+1)P > A then the payment of A = 599.55 becomes (i+1)P instead of A and the ending balance should become $0.

The first part of the report should look like:

Beginning Bal. Interest paid Principle paid Ending Bal.
$99,850.00 $499.25 $100.30 $99,749.70
$99,749.70 $498.75 $100.80 $99,648.90

and so on.


My source code thus far is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  
     #include <cstdlib>
        #include <cmath>
        #include <iostream>
        #include "Loan_Data.h"
        
        using namespace std;
        
        
        Loan_Data::Loan_Data(double p, double n, double i)
        {
        cout << "Enter the loan amount: $";
        cin >> p;
        cout << "Enter the loan length: ";
        cin >> n;
        cout << "Enter your credit score: ";
        cin >> i;
        
            i = i / 100;
        i = i / 12;
        n = n * 12;
        double Bal = p;
            double A = (p * ((i * pow(1 + i, n)) / (pow(1 + i, n) - 1)));
        
            cout << "A is: " << A << endl;
            cout << "Bal is: " << Bal << endl;
            cout << "i is: " << i << endl;
        }
        void Loan_Data::MakePayment(double pay)
        {
         cout << "Enter payment amount: $";
        cin >> pay;
        
         cout << "Bal is: " << Bal << endl;
            
        Bal = ((i + 1) * Bal) - pay;
            
            cout << "Bal is: " << Bal << endl;
            cout << "Pay is: " << pay << endl;
            cout << "A is: " << A << endl;
                }
        
                void Loan_Data::PrintAmortizationSchedule()
                {
                double iP = (i * Bal);
                double pP = (A - (i*Bal));
                double endingBalance = ((1 + i)*Bal - A);
                double payment2 = (i + 1)*Bal;
                
                while (Bal > 0)
        {
        	cout << "Beginning Bal." << "\t""\t" << cout << "Interest paid" <<               
           "\t""\t" << cout << "Principle paid" << "\t""\t" << cout << "Ending Bal." 
        <<     "\t""\t" "\n";
        
        	if ((i + 1)*Bal > A)
        	{
        		cout << Bal << "\t""\t" << iP << "\t""\t" << pP << "\t\t"   << 
                endingBalance << "\n";
        		endingBalance = Bal;
        	}
        	else
        	{
        	        	cout << Bal << "\t""\t" << iP << "\t""\t" << (payment2 -          
            (i*Bal)) << "\t\t" << ((1 + i)*Bal - payment2) << "\n";		
                endingBalance = Bal;
        	}
                        }
           
        	  cout << "0" << "\t""\t" << "0" << "\t""\t" << "0" << "\t\t" <<           
              "0" <<    
             "\n";
                }
        
                int main(int argc, char *argv[])
                {
                double p;
                double n;
                double i;
                double pay;
                double A; 
                
        
        
        Loan_Data loan1(p,n,i);
            
                loan1.MakePayment(pay);
                loan1.PrintAmortizationSchedule();
                    
                return 0;
        
                }


My header file is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     #ifndef LOAN_DATA_H
        #define	LOAN_DATA_H

        class Loan_Data
        {
        private:
        double  Bal;
        double  n;
        double	i;
        double	A;
        
        public:
        Loan_Data(double p, double n, double i);
        void	MakePayment(double pay);
        void	PrintAmortizationSchedule();
        };

        #endif	/* LOAN_DATA_H */ 


While I've finally managed to get the program to compile, I cannot seem to pass the value of the variable "Bal" from the constructor to the MakePayment function, given the parameters that the instructor specified. As you can probably see from my code, I wrote an output statement before and after I calculated Bal's new value. The beginning value outputted for Bal is essentially zero (as opposed to the 100000 that I would like), and the value afterwards is -525 (the payment that I entered). Is there some way of doing this without passing the as a parameter that I am missing? Any help would be greatly appreciated!

Also, this is my first time posting here, so please forgive any errors in my posting format.

Learn to indent.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Loan_Data::Loan_Data(double p, double n, double i)
{
	cout << "Enter the loan amount: $";
	cin >> p;
	cout << "Enter the loan length: ";
	cin >> n;
	cout << "Enter your credit score: ";
	cin >> i;

	i = i / 100;
	i = i / 12;
	n = n * 12;
	double Bal = p; //you are declaring a local variable here
	double A = (p * ((i * pow(1 + i, n)) / (pow(1 + i, n) - 1)));

	cout << "A is: " << A << endl;
	cout << "Bal is: " << Bal << endl;
	cout << "i is: " << i << endl;
}
Variables declared inside a function only exist there. It doesn't matter that it has the same name as other variable, it is other variable.

To refer to your member variable you may use this->Bal = p; or simply Bal = p;(not declaring anything)
Thank you! That fixed my issue with "Bal". I did notice, however, that I am having a similar issue with "i" when I use it in the MakePayment() function. I wrote an output statement to see what it's value was, and it is also very close to 0 (instead of the value calculated in the constructor). I can't see any areas that I have declared it in, other than the class header.

I would appreciate any advice that you could offer!
Last edited on
First of all `i' is a terrible name for a variable important enough to be consider a member.

Loan_Data::Loan_Data(double p, double n, double i /*here*/)
you are declaring it as a parameter of the function, then you refer to the parameter instead of the member variable.

¿Why do you even ask for parameters? You don't use their values, because the first thing that you do is overwrite them with the user input.
1
2
Loan_Data loan1(6, 9, 42);
Loan_Data loan2(3.14, 2.79, 1.61);
there is no real difference between those two object as their state would depend on the user input.
Topic archived. No new replies allowed.