Calling a function that has an array?

Nothing I try works. The offender is in the switch statement inside the showMainMenu function.

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
  #include <iostream>
#include <iomanip>

#include "bankAccount.h"

using namespace std;


const int MAX_NUM_OF_ACCOUNTS = 10;


void showMainMenu();
void showAccountMenu();
int searchBankAccount(bankAccount list[], int length, int acctNum);
void addBankAccount(bankAccount list[], int& length);
void processBankAccount(bankAccount list[], int length, int acctNum);
void printBankAccountsData(bankAccount list[], int length);

// if you need more functions, add their headers here.


int main()
{
	bankAccount bankAccounts[MAX_NUM_OF_ACCOUNTS];
	int numOfBankAccounts = 0; // this shows the actual number of bank accounts in the array
	bankAccounts[0].getAccountNumber;
	// enter the code for main function here.


	return 0;
}

void showMainMenu(){
	
	int userinput;
	
	int list[MAX_NUM_OF_ACCOUNTS];

	cout << "Hello, welcome to my bank! Press the number next to the requested action and press enter to continue" << endl;
	cout << "1. Add a bank account." << endl;
	cout << "2. Print existing bank accounts." << endl;
	cout << "3. Search existing bank accounts based on the account number." << endl;
	cout << "4. Exit the program." << endl;
	cin >> userinput;

	switch (userinput) {

	case 1: addBankAccount(list[]
		break;
	

	}
}
Well, I managed to get rid of all the syntax errors, but by creating variables in showMainMenu that matched those in the addBankAccount prototype:

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
void showMainMenu(){
	
	int userinput;
	int length;
	int acctNum;
	
	bankAccount list[MAX_NUM_OF_ACCOUNTS];

	cout << "Hello, welcome to my bank! Press the number next to the requested action and press enter to continue" << endl;
	cout << "1. Add a bank account." << endl;
	cout << "2. Print existing bank accounts." << endl;
	cout << "3. Search existing bank accounts based on the account number." << endl;
	cout << "4. Exit the program." << endl;
	cin >> userinput;

	switch (userinput) {

	case 1: addBankAccount(list, length);
		break;
	case 2: printBankAccountsData(list, length);
		break;
	case 3: searchBankAccount(list, length, acctNum);
		break;
	case 4: 

	}
What help do u need now ?
Well, using every function header provided to me by my professor, I now have to make the addBankAccount function by adding a bank account to the bankAccount array (list) . However, I'm unsure as to what "length" is supposed to do exactly.
Except you post the implementation details of the function addBankAccount, it would be hard for me to know what the Parameter length i used for
Oh, sorry. Here it is.

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
#include <iostream>
#include <iomanip>
#include <string>

#include "bankAccount.h"

using namespace std;

int bankAccount::getAccountNumber()
{
	return accountNum;
}

string bankAccount::getAccountHolderName()
{
	return fullName;
}

string bankAccount::getAccountType()
{
	return type;
}

double bankAccount::getBalance()
{
	return balance;
}

string bankAccount::printAccountData()
{
	cout << fullName << endl;
	cout << accountNum << endl;
	cout << type << endl;
	cout << balance << endl;
}

void bankAccount::setData(string fn, int accNUM, string accTYPE, double remaining)
{
	fullName = fn;
	accountNum = accNUM;
	type = accTYPE;
	balance = remaining;
}

double bankAccount::deposit(double deposits)
{
	balance = balance + deposits;
}

double bankAccount::withdrawal(double withdrawals)
{
	balance = balance - withdrawals;
}
i mean your code for the function addBankAccount not for the member function..
I didn't add anything to it yet.

1
2
3
void addBankAccount(bankAccount list[], int& length){

}
Great, I tried adding some things to it and it's saying fullName is undefined for seemingly no reason. Also, are those member function calls correct? Probably not, but I'd like some assistance anyway. Thank you in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void addBankAccount(bankAccount list[], int& length){
	int accountNum;
	cout << "Enter the new account number: " << endl;
	cin >> accountNum;

	bankAccount() { list[getAccountNumber()]; }

	string fullName;
	cout << "Enter your name: " << endl;
	cin >> fullName;

	bankAccount() { list[getAccountHolderName()]; }

}
1
2
3
4
5
6
7
8
9
10
11
	switch (userinput) {

	case 1: addBankAccount(list, length);
		break;
	case 2: printBankAccountsData(list, length);
		break;
	case 3: searchBankAccount(list, length, acctNum);
		break;
	case 4: 

	

^This should not be part of your showMainMenu() function. It should be part of your main() and look like this:

1
2
3
4
5
6
7
8
9
10
11
	switch (userinput) {

	case 1: addBankAccount(bankAccounts, numOfBankAccounts);
		break;
	case 2: printBankAccountsData(bankAccounts, numOfBankAccounts);
		break;
	case 3: //prompt for acctNum
		//input acctNum
		searchBankAccount(bankAccounts, numOfBankAccounts, acctNum);
		break;
	case 4: return 0;
Crap. This is what I have now. 11 bugs including unresolved externals, fullName being undefined in addBankAccount, and expected semicolons I don't know where to put. Sorry guys, it's just that I'm getting really frustrated with this.

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
#include <iostream>
#include <iomanip>
#include <string>

#include "bankAccount.h"

using namespace std;


const int MAX_NUM_OF_ACCOUNTS = 10;


void showMainMenu();
void showAccountMenu();
int searchBankAccount(bankAccount list[], int length, int acctNum);
void addBankAccount(bankAccount list[], int& length);
void processBankAccount(bankAccount list[], int length, int acctNum);
void printBankAccountsData(bankAccount list[], int length);

// if you need more functions, add their headers here.


int main()
{
	bankAccount bankAccounts[MAX_NUM_OF_ACCOUNTS];
	int numOfBankAccounts = 0; // this shows the actual number of bank accounts in the array
	&bankAccount::getAccountNumber;
	
	showMainMenu();

	int userinput;
	int acctNum;

	switch (userinput) {

	case 1: addBankAccount(bankAccounts, numOfBankAccounts);
		break;
	case 2: printBankAccountsData(bankAccounts, numOfBankAccounts);
		break;
	case 3: cout << "Enter account Number: ";
		cin >> acctNum;
		searchBankAccount(bankAccounts, numOfBankAccounts, acctNum);
		if (acctNum = bankAccount::getAccountNumber){
			showAccountMenu();
		else {
			addBankAccount(bankAccounts, acctNum);
		}
			
		break;
	case 4: return 0;


	return 0;
}

void showMainMenu(){
	
	cout << "Hello, welcome to my bank! Press the number next to the requested action and press enter to continue" << endl;
	cout << "1. Add a bank account." << endl;
	cout << "2. Print existing bank accounts." << endl;
	cout << "3. Search existing bank accounts based on the account number." << endl;
	cout << "4. Exit the program." << endl;

}

void showAccountMenu(){
	bankAccount list[MAX_NUM_OF_ACCOUNTS];
	
	int userinput;
	int length = 0;
	int acctNum = 0;
	
	cout << "5. Deposit money to the account." << endl;
	cout << "6. Withdraw money from the account." << endl;
	cout << "7.  Check the balance on the account." << endl;
	cout << "8. Exit to main menu." << endl;
	cin >> userinput;

	switch (userinput) {

	case 5: processBankAccount(list, length, acctNum);
		break;
	case 6: processBankAccount(list, length, acctNum);
		break;
	case 7: processBankAccount(list, length, acctNum);
		break;
	case 8: showMainMenu();
		system("pause");
	}

}

void addBankAccount(bankAccount list[], int& length){
	int accountNum;
	cout << "Enter the new account number: " << endl;
	cin >> accountNum;

	bankAccount() { list[getAccountNumber()]; }

	string fullName;
	cout << "Enter your name: " << endl;
	cin >> fullName;

	bankAccount() { list[getAccountHolderName()]; }

}
In addition to the 4 unresolved externals. It still says it expected a semicolon around Line 66 and a declaration near Line 91. Please help!
I've fixed the unresolved externals, but now it's saying showAccountMenu has illegal function definitions even though there are no function calls or anything in it, plus I don't know how to get the name, etc, in printBankAccountsData.

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
//Main program

#include <iostream>
#include <iomanip>
#include <string>

#include "bankAccount.h"

using namespace std;


const int MAX_NUM_OF_ACCOUNTS = 10;


void showMainMenu();
void showAccountMenu();
int searchBankAccount(bankAccount list[], int length, int acctNum);
void addBankAccount(bankAccount list[], int& length);
void processBankAccount(bankAccount list[], int length, int acctNum);
void printBankAccountsData(bankAccount list[], int length);

// if you need more functions, add their headers here.


int main()
{
	bankAccount bankAccounts[MAX_NUM_OF_ACCOUNTS];
	int numOfBankAccounts = 0; // this shows the actual number of bank accounts in the array
	&bankAccount::getAccountNumber;

	showMainMenu();

	int userinput;
	int acctNum;

	switch (userinput) {

	case 1: addBankAccount(bankAccounts, numOfBankAccounts);
		break;
	case 2: printBankAccountsData(bankAccounts, numOfBankAccounts);
		break;
	case 3: cout << "Enter account Number: ";
		cin >> acctNum;
		searchBankAccount(bankAccounts, numOfBankAccounts, acctNum);
		if (acctNum > 0) {
			showAccountMenu();

			switch (userinput) {

			case 5: processBankAccount(bankAccounts, numOfBankAccounts, acctNum);
				break;
			case 6: processBankAccount(bankAccounts, numOfBankAccounts, acctNum);
				break;
			case 7: processBankAccount(bankAccounts, numOfBankAccounts, acctNum);
				break;
			case 8: showMainMenu();
				system("pause");
			}
			if (int numofBankAccounts = 0)
			{

				addBankAccount(bankAccounts, numofBankAccounts);
			}
			break;
	case 4: return 0;

		return 0;
		}
	}
	void showMainMenu();
	{

		cout << "Hello, welcome to my bank! Press the number next to the requested action and press enter to continue" << endl;
		cout << "1. Add a bank account." << endl;
		cout << "2. Print existing bank accounts." << endl;
		cout << "3. Search existing bank accounts based on the account number." << endl;
		cout << "4. Exit the program." << endl;

	}

	void showAccountMenu()
	{
		cout << "5. Deposit money to the account." << endl;
		cout << "6. Withdraw money from the account." << endl;
		cout << "7.  Check the balance on the account." << endl;
		cout << "8. Exit to main menu." << endl;

	}


void addBankAccount(bankAccount list[], int& length){
	int accountNum;
	cout << "Enter the new account number: " << endl;
	cin >> accountNum;

	bankAccount() { list[getAccountNumber()]; }

	string fullName;
	cout << "Enter your name: " << endl;
	cin >> fullName;

	bankAccount() { list[getAccountHolderName()]; }

}

void processBankAccount(bankAccount list[], int length, int acctNum)
{
	int userinput;
	double deposit, withdrawal, balance;
	cout << "1. Add a deposit." << endl;
	cout << "2. Do a withdrawal." << endl;
	cout << "3. See your remaining balance" << endl;
	cin >> userinput;

	switch (userinput) {
	case 1: {
		cout << "How much do you want to deposit?" << endl;
		cin >> deposit;
		balance = balance + deposit;
	}
	case 2: {
		cout << "How much do you want to withdraw?" << endl;
		cin >> withdrawal;
		balance = balance - withdrawal;
	}
	case 3:
		cout << "Remaining balance: " << balance << endl;
	}
}

	int searchBankAccount(bankAccount list[], int length, int acctNum)
	{
		int userinput;
		cout << "Enter the number of the account you would like to find." << endl;
		cin >> userinput;

		if (userinput = bankAccount.accountNum)
		{
			printBankAccountsData(list, length);
		}
	}

	void printBankAccountsData(bankAccount list[], int length)
	{
		string fullName = bankAccount::getAccountHolderName;
		int accountNum;
		string type;
		double balance;

		for (int i = 1; i < MAX_NUM_OF_ACCOUNTS; i++)
		{
			cout << "Details of all accounts:" << endl;
			cout << " --- ---- ------- ------- " << endl;
			cout << "Account no.: " << << endl;
			cout << "Name: " << fullName << endl;
			cout << "Current Balance: " << balance << endl;
		}
	}
Not sure if it's a problem with copy/paste but you're missing some brackets. I don't see the bracket that closes off the main function, which means that all those other functions are being defined inside of main. You cannot define a function inside of another function.

I see that you moved the switch into the main function like i said, but you forgot to actually get userinput. There should probably be a loop in there somewhere too.

I made some changes to your main function and commented them, now you have to fix the rest of the code. For example, there is a semicolon when there shouldn't be on line 72.

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
int main()
{
    bankAccount bankAccounts[MAX_NUM_OF_ACCOUNTS];
    int numOfBankAccounts = 0; // this shows the actual number of bank accounts in the array
    &bankAccount::getAccountNumber; //what is this supposed to do?
    int userinput, acctNum, index;

    while (true) //added this infinite loop, it's fine because of case 4 in outer switch case
    {
        showMainMenu();
        cin >> userinput; //store input from user

        switch (userinput) {

            case 1: addBankAccount(bankAccounts, numOfBankAccounts);
                    break;
            case 2: printBankAccountsData(bankAccounts, numOfBankAccounts);
                    break;
            case 3: cout << "Enter account Number: ";
                    cin >> acctNum;
                    index = searchBankAccount(bankAccounts, numOfBankAccounts, acctNum); //save the account's index in array
                    if (index >= 0) { //usually, these search functions return -1 if not found
                        showAccountMenu();
                        cin >> userinput; //store input from user
                        switch (userinput) {

                            case 5: processBankAccount(bankAccounts, numOfBankAccounts, acctNum);
                                    break;
                            case 6: processBankAccount(bankAccounts, numOfBankAccounts, acctNum);
                                    break;
                            case 7: processBankAccount(bankAccounts, numOfBankAccounts, acctNum);
                                    break;
                            case 8: //don't need to do anything because of infinite loop
                        }
                        if (numOfBankAccounts == 0) //removed int, fixed the name of the variable, changed to ==
                            addBankAccount(bankAccounts, numOfBankAccounts); //not sure why you want to do this though
                    } //fixed some bracket locations, added some, deleted some, etc.
                    break;
            case 4: return 0;
        }
    }
    return 0; //function never actually gets here, so this isn't really necessary
}
Last edited on
Topic archived. No new replies allowed.