Creating a Banking Application That Needs To Store Multiple User Accounts

I've been trying to figure this out for weeks now. My program will take in one user's information and display it using that display function. However, when I create another user, the previous user is overwritten. I'm aware I need to implement an array but I am at a complete loss as to where and how to do so.

My code is a follows:

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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426

Source.cpp

#include <iostream>
#include <string>
#include "BankAccount.h"


using namespace std;

int Menu()
{
	int i;
	cout << ("\n\n MENU \n\n");
	cout << ("1. Create An Account \n");
	cout << ("2. Display An Account \n");
	cout << ("3. Withdraw Funds Into An Account \n");
	cout << ("4. Deposit Funds From An Account \n");
	cout << ("5. Take Out A Loan \n");
	cout << ("6. Calculate Interest \n");
	cout << ("7. Take Out An Overdraft \n");
	cout << ("8. Exit \n\n");
	cout << ("Enter Your Choice Here: ");
	cin >> i;
	cin.clear();
	cin.ignore(INT_MAX, '\n');
	return i;
}

int main()
{
	
	
	BankAccount ca; //Create Account 
	/*BankAccount da; //Display Account
	BankAccount w; //Withdraw Funds
	BankAccount d; //Deposit Funds
	BankAccount l; //Take Out Loan
	BankAccount i; //Interest
	BankAccount o; //Overdraft*/

	int end = 0;
	while (end != 1)
	{
	
		int op;
		system("CLS");
		op = Menu();
		switch (op)
		{
		
		case 1:
			
			ca.CreateAccount(); //Loads the CreateAccount function

			break;
		
		case 2:
			
			ca.DisplayAccount(); //Loads the DisplayAccount function

			break;
		
		case 3:
			
			ca.Withdraw(); //Loads the Withdraw function

			break;
		
		case 4:
			
			ca.Deposit(); //Loads the Deposit function

			break;

		case 5:

			ca.Loan(); //Loads the Loan Function

			break;

		case 6:

			ca.Interest(); //Loads the Interest Function

			break;

		case 7:

			ca.Overdraft(); //Loads the Overdraft Function

			break;


		case 8: // Exits The Program
			end = 1;
			break;
		
		default:
			cout << ("Please Enter a Valid Option.\n\n");
			system("PAUSE");
			break;
		}
	}
	return 0;
}










BankAccount.cpp

#include <iostream>
#include "BankAccount.h"
using namespace std;


BankAccount account;


BankAccount::BankAccount()
{
}


