Passing an array of structures using a pointer

Hey guys, I'm having some problems with a program I'm writing for my class. One of the steps calls for me to write a function that uses a pointer to a structure variable as a parameter. I've tried ever variation I can think of, but I'm not getting anywhere. I keep getting errors relating to overloading my function or something about an unresolved external symbol. The program and the function are incomplete while I try to solve this problem. Here's my program so far (the specific areas are lines 12, 86, and 101):

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

using namespace std;

struct DeptData
{
	string deptName;
	int numTraders;
};

int addTraders (DeptData *, int, int);

int main()
{
	const int maxDept = 10;
	DeptData department[maxDept];
	DeptData *arrPtr;
	arrPtr = department;
	string answer;
	int num;

	department[0].deptName = "Bank Loan";
	department[0].numTraders = 15;
	cout << "1. " << department[0].deptName << " --- "; 
	cout << department[0].numTraders << endl;

	department[1].deptName = "Conservative Allocation";
	department[1].numTraders = 9;
	cout << "2. " << department[1].deptName << " --- "; 
	cout << department[1].numTraders << endl;

	department[2].deptName = "Europe Stock";
	department[2].numTraders = 10;
	cout << "3. " << department[2].deptName << " --- ";
	cout << department[2].numTraders << endl;


	department[3].deptName = "Domestic";
	department[3].numTraders = 21;
	cout << "4. " << department[3].deptName << " --- "; 
	cout << department[3].numTraders << endl;
		
	department[4].deptName = "Asia";
	department[4].numTraders = 7;
	cout << "5. " << department[4].deptName << " --- "; 
	cout << department[4].numTraders << endl;

	department[5].deptName = "Large Growth";
	department[5].numTraders = 5;
	cout << "6. " << department[5].deptName << " --- "; 
	cout << department[5].numTraders << endl;
			
	department[6].deptName = "Long-term Bond";
	department[6].numTraders = 5;
	cout << "7. " << department[6].deptName << " --- "; 
	cout << department[6].numTraders << endl;

	department[7].deptName = "Money Market";
	department[7].numTraders = 25;
	cout << "8. " << department[7].deptName << " --- "; 
	cout << department[7].numTraders << endl;
			
	department[8].deptName = "Emerging Market";
	department[8].numTraders = 18;
	cout << "9. " << department[8].deptName << " --- "; 
	cout << department[8].numTraders << endl;

	department[9].deptName = "Large Blend";
	department[9].numTraders = 12;
	cout << "10. " << department[9].deptName << " --- "; 
	cout << department[9].numTraders << endl;


	cout << "Would you like to add or remove traders from a department?\n";
	cout << " (enter yes or no, or quit to terminate)\n";
	cin >> answer;
	if (answer == "quit")
		return 0;
	if (answer == "yes")
	{
		cout << "Please enter the number to the left of whichever ";
		cout << "department you would like to alter.\n";
		cin >> num;
		int index = num - 1; 
		department[num - 1].numTraders = addTraders (department[index], index);
	}

	if (answer != "yes" || answer != "quit")
	{
		cout << "You must enter either yes or quit. Please ";
		cout << "restart the program and enter a valid choice.\n";
		system("Pause");
		return 0;
	}
	
	system("PAUSE");
	return 0;
}

int addTraders (int *department, int maxDept, int index)
{
	cout << "Please enter the number of traders you would like ";
	cout << "to add for" << department[index];
}

Please show the error messages.
yes, your forward declaration isn't compatible with the actual definition of the addTraders() function. This causes the unresolved external error. In line 86 you call addTraders() with only two arguments, and there have to be three. So: change int addTraders (int *department, int maxDept, int index) to int addTraders( DeptData* department, int maxDept, int index ), add a maxDept argument at line 86, and change the body of addTraders(), so that is works in the right way.
Sorry about that. I've revised my program to how it's supposed to look and I'm still having problems. The errors I'm getting:

error LNK2019: unresolved external symbol "void __cdecl addTraders(struct DeptData *,int)" (?addTraders@@YAXPAUDeptData@@H@Z) referenced in function _main
C:\Users\owner\Desktop\Josh - School\CSCI2010\Pass5\Debug\Pass5.exe : fatal error LNK1120: 1 unresolved externals

I also tried to put in DeptData* department in my call, but it says type name is not allowed, so I changed it to arrPtr, which is supposed to point to the array in line 19.

Here is my revised code, with the problems still on lines 12, 86, and 101 (and possibly 19):

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

using namespace std;

struct DeptData
{
	string deptName;
	int numTraders;
};

void addTraders (DeptData *, int);

int main()
{
	const int maxDept = 10;
	DeptData department[maxDept];
	DeptData *arrPtr;
	arrPtr = department;
	string answer;
	int num;

	department[0].deptName = "Bank Loan";
	department[0].numTraders = 15;
	cout << "1. " << department[0].deptName << " --- "; 
	cout << department[0].numTraders << endl;

	department[1].deptName = "Conservative Allocation";
	department[1].numTraders = 9;
	cout << "2. " << department[1].deptName << " --- "; 
	cout << department[1].numTraders << endl;

	department[2].deptName = "Europe Stock";
	department[2].numTraders = 10;
	cout << "3. " << department[2].deptName << " --- ";
	cout << department[2].numTraders << endl;


	department[3].deptName = "Domestic";
	department[3].numTraders = 21;
	cout << "4. " << department[3].deptName << " --- "; 
	cout << department[3].numTraders << endl;
		
	department[4].deptName = "Asia";
	department[4].numTraders = 7;
	cout << "5. " << department[4].deptName << " --- "; 
	cout << department[4].numTraders << endl;

	department[5].deptName = "Large Growth";
	department[5].numTraders = 5;
	cout << "6. " << department[5].deptName << " --- "; 
	cout << department[5].numTraders << endl;
			
	department[6].deptName = "Long-term Bond";
	department[6].numTraders = 5;
	cout << "7. " << department[6].deptName << " --- "; 
	cout << department[6].numTraders << endl;

	department[7].deptName = "Money Market";
	department[7].numTraders = 25;
	cout << "8. " << department[7].deptName << " --- "; 
	cout << department[7].numTraders << endl;
			
	department[8].deptName = "Emerging Market";
	department[8].numTraders = 18;
	cout << "9. " << department[8].deptName << " --- "; 
	cout << department[8].numTraders << endl;

	department[9].deptName = "Large Blend";
	department[9].numTraders = 12;
	cout << "10. " << department[9].deptName << " --- "; 
	cout << department[9].numTraders << endl;


	cout << "Would you like to add traders from a department?\n";
	cout << " (enter yes, or quit to terminate)\n";
	cin >> answer;
	if (answer == "quit")
		return 0;
	if (answer == "yes")
	{
		cout << "Please enter the number to the left of whichever ";
		cout << "department you would like to alter.\n";
		cin >> num;
		int index = num - 1; 
		addTraders (arrPtr, index);
	}

	if (answer != "yes" || answer != "quit")
	{
		cout << "You must enter either yes or quit. Please ";
		cout << "restart the program and enter a valid choice.\n";
		system("Pause");
		return 0;
	}
	
	system("PAUSE");
	return 0;
}

void addTraders (int *arrPtr, int index)
{
	int add;
	cout << "Please enter the number of traders you would like ";
	cout << "to add. ";
	cin >> add;
	cout << endl;
	arrPtr[index] += add;
}
Last edited on
You declared the function as

void addTraders (DeptData *, int);

but defined it as

void addTraders (int *arrPtr, int index)





Thanks. Now the += in line 108 show an error. I'm looking through my notes for that problem, but any help would be appreciated.
If you now declared the function as

void addTraders (DeptData *, int);

then you cannot assign an integer to an object of type Deptdate

Maybe you meant to assign structure data member numTraders?
In this case you shall write

arrPtr[index].numTraders += add;
That worked perfectly, thank you. And sorry for the simple questions. I've been studying for 4 midterms tomorrow and doing this assignment, so my brains on the mushy side right now. Thanks again.
Topic archived. No new replies allowed.