Using arrays and loops(?) to write more elegant code

Hey guys,

Currently i'm just tidying up some of my programs code.

What i'm trying to do is find a way so that I don't have 3 different if statements to go into each customers details. I'm assuming I can use the customer array possibly in a loop so the code is only written once.

Just the one switch statement for all customer details instead of the 3, as adding more customers will make the code a lot longer than it needs to be.


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
  if (input == 1111 || input == 2222 || input == 3333)
		{
			cout << "Please enter PIN number \t \t (It's 1234)" << endl;
			cin >> pin;
			cout << endl;
			if (pin == 1234)
			{
				displayInfo(customers[0]);
				if (input == 1 || input == 2 || input == 3) // Entering Dans #1 account to complete transactions
				{
					menuDisplay();
					switch (menuChoice)
					{
						case 1: withdraw(customers[0], input-1); // Making withdrawl from Dans #1 account
							break;
						case 2: deposit(customers[0], input-1); // Making deposits to Dans #1 account
							break;
						case 3: loan(customers[0], input-1); // Applying for loan on Dans #1 account
							break;
						case 4: overdraft(customers[0], input-1); // Applying for overdraft on Dans #1 account
							break;
						case 5: interest(customers[0], input-1); // Calculating interest for the year on Dans #1 account
							break;
					}
				}
			}
			// Input was invalid
			else
			{
				cout << "---------------" << endl;
				cout << "Invalid input." << endl;
				cout << "---------------" << endl;
			}
		}
		// Going into customer Johns details
		else if (input == 2222)
		{
			cout << "Please enter PIN number \t \t (It's 1234)" << endl;
			cin >> pin;
			cout << endl;
			if (pin == 1234)
			{
				displayInfo(customers[1]);
				if (input == 1 || input == 2 || input == 3) // Entering Johns #1 account to complete transactions
				{
					menuDisplay();
					switch (input) 
					{
					case 1: withdraw(customers[1], input - 1); // Making withdrawl from Johns #1 account
						break;
					case 2: deposit(customers[1], input - 1); // Making deposits to Johns #1 account
						break;
					case 3: loan(customers[1], input - 1); // Applying for loan on Johns #1 account
						break;
					case 4: overdraft(customers[1], input - 1); // Applying for overdraft on Johns #1 account
						break;
					case 5: interest(customers[1], input - 1); // Calculating interest for the year on Johns #1 account
						break;
					}
				}
				
			}
			// Input was invalid
			else
			{
				cout << "---------------" << endl;
				cout << "Invalid input." << endl;
				cout << "---------------" << endl;
			}
		}
		// Entering Daves customer details
		else if (input == 3333)
		{
			cout << "Please enter PIN number \t \t (It's 1234)" << endl;
			cin >> pin;
			cout << endl;
			if (pin == 1234)
			{
				displayInfo(customers[2]);
				if (input == 1 || input == 2 || input == 3) // Entering Daves #1 account to complete transactions
				{
					menuDisplay();
					switch (input)
					{
					case 1: withdraw(customers[2], input - 1); // Making withdrawl from Daves #1 account
						break;
					case 2: deposit(customers[2], input - 1); // Making deposits to Daves #1 account
						break;
					case 3: loan(customers[2], input - 1); // Applying for loan on Daves #1 account
						break;
					case 4: overdraft(customers[2], input - 1); // Applying for overdraft on Daves #1 account
						break;
					case 5: interest(customers[2], input - 1); // Calculating interest for the year on Daves #1 account
						break;
					}
				}
				
			}
			// Input was invalid
			else
			{
				cout << "---------------" << endl;
				cout << "Invalid input." << endl;
				cout << "---------------" << endl;
			}
		}


Any ideas?
Last edited on
Topic archived. No new replies allowed.