BankAccount::~BankAccount()
{
}


	void BankAccount::CreateAccount() // Used to create a new account for a customer
	{

		cout << "Please Enter The Name of The Account Holder: ";
		getline(cin, account.AccountHolder);

		cout << "Please Enter Your Account Number: "; 
		cin >> account.AccountID;

		cout << "Please Enter Your Initial Balance: ";
		cin >> account.AccountBalance;

		cout << "\nYou Have Successfully Created An Account. "; 
		system("PAUSE");

	}





	void BankAccount::DisplayAccount() // Used to display a customers account

	{

		int AccountID;
		cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
		cin >> AccountID;
			
		if (AccountID == account.AccountID)
			{

				cout << "The Account Holder is: " << account.AccountHolder << endl;
				cout << "The Account Number is: " << account.AccountID << endl;
				cout << "The Account's Balance is: " << account.AccountBalance << endl;
				system("PAUSE");

			}

				else if (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
				{

					cout << "Please Enter A Valid Account Number."<< endl;
					system("PAUSE");

				}

	}





	void BankAccount::Withdraw() // Allows The User To Withdraw Funds From Their Account
	{

		int AccountID;
		double Withdraw;
		double NewBalance;

		cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
		cin >> AccountID;
	
			if (AccountID == account.AccountID)
			{

				cout << "The Account Holder is: " << account.AccountHolder << endl;
				cout << "The Account Number is: " << account.AccountID << endl;
				cout << "The Account's Balance is: " << account.AccountBalance << endl;
				cout << "Please Enter The Amount You Wish To Withdraw: " ;
		
				cin >> Withdraw;

				NewBalance = account.AccountBalance - Withdraw;
				account.AccountBalance = NewBalance;
				cout << "Your New Balance Is: " << account.AccountBalance << endl;
				system("PAUSE");

			}


				else if (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
				{

					cout << "Please Enter A Valid Account Number." << endl;
					system("PAUSE");

				}
	}





	void BankAccount::Deposit() // Allows The User To Deposit Funds To Their Account
	{

		int AccountID;
		double Deposit;
		double NewBalance;
		cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
		cin >> AccountID;
	
			if (AccountID == account.AccountID)
			{

				cout << "The Account Holder is: " << account.AccountHolder << endl;
				cout << "The Account Number is: " << account.AccountID << endl;
				cout << "The Account's Balance is: " << account.AccountBalance << endl;
				cout << "Please Enter The Amount You Wish To Deposit: ";

				cin >> Deposit;

				NewBalance = account.AccountBalance + Deposit;
				account.AccountBalance = NewBalance;
				cout << "Your New Balance Is: " << account.AccountBalance << endl;
				system("PAUSE");


			}

				else if (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
				{

					cout << "Please Enter A Valid Account Number." << endl;
					system("PAUSE");

				}

	}





	void BankAccount::Loan() //Allow The User To Check Their Eligibility For A Loan And Take One Out If They Are.
	{

		double Loan;
		double NewBalance;
		cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
		cin >> AccountID;

		if (AccountID == account.AccountID)
		{


			cout << "The Account Holder is: " << account.AccountHolder << endl;
			cout << "The Account Number is: " << account.AccountID << endl;
			cout << "The Account's Balance is: " << account.AccountBalance << endl;
			cout << "Please Enter The Amount You Wish To Take Out As A Loan: ";

			cin >> Loan;

			if (Loan > account.AccountBalance * 2)
			{

				cout << "You Are Not Eligible For A Loan Of This Size.\nPlease Enter A Value No More Than Double Your Current Balance"<< endl;
				system("PAUSE");

			}
			else if (Loan)
			{
				NewBalance = Loan + account.AccountBalance;
				account.AccountBalance = NewBalance;
				cout << "You Have Successfully Taken Out A Loan.\nYour New Balance Is: " << account.AccountBalance << endl;
				system("PAUSE");
			}
			

		}


				else if (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
				{

					cout << "Please Enter A Valid Account Number." << endl;
					system("PAUSE");

				}

	}




	void BankAccount::Interest()
	{
		
		double Interest;
		double NewBalance;

		cout << "Please Enter The Account Number Associated With The Account You Wish To Access: ";
		cin >> AccountID;

		if (AccountID == account.AccountID)
		
		{


			cout << "The Account Holder is: " << account.AccountHolder << endl;
			cout << "The Account Number is: " << account.AccountID << endl;
			cout << "The Account's Balance is: " << account.AccountBalance << endl;

			Interest = account.AccountBalance * 0.03;
			
			cout << "Your interest rate is 3%. You have earned a total of: " << Interest;
			NewBalance = account.AccountBalance + Interest;

			account.AccountBalance = NewBalance;

			cout << "\nYour New Balance Is : " << account.AccountBalance << endl;

			system("PAUSE");

		}


		else if (AccountID != BankAccount::AccountID) // To Flag An Invalid Account Number Being Entered
		
		{

			cout << "Please Enter A Valid Account Number." << endl;
			system("PAUSE");

		}
		

	}


	void BankAccount::Overdraft()
	{
	
	//Incomplete


	
	}










BankAccount.h

#pragma once
#include <string>

using namespace std;

class BankAccount
	
	{

		public:
			BankAccount();
			~BankAccount();

		//Variables



		private:
			
			string AccountHolder;
			double AccountBalance = 00.00;
			int AccountID;


				public: 
	
					void CreateAccount();
					void Withdraw();
					void Deposit();
					void DisplayAccount();
					void Loan();
					void Interest();
					void Overdraft();

	};







Any and all help is greatly appreciated.
You're right you need an array.
1
2
3
4
//  At line 34
const int MAX_ACCOUNTS = 10;
BankAccount ca[MAX_ACCOUNTS];
int num_accounts = 0;

Now you need to manage the array of accounts. Add an account to the array, find an account in the array, etc.

I don't know if you've learned std::vector yet. If you have, that is a much safer alternative to a raw array.
Last edited on
Unfortunately I've had no experience with vectors yet.

I am still a bit confused on how I can make the array function properly, i.e. how to pull out the account I wish to display once it's stored.
You refer to an element of an array by using a subscript.

Assuming the snippet I provided above:
1
2
3
    //  We start with num_accounts = 0
    ca[num_accounts].CreateAccount ();   // Create the first account
    num_accounts++;   // Increment the number of accounts 



I'm sorry but I am struggling to follow how and where I implement this stuff. My apologies but my c++ knowledge is very basic as it stands.
This should get you started:
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
// Function prototype.  You need to provide
BankAccount * FindAccount (BankAccount accounts[], int acct_id, int num_accts);  // return ptr to desired acct

int main()
{   const int MAX_ACCOUNTS = 10;
	BankAccount accounts[MAX_ACCOUNTS];
	int num_accounts = 0;
	BankAccount *   curr;           //  Pointer to current account	
	int end = 0;
	int acct_id;
	
	while (end != 1)
	{   int op;
		system("CLS");
		op = Menu();
		switch (op)
		{		
		case 1:			
		    curr = &accounts[num_accounts];  // point to next available accout
			curr->CreateAccount();        
			num_accounts++;                 //  Increment the number of accouts
			break;
		
		case 2:
	        cout << "Enter the account Id of the account you want to display";
	        cin >> acct_id;	
	        curr = FindAccount (accounts, acct_id, num_accounts);
	        if (curr == nullptr)
	        {   cout << "Account not found" << endl;
	        }
	        else
	        {   curr->DisplayAccount(); 
                }			    
		break;


Edit: Added num_accounts as an argument to FindAccount().

Edit #2: Fixed missing & on line 19.
Last edited on
Thank you for the help. Unfortunately I'm still at a loss. I've tried implementing this and it's not working at all now. Due to my own lack of knowing how to use it. Is there any more help you can give me?
You're going to have to ask specific questions about what you don't understand.
I'm not going to write your program for you.

Do you understand how to use pointers?
Do you understand what FindAccount() needs to do?

Note: I edited the post above to add num_accounts as an argument to FindAccount().
Last edited on
Unfrotunately not.

Is FindAccount() a function I need to create in the BankAccount.cpp?

I have had no experience with using pointers either.

On the line underneath case 1: "curr = accounts[num_accounts];"

Accounts is underlined red, any clue why this is?

Again I really do appreciate the help!
Last edited on
Is FindAccount() a function I need to create in the BankAccount.cpp?

Yes, you need to create it. It should be in source.cpp since it is not a member function of BankAccount. It should loop through the accounts that have been created and look for a match on acct_id. When a matching account is found, it should return the address of that account. If not match is found, it should return nullptr.

On the line underneath case 1: "curr = accounts[num_accounts];"
Accounts is underlined red, any clue why this is?

Sorry. My mistake. It should have been:
 
curr = &accounts[num_accounts];  //  Note the &.  We need the address of the account 


1
2
3
4
5
6
7
BankAccount * FindAccount (BankAccount accts[], int acct_id, int num_accts)
{   for (int i=0; i<num_accts; i++)
    {   if (accts[i].AccountID == acct_id)
            return &accts[i];  //  Found it.  Return pointer to account
    }
    return nullptr;  // Not found
}


Line 124 of your original post, account should not be a global.

bankaccount.cpp - numerous places - You should not be referring to the global account. A member function implicitly refers to a specific instance.
Last edited on
Topic archived. No new replies allowed.