0

I am creating a program which includes a parent class Account and a derived class BankAccount. Initially, I have to set the balance in account to be 5000. And when certain transactions are made, this balance should be updated. Also, whenever the program is closed, the program should return the latest balance. Below is my code. However, I am not able to initialize the constructor properly and hence, initial balance is never set correctly. Please tell me what I am doing wrong.

 class Account
{
public:


    void setCashBal(); //updates the cash balance in account whenever a transaction is made
    double getCashBal(); //retrieves recent cash balance


protected:
    double cash_balance;
};


void Account::setCashBal()
{

    double initial_bal;


    ifstream read_cash_file("cash_bal_file.txt", ios_base::in);

    int count = 0;

    if (read_cash_file.is_open()) //check whether the file could be openend
    {

        while (!(read_cash_file.eof())) //reading until the end of file
        {
            count = count + 1;
            break;
        }
        read_cash_file.close();

        if (count == 0)//if the file is opened the first time, initial cash balance is 5000
        {
            initial_bal = 5000;

            ofstream write_cash_file("cash_bal_file.txt", ios::out);


            write_cash_file << initial_bal; //writing the initial value of cash balance in cash_bal_file
            write_cash_file.close();

            read_cash_file.open("cash_bal_file.txt", ios_base::in); 
            read_cash_file >> cash_balance;
            read_cash_file.close();
        }


        else //getting the latest cash balance
        {
            read_cash_file.open("cash_bal_file.txt", ios::in);
            read_cash_file >> cash_balance;
            read_cash_file.close();
        }
    }

    else //case when the file could not be openend
        cout << endl << "Sorry, cash_bal_file could not be opened." << endl;
}

double Account::getCashBal()
{
    return cash_balance;
}


    class BankAccount :public Account
    {
    public:
        BankAccount();
        void viewBalance();
    };

    BankAccount::BankAccount()

    {
        setCashBal();
        cash_balance = getCashBal();
    }


    void BankAccount::viewBalance()
    {

        double balance;
        ifstream view_bal("cash_bal_file.txt", ios_base::in); //getting the latest cash_balance from cash_bal_file.
        view_bal >> balance;


        cout << endl << "The balance in your bank account is " << balance << endl;
    }


    int main()
    {
        BankAccount bnkObj;
        bnkObj.viewBalance();

    return 0;
    }
  • Don't use !.eof() it is really bad – Jake Freeman Dec 05 '17 at 00:35
  • string name;while(read_cash_file>>name){} would do? – Resting Platypus Dec 05 '17 at 00:37
  • in this case use `string name; getline(read_cash_file, name);`. Also you need a constructor otherwise it will default to 0 – Jake Freeman Dec 05 '17 at 00:39
  • You haven't implemented `Account::getCashBal()` – Disillusioned Dec 05 '17 at 00:41
  • @CraigYoung I am sorry I forgot to add that. Have edited the code now. – Resting Platypus Dec 05 '17 at 00:42
  • 2
    When you try to implement something new, do it *in isolation*. You want the constructor to set the initial value of a member variable, so work on that; don't try to get it working in the middle of all this other code. – Beta Dec 05 '17 at 00:44
  • @Beta That's a part of my project. Initializing the member variable can be done, but how to initialize it in this way is the question. – Resting Platypus Dec 05 '17 at 00:47
  • Expanding on @JakeFreeman 's comment: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Dec 05 '17 at 00:50
  • @RestingPlatypus Perhaps next time you'll make sure you provide a complete question. But take the time to think about what you're doing. `getCashBal` returns `cash_balance`. So the line `cash_balance = getCashBal();` is the same as `cash_balance = cash_balance;`. I.e. you're setting the member ***to exactly the same value*** it had before. – Disillusioned Dec 05 '17 at 01:02

1 Answers1

0
class Account
{
  public:
    int n;

    Account(): n(5000)
    {}
};

This is called "initializing a member variable in the constructor", or just "initializing". If you search an introductory C++ text for "initialize", you'll find something like this.

Beta
  • 86,746
  • 10
  • 132
  • 141
  • I did this. But then, every time the program is run, balance would be set to 5000. The program should return the latest balance after certain deposits or withdrawals. – Resting Platypus Dec 05 '17 at 00:56
  • @RestingPlatypus: [rubs temples] This is how you get the constructor to initialize a variable, which is what you asked for help with. If your assignment calls for other things in addition to this, then you will have to write some code yourself. Or [hire a programmer](https://stackoverflow.com/jobs). – Beta Dec 05 '17 at 00:59
  • 1
    "every time the program is run" ... suggests that your program should perform a 'persistent store' and save the values to a file for later reload. Every time you restart your program, the first thing it does is load from the persistent store and use those number to initialize your data. Between loading and saving you perform customer requested actions (deposit, withdraw, view balance, etc.) – 2785528 Dec 05 '17 at 02:40