Bank account with classes won't store new balance

I'm writing a bank account program using classes and I'm running into a problem. Everything compiles and runs but the account won't store the balance that a user enters for withdraw and deposit. When I show the list of accounts and their balance, it still shows the initial balance that was read in through a file. Here's the code :

Main.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <fstream>
#include <string>
#include "Bank.h"
using namespace std;
//----------------------------Function Prototypes---------------------------------//
void BankMenu();
void ReadAccounts(Bank &);
void PrintAccounts(Bank &, ofstream &);
void ViewBalance(Bank &);
void Deposit(Bank &, ofstream &);
//--------------------------------------------------------------------------------//
int main()
{
   /*ofstream output("C:\\Users\\Home\\Documents\\Programming Work\\testing\\IOfiles\\"
                "output.txt");   */
   ofstream output("con");
   output.setf(ios::fixed, ios::floatfield); output.precision(2);

   char choice;
   bool not_done = true;
   Bank MaqBank;

   ReadAccounts(MaqBank);

   do{
      BankMenu();
      cin >> choice;
      switch(choice)
      {
         case 'Q':   // Quit
            // Print database before quitting the program
            not_done = false;
            break;
         case 'L':   // List accounts/Database
            PrintAccounts(MaqBank, output);
            break;
         case 'I':   // Account Info
            break;
         case 'D':   // Deposit
            Deposit(MaqBank, output);
            break;
         case 'W':   // Withdraw
            break;
         case 'B':   // Balance
            ViewBalance(MaqBank);
            break;
         case 'N':   // New Account
            break;
         case 'X':   // Delete Account
            break;
         default:    // Triggers if user enters a choice other than above
            cout << choice << " is not valid choice. Please make another "
               "selection" << endl << endl;
            break;
      }
   }while(not_done);

   output.close();

   return 0;
}
//--------------------------------------------------------------------------------//
void ReadAccounts(Bank &MaqBank)
{
   Account acct;
   Depositor dep;
   Name name;
   string First, Last, SSN;
   string Type, AcctNum;
   double bal;

   ifstream input("C:\\Users\\Home\\Documents\\Programming Work\\"
                  "HW3-Bank_With_Classes\\IOFiles\\InputFile.txt");
   while(input >> Last)
   {
      input >> First;
      input >> SSN;
      input >> Type;
      input >> AcctNum;
      input >> bal;

      name.setFirstName(First);
      name.setLastName(Last);
      dep.setName(name);
      dep.setSocial(SSN);

      acct.setDepositor(dep);
      acct.setAccountType(Type);
      acct.setAccountNumber(AcctNum);
      acct.setInitialBalance(bal);

      MaqBank.addAccount(acct);
   }
   input.close();
}
//--------------------------------------------------------------------------------//
void PrintAccounts(Bank &MaqBank, ofstream &output)
{
   Account Acct;
   Depositor Dep;
   Name Name;
   string First, Last, SSN;
   string Type, AcctNum;
   double Bal;

   output << "-------------------------------------Account Database----------------"
            "-------------------" << endl;
   output.width(57); output << right << "Current Number of Accounts: " <<
                                          MaqBank.getNumAccounts() << endl;
   output << "---------------------------------------------------------------------"
            "-------------------" << endl;
   output.width(14); output << left << "First";
   output.width(13); output << left << "Last";
   output.width(18); output << left << "Social Security";
   output.width(14); output << left << "Account Type";
   output.width(22); output << left << "Account Number";
   output << "Balance" << endl;
   output << "_____________________________________________________________________"
            "___________________" << endl;

   for(int count = 0; count < MaqBank.getNumAccounts(); count++)
   {
      Acct = MaqBank.getAccount(count);
      Type = Acct.getAccountType();
      AcctNum = Acct.getAccountNumber();
      Bal = Acct.getAccountBalance();
      Dep = Acct.getAccountDepositor();
      Name = Dep.getName();
      First = Name.getFirstName();
      Last = Name.getLastName();
      SSN = Dep.getSocial();

      output.width(14); output << left << First;
      output.width(15); output << left << Last;
      output.width(18); output << left << SSN;
      output.width(16); output << left << Type;
      output.width(10); output << left << AcctNum;
      output.width(15); output << right << Bal << endl;

      output << "------------------------------------------------------------------"
            "----------------------" << endl << endl;
   }
}
//--------------------------------------------------------------------------------//
void Deposit(Bank &MaqBank, ofstream &output)
{
   Account Acct;
   int Index;
   string AcctNum;
   double amount;

   cout << "Enter the account you wish to deposit into: "; cin >> AcctNum;
   Index = MaqBank.findAccount(AcctNum);
   if(Index == -1)
   {
      output << "Transaction requested: Deposit" << endl;
      output << "Account requested: " << AcctNum << endl;
      output << "Error - account not found. Returning to main menu." << endl;
      return;
   }
   else
   {
      cout << "Enter the amount you wish to deposit: $"; cin >> amount;
      if(amount > 0)
      {
         Acct = MaqBank.getAccount(Index);
         Acct.makeDeposit(amount);
      }
      else
      {
         int tries = 0;
         do
         {
            if(amount <= 0)
               cout << "Error - invalid deposit amount. Please enter another: $";
            else if(amount > 0)
            {
               Acct = MaqBank.getAccount(Index);
               Acct.makeDeposit(amount);
               return;
            }
            tries++;
            cin >> amount;
         }while(tries != 2 || tries == -1);
      }
   }
}
//--------------------------------------------------------------------------------// 


Account.h
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
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "Depositor.h"

class Account
{
   private:
      string AccountNumber;
      string AccountType;
      double AccountBalance;
      Depositor AccountDepositor;
   public:
      //Setter methods
      void setAccountNumber(string AcctNum);
      void setAccountType(string AcctType);
      void setInitialBalance(double Bal);
      void setDepositor(Depositor dep);

      //Getter methods
      string getAccountNumber() const;
      string getAccountType() const;
      double getAccountBalance() const;
      Depositor getAccountDepositor() const;

      //Deposit and Withdraw method
      void makeDeposit(double D_Amount);
      void makeWithdrawal(double W_Amount);
};

#endif 


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
#include "Account.h"
//-----------------------Setters--------------------------------------------------//
void Account::setAccountNumber(string AcctNum)
{
   AccountNumber = AcctNum;
}

void Account::setAccountType(string AcctType)
{
   AccountType = AcctType;
}

void Account::setInitialBalance(double bal)
{
   AccountBalance = bal;
}

void Account::setDepositor(Depositor dep)
{
   AccountDepositor = dep;
}
//-----------------------Getters--------------------------------------------------//
string Account::getAccountNumber() const
{
   return(AccountNumber);
}

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

double Account::getAccountBalance() const
{
   return(AccountBalance);
}

Depositor Account::getAccountDepositor() const
{
   return(AccountDepositor);
}
//---------------------Deposit/Withdraw method------------------------------------//
void Account::makeDeposit(double D_Amount)
{
   AccountBalance += D_Amount;
}

//void Account::makeWithdrawal(double W_amount)
//{
//   AccountBalance -= W_amount;
//} 
Last edited on
It looks like when you deposit, you deposit into a temporary account that goes out of scope when the function ends. From brief inspection, I believe if you change
1
2
 Acct = MaqBank.getAccount(Index);
         Acct.makeDeposit(amount);


to something like this
MaqBank.getAccount(Index).makeDeposit(amount);

You should see the correct output.
that didn't help with the initial problem, but it did help with a different issue. Thanks anyway!
Topic archived. No new replies allowed.