Programming issue

So this is what i need to do and its not coming out right in my code. A function to calculate how long the balance will take to grow to a given value. This function is not part of the account class. It should take two arguments: an object of the account class and a target value. It should return how long should pass until the balance on the account object reaches the target value.
The account object must be a const reference parameter so that inside the function, you call the function to add interest to the balance as many times as needed without actually modifying the object.

I'm not getting how to make the object a const reference parameter.
This is my code. at the end i put a formula im not sure if i did it right. also whenever the program runs there is a zero next to balance and interest when i ask the user to enter there intial balance and interest.

By the way the object of my BankAccount Class is Checking

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  
class BankAccount
{
private:
    double Balance;
    double InterestRate;
    
public:
	BankAccount();
	BankAccount(double B, double IR){Balance=B; InterestRate=IR;}
	
  
    void setB(double newB);
    double getB() const;
    void setIR(double IR);
    double getIR() const;
    void Deposit(double amount);
    void Withdraw(double amount);
    void addInterest(int years);
    double addInterest();
	

    
};


BankAccount::BankAccount()

{
    Balance=0;
	InterestRate=0;
}


void BankAccount::setB(double newBalance)
{
    Balance = newBalance;
}

void BankAccount::setIR(double newInterestRate)
{
    InterestRate = newInterestRate;
}

double BankAccount::getB() const
{
    return Balance;
}
double BankAccount::getIR() const
{
    return InterestRate;
}

void BankAccount::Deposit(double amount)
{
    Balance = Balance + amount;
}

void BankAccount::Withdraw(double amount)
{
    Balance = Balance - amount;
}




#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;

int main ()

{   
	int year;
	double getB;
	double getIR; 
	double B = 0;
	double IR =0;
	double IRate;
	double TV=0;
	
 
BankAccount Checking; 


cout<<"Welcome to your BankAccount"<<endl;
cout<<" what is your initial Balance? " <<Checking.getB()<<endl;
cin>> B;

cout<<" what is your initial Interest Rate? " <<Checking.getIR()<<endl;
cin>> IR;

cout<<" what is your target Value?"<<endl;
cin >>TV;


{
	
	year = ((TV/B-1)/IR);
	
}

cout << "Your initial account will take to grow to get interest" << endl; 
		
}
closed account (o3hC5Di1)
Hi there,

You will have to declare a function as follows:

int calculate_growth_span(const BankAccount& account, double target)

That's how you pass a const reference. Const means that within the function the object will not be altered. Passing by reference makes sure that you only pass the address of the existing object to the function, as opposed to a copy of the original. That means that if you didn't declare this parameter const, you would be altering the exact same object which you passed in.

I'll leave the implementation of the function up to you, but do not hesitate to ask if you require any further help.

All the best,
NwN
Topic archived. No new replies allowed.