Need help figuring out why my deposit/withdraw member functions won't work

I wrote a bank account program that uses classes and separate compilation. The heirarchy is as follows:

Class Bank:
Account account;

Class Account:
Private:
double balance;
string type;
Depositor depositor;
Public:
void makeWithdrawal(double);
void makeDeposit(double)

void makeWithdrawal(double amount)
balance -= amount;
// deposit is the same.

now I can't seem to make a withdrawal. The program compiles, and I can type in a value to deposit/withdraw, but the actual deposit and withdrawal never happens.


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
  void withdraw(Bank bank[], int num_accts, ofstream &out)
{
   int acct_requested, index;
   double amount;
   cout << "Transaction requested: Withdrawal" << endl;
   cout << "Please enter the account you with to withdraw from: ";
   cin >> acct_requested;
   index = find_Acct(bank, num_accts, acct_requested);

   if(index == -1)
   {
      out << "Transaction requested: Withdrawal" << endl;
      out << "Account requested: " << acct_requested << endl;
      out << "Error - account not found. Returning to main menu." << endl;
   }
   else
   {
      cout << "Enter the amount you wish to withdraw: $"; cin >> amount;

      if(amount > 0 && amount <= bank[index].getAccount().getBalance())
         bank[index].getAccount().makeWithdrawal(amount);
      else if(amount > bank[index].getAccount().getBalance())
      {
         int tries = 0;
            do
            {
               if(bank[index].getAccount().getBalance())
                  cout << "Error - withdrawal amount exceeds account balance. Enter"
                     " another amount: $";
               else if(bank[index].getAccount().getBalance())
               {
                  bank[index].getAccount().makeWithdrawal(amount);
                  tries = -1;
                  return;
               }
               tries++;
               cin >> amount;
            }while(tries != 2 || tries == -1);
      }
      else
      {
         int tries = 0;
         do
         {
            if(amount <= 0)
               cout << "Error - invalid withdrawal amount. Please enter another:        $";
            else if(amount > 0)
            {
               bank[index].getAccount().makeWithdrawal(amount);
               tries = -1;
               return;
            }
            tries++;
            cin >> amount;
         }while(tries != 2 || tries == -1);
      }
   }
   return;
}


Please help me figure this out.
That you have multiple banks with just one account doesn't seem to be right. Normally you have a bank where you find the account according to a number.

The question is what does getAccount() return here:

bank[index].getAccount().makeWithdrawal(amount);

if it returns a copy only that copy will be changed not the object within the class. So please show the entire getAccount() function.
closed account (48T7M4Gy)
Another way of putting the same as @coder777.

An Account has a Depositor, a balance, etc and has as one method Account::makeWithdrawal( ... )

A Bank has (an array of) Account's. ie Accounts[10] say

The Account comes 'first' in the hierarchy with a Bank being simply a container of Accounts.
So how would I go about calling an account[] from my bank "container" class?

I keep getting errors. Here's the bank.h and bank.cpp

bank.h
1
2
3
4
5
6
7
8
9
10
11
12
13
class Bank
{
   private:
      Account account[25];
   public:
      int openAccount(int, int);
      int closeAccount(int);

      void setAccount(Account []);

      Account getAccount() const;
};

bank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int Bank::openAccount(int num_accts, int MAX_ACCTS)
{

   return 0;
}

int Bank::closeAccount(int num_accts)
{

   return 0;
}

void Bank::setAccount(Account acct[])
{
   account[] = acct;
}

Account Bank::getAccount() const
{
   return(account);
}
You have some design problems in your Bank class.
1) Wouldn't it be a good idea to keep track of how many accounts there are?
2) openAcount requires number of accounts and MAX_ACCTS as arguments. These should be variables private to the Bank class.
3) Ditto for closeAccount. BTW, which account are you trying to close?
4) setAccount() requires an array of accounts to be passed in. Why are you maintaining accounts in two places? How do you know how many accounts are in acct[]?
5) getAccount says it returns a single account, but you're retrying to return an arrray of accounts.

I strongly recommend you use std::vector<Account> to maintain your list of accounts.

Last edited on
ignore openAccount and closeAccount for now, those are unrelated and haven't been coded. We havent learned vectors so I cant use those.

Here's the complete hierarchy. What I'm trying to do is manipulate the balance of an account using makeWithdrawal and makeDeposit. An account has a balance, and a bank has a certain number of accounts. How would I manipulate the account balance in main? How do I call an account[] that is within the bank class?

bank.h
1
2
3
4
5
6
7
8
9
10
11
12
13
class Bank
{
   private:
      Account account[25];
   public:
      int openAccount(int, int);
      int closeAccount(int);

      void setAccount(Account []);

      Account getAccount() const;
};

bank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int Bank::openAccount(int num_accts, int MAX_ACCTS)
{

   return 0;
}

int Bank::closeAccount(int num_accts)
{

   return 0;
}

void Bank::setAccount(Account acct[])
{
   account[] = acct;
}

Account Bank::getAccount() const
{
   return(account);
}


Account.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Account
{
   private:
      Depositor depositor;
      int acct_number;
      double balance;
      string acct_type;
   public:
      void makeDeposit(double);
      void makeWithdrawal(double);
      void setAccountNum(int);
      void setAccountType(string);
      void setDepositor(Depositor dep);
      void setInitBalance(double);

      double getBalance() const;
      int getAccountNumber() const;
      string getAccountType() const;
      Depositor getDepositor() const;
};


Account.cpp
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
//-----------------------------Setter methods-------------------------------------//
void Account::makeDeposit(double deposit)
{
   balance += deposit;
}

void Account::makeWithdrawal(double withdraw)
{
   balance -= withdraw;
}

void Account::setAccountNum(int acct_num)
{
   acct_number = acct_num;
}

void Account::setInitBalance(double bal)
{
   balance = bal;
}

void Account::setAccountType(string type)
{
   acct_type = type;
}

void Account::setDepositor(Depositor dep_info)
{
   depositor = dep_info;
}

//------------------------------Getter methods------------------------------------//
double Account::getBalance() const
{
   return(balance);
}

int Account::getAccountNumber() const
{
   return(acct_number);
}

string Account::getAccountType() const
{
   return(acct_type);
}

Depositor Account::getDepositor() const
{
   return(depositor);
}
Last edited on
Topic archived. No new replies allowed.