Using loops with classes and arrays

Hey,

I'm currently working on a banking system project and have created 3 objects representing 3 different customers. I've created a menu for the user to navigate various options on various accounts for the customers. I have way to much code in my int main() and am wondering how I can use a loop to create the customers and their bank account dynamically. My code for one customer is as 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
  		if (input == 1111)
		{
			cout << "Please enter PIN number \t \t (It's 1234)" << endl;
			cin >> pin;
			cout << endl;
			if (pin == 1234)
			{
				cout << "Name: " << Ross.getCustomerName() << endl << "ID: " << Ross.getCustomerID() << endl << "Number of accounts: " << Ross.getNumberOfAccounts() << endl;
				cout << "------------------------------------" << endl;
				cout << "The balance of account #1 is: " << Ross.accounts[0].getAccountBalance();
				cout << "\t Available Funds: " << Ross.accounts[0].getAvailableBalance() << endl;
				cout << "The balance of account #2 is: " << Ross.accounts[1].getAccountBalance();
				cout << "\t Available Funds: " << Ross.accounts[1].getAvailableBalance() << endl;
				cout << "The balance of account #3 is: " << Ross.accounts[2].getAccountBalance();
				cout << "\t Available Funds: " << Ross.accounts[2].getAvailableBalance() << endl;
				cout << "Please select an account: ";
				cin >> input;

				// Entering Ross #1 account to complete transactions
				if (input == 1)
				{
					cout << endl << "Please enter 1, 2, 3, 4 or 5:" << endl;
					cout << "1 - Withdrawl" << endl << "2 - Deposit" << endl << "3 - Loan" << endl << "4 - Overdraft" << endl << "5 - Interest" << endl;
					cin >> input;
					// Making withdrawl from Ross #1 account
					if (input == 1)
					{
						// makes the withdrawl
					}
					// Making deposit to Ross #1 account
					else if (input == 2)
					{
						// makes a deposit
					}
					// Applying for a loan on Ross #1 account
					else if (input == 3)
					{
						// applying for the loan
					}
					// Applying for an overdraft on Ross #1 account
					else if (input == 4)
					{
						// Applying for an overdraft
					}
					// Calculating the interest rate
					else if (input == 5)
					{
						// Calculates the interest
					}
				}


So having this for multiple accounts for multiple customers in the main is inefficient. I know using loops is the way to fix this but i'm unsure how to assign the balance values etc to each customer when creating them.

Any help appreciated.

edit: took away unnecessary code
Last edited on
I hope i'm correct at what you're asking for
Or if you were to have more customers, use structure: http://www.cplusplus.com/doc/tutorial/structures/
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
#include <iostream>
using namespace std;
int umm(int balance)
{
	cout<<"your balance is "<<balance;
//here you can add more options for the customer to explore
	return 0;
}

int main() 
{
	int pin1, pin2, pin3, balance1, balance2, balance3, num;
	pin1=1234;
	pin2=0000;
	pin3=4321;
	balance1=10000;
	balance2=100000;
	balance3=9990;
	cin>>num;
	if(num==pin1)
	{
		umm(balance1);
	}
	if(num==pin2)
	{
		umm(balance2);
	}
	if(num==pin3)
	{
		umm(balance3);
	}
	return 0;
}
Last edited on
Split your code up into functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int get_pin( )
{
    int pin{};
    cout << "Please enter PIN number \t \t (It's 1234)" << endl;
    cin >> pin;
    cout << endl;

    return pin;
}

void display_info( const <class name>& Ross )
{
    cout << "Name: " << Ross.getCustomerName() << endl << "ID: " << Ross.getCustomerID() << endl << "Number of accounts: " << Ross.getNumberOfAccounts() << endl;
    cout << "------------------------------------" << endl;
    cout << "The balance of account #1 is: " << Ross.accounts[0].getAccountBalance();
    cout << "\t Available Funds: " << Ross.accounts[0].getAvailableBalance() << endl;
    cout << "The balance of account #2 is: " << Ross.accounts[1].getAccountBalance();
    cout << "\t Available Funds: " << Ross.accounts[1].getAvailableBalance() << endl;
    cout << "The balance of account #3 is: " << Ross.accounts[2].getAccountBalance();
    cout << "\t Available Funds: " << Ross.accounts[2].getAvailableBalance() << endl;
}


Also, consider using a switch statement for line 26 - 49.
Thanks for the help guys!
Ok so when i'm trying to separate them into functions to display the balances, I have 3 customers. Is there anyway to pass through each customers balances to the function or do i need to create 3 different functions to display each customer?

For instance the above example is displaying one customer but what if i wanted to display <class name> Michael and transact on his accounts?
For instance the above example is displaying one customer but what if i wanted to display <class name> Michael and transact on his accounts?

You just pass Michael to the function like so: display_info( Michael );. It would be better to rename the parameter Ross to something like customer, I just named it Ross because I assumed you were working on one customer.
aha that has helped a lot thanks.

I do have a slight problem when I am doing this for other functions such as withdrawing from the account

1
2
3
4
5
6
7
8
9
10
void withdraw(Customers Cust, int AccNo)
{
	cout << "How much would you like to withdrawl?: ";
	cin >> input; cout << endl;
	accountBalance = Cust.accounts[AccNo].getAccountBalance();
	availableBalance = Cust.accounts[AccNo].getAvailableBalance();
	Cust.accounts[AccNo].setAccountBalance(Calculate.makeWithdrawl(accountBalance, input));
	Cust.accounts[AccNo].setAvailableBalance(Calculate.makeWithdrawlOverdraft(availableBalance, input));
	cout << "New balance: " << Calculate.makeWithdrawl(accountBalance, input) << endl;
}


I am required to use getters and setters to store the balance of the accounts and when this function is called it does correctly display the balance after the withdrawl however it does not store the result after the program loops back to the start.

The makeWithdrawl(), setAccountBalance() and getAccountBalance() functions are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Calculations::makeWithdrawl(int accountBalance, int Input)
{
	int sum = accountBalance - Input;
	return sum;
}

void BankAccounts::setAccountBalance(int x)
{
	balance = x;
}

int BankAccounts::getAccountBalance()
{
	return balance;
}


the available ones are pretty much the same except for the 'available' balance. Any ideas what i'm getting wrong?
1
2
int sum = accountBalance - Input;
return sum;


It should be :
1
2
int sum = (accountBalance -= Input);
return sum;
void withdraw(Customers Cust, int AccNo)
Your Customer object is passed by value, so any changes made within the function won't affect the original variable.

You'll need to pass by reference to modify the original variable.
void withdraw(Customers& Cust, int AccNo)
Topic archived. No new replies allowed.