So this is what i need to do and its not coming out right in my code. i need 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.

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; 

}

By the way the object of my BankAccount Class is Checking.

Recommended Answers

All 15 Replies

This function is not part of the account class.

Where is the function, I don't see it? It should look similar to this:

int foo(BankAccount& obj, double target)
{


}

I didn't do it I wasn't sure how to do it. do I actually write foo. im sorry if it's a stupid question im new to C++ and still learning. would I have to declare a variable name double target? and within the curly braces i put the formula for calculation the year?

do I actually write foo

No. foo is just a name commonly used to indicate any function name. You will often see

int foo()
int bar()

You will want to replace those names with something more meaningful to your program.

would I have to declare a variable name double target?

You can name it anything you want, but make the name meaningful for what the variable represents. In this case targetValue

and within the curly braces i put the formula for calculation the year?

Yes

thanks...okay so i did it like this

int main ()

{
    int year;
    double getB;
    double getIR;
    double B = 0;
    double IR =0;
    double IRate;
    double TargetValue;


    BankAccount Checking;


    int year(BankAccount& Checking, double TargetValue)
    {
        year = ((TargetValue/B-1)/IR);

    };

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



    cout << "Your initial account will take to grow to get interest" << endl; 

}

but i'm getting an error saying redefinition of "year" as a different kind of symbol. i also want it to return ho long will it take for the balance to reach the target value for interest to be added.

Are you coming from a Pascal like language?
Line 18 of your code should read

return (TargetValue/B-1)/IR;

Because your function returns an integer, your calculation should be rounded.

line 16-20. You can not declare a function inside another function.

int year(BankAccount& Checking, double TargetValue)
{
        year = ((TargetValue/B-1)/IR);

};


int main ()

{
    int year;
    double getB;
    double getIR;
    double B = 0;
    double IR =0;
    double IRate;
    double TargetValue;


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



    cout << "Your initial account will take to grow to get interest" << endl; 

}

no this is actually my first language. so i had to change some stuff up cause i kept getting 3 errors saying
-'IRate' : redefinition; previous definition was 'data variable'
-see declaration of 'IRate'
-'return' : conversion from 'double' to 'int', possible loss of data

this is the code now

int main ()

{   
    int year;
    double getB;
    double getIR; 
    double B = 0;
    double IR =0;
    double IRate;
    double Target;


BankAccount Checking; 

 double IRate(BankAccount& Checking, double target);
{
    IR = ((Target/B-1)/IR);
    return (Target/B-1)/IR;
}


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



}

You're still attempting to declare a function inside another function. You can't do that! Move lines 15-19 outside main() as I showed you in my first post, then remove the semicolon at the end of line 15.

I did but now its saying those variables are undeclared when i move it.

You must declare the variables inside the function in which they are used. You can't declare them in main() then expect to use them in other functions, doesn't work like that.

 double IRate(BankAccount& Checking, double target)
{
    double IR;
    double B; // you need to initialize this to something

    IR = ((Target/B-1)/IR);
    return (Target/B-1)/IR;
}

I did it just like you said and i'm getting these errors:
1. getB, IR, year, IRate is unreference local variable.
2. B,IR,Target undeclared identifier.

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


 double IRate(BankAccount& Checking, double target)
{

    int year;
    double getB;
    double getIR; 
    double B=0;
    double IR =0;
    double IRate;
    double Target;

    IR = ((Target/B-1)/IR);
    return (Target/B-1)/IR;
}


int main ()

{       


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?"<<Checking.addInterest()<<endl;
cin >>Target;



}

Check spelling (capitalization is important, "target" is not the same as "Target")

Delete unused variables.

thank you.. so i got it to run but its outputting the same results. meaning if the user enter diff numbers for balance, interest rate and target it only output 1 for how long it will take for interest to be added to their balance. it should output diff # of years depending how the numbers they choose but it doesn't. do i need a if else statement for it or do, while or for loop?

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




};





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;


double IRate(BankAccount& Checking, double Target)
{


    double B=0;
    double IR =0;
    double IRate;


    IRate = ((Target/B-1)/IR);
    return (Target/B-1)/IR;
}


int main ()

{

    BankAccount Checking;
    int UserInput;


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

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

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

    cout<< "Your Balance will take"<<&IRate<< "year to reach your Target Value"<< endl;

    return 0;
}

Function IRate() needs a loop in order to find out how many years it takes to reach the target value. Before doing this with your program, sit down and do it by hand with pencil & paper. Start out with an initial balance and interest rate. Then with pen & paper figure out how long it will take for the balance to reach the desired target amount. Once you figure out how to do it manually you should be able to code the function to simulate the method you used to do it manually. Just remember, you need a loop to do this, even when you do it manually.

thanks a lot i finaly got it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.