Need some guidance with passing struct by reference

Hey guys I was hoping to see if you guys could give me a push in the right direction.... I have a small program written out and I need to incorporate a few things from my assignment. I'm trying to pass a struct by reference and I'm not exactly sure how to do it.

My struct has to have the following:

a string object that holds the division's name.
4 doubles that hold sales totals for each quarter
two other doubles, one for annual sales, another for average,

should use four variables (istances) of the strcture, each variable should represent: east, west, north, south.

My functions have to look like this:

void DisplayCorpInformation (const Division& east, const Division& west, const Division& north, const Division& south);

void FindTotalAndAverageSales(Divison& div);

void GetDivisionSales(Division& div);

Any help would be greatly appreciated! Thank you!

Here is my code so far....

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

using namespace std;

// stucture function

struct Division												// changed from Corp Data to Division
{
	string div;												// divisions name.... changed from Division to name

	double annualSales;										// annual sales, now holds the total
	double avg;												// average of quarter sales
	double firstqtr, secondqtr, thirdqtr, fourthqtr;		// quater sales for first, second, third and fourth.
	//double total											// do not need this anymore
};


// Function prototypes

void GetDivisionSales(Division& div);
void FindTotalandAvgSales(Division& div);
void DisplayCorpInfo(const Division& north, const Division& south, const Division& east, const Division& west);

int main()

{
	Division north, south, east, west, div;					// added to declare north, south, east, west and div

	// Intro

	cout << "Corporate Data Sales Program" << endl;
	cout << "=============================\n" << endl;

	GetDivisionSales(div);
	FindTotalandAvgSales(div);
	DisplayCorpInfo(north, south, east, west);

	return 0;

}

// Function to get the Division sales
void GetDivisionSales(Division& div)
{

	Division north, south, east, west;

	// North Division

			cout << "\nEnter the quarterly sales for the North Division:" << endl;

			cout << "\t First quarter: ";
			cin >> north.firstqtr;

			cout << "\t Second quarter: ";
			cin >> north.secondqtr;

			cout << "\t Third quarter: ";
			cin >> north.thirdqtr;

			cout << "\t Fourth quarter: ";
			cin >> north.fourthqtr;

			// Validation

			if (north.firstqtr < 0 || north.secondqtr < 0 || north.thirdqtr < 0 || north.fourthqtr < 0)
			{
				cout << "You have entered in the a wrong number, please try something greater than 0" << endl;
				cin >> north.firstqtr;
				cin >> north.secondqtr;
				cin >> north.thirdqtr;
				cin >> north.fourthqtr;
			}

	// South Division

			cout << "\nEnter the quarterly sales for the South Division:" << endl;
			cout << "\t First quarter: ";
			cin >> south.firstqtr;

			cout << "\t Second quarter: ";
			cin >> south.secondqtr;

			cout << "\t Third quarter: ";
			cin >> south.thirdqtr;

			cout << "\t Fourth quarter: ";
			cin >> south.fourthqtr;

			// Validation

			if (north.firstqtr < 0 || north.secondqtr < 0 || north.thirdqtr < 0 || north.fourthqtr < 0)
			{
				cout << "You have entered in the a wrong number, please try something greater than 0" << endl;
				cin >> south.firstqtr;
				cin >> south.secondqtr;
				cin >> south.thirdqtr;
				cin >> south.fourthqtr;
			}

	// East Division

			cout << "\nEnter the quarterly sales for the East Division:" << endl;
			cout << "\t First quarter: ";
			cin >> east.firstqtr;

			cout << "\t Second quarter: ";
			cin >> east.secondqtr;

			cout << "\t Third quarter: ";
			cin >> east.thirdqtr;

			cout << "\t Fourth quarter: ";
			cin >> east.fourthqtr;

			// Validation

			if (east.firstqtr < 0 || east.secondqtr < 0 || east.thirdqtr < 0 || east.fourthqtr < 0)
			{
				cout << "You have entered in the a wrong number, please try something greater than 0" << endl;
				cin >> east.firstqtr;
				cin >> east.secondqtr;
				cin >> east.thirdqtr;
				cin >> east.fourthqtr;
			}

	// West Division

			cout << "\nEnter the quarterly sales for the West Division:" << endl;
			cout << "\t First quarter: ";
			cin >> west.firstqtr;

			cout << "\t Second quarter: ";
			cin >> west.secondqtr;

			cout << "\t Third quarter: ";
			cin >> west.thirdqtr;

			cout << "\t Fourth quarter: ";
			cin >> west.fourthqtr;

			// Validation

			if (west.firstqtr < 0 || west.secondqtr < 0 || west.thirdqtr < 0 || west.fourthqtr < 0)
			{
				cout << "You have entered in the a wrong number, please try something greater than 0" << endl;
				cin >> west.firstqtr;
				cin >> west.secondqtr;
				cin >> west.thirdqtr;
				cin >> west.fourthqtr;
			}
					

}

// Function to find Total and Average sales

void FindTotalandAvgSales(Division& div)

{

	Division north, south, east, west;

	// Calculates Totals

	north.annualSales = (north.firstqtr + north.secondqtr + north.thirdqtr + north.fourthqtr);
	south.annualSales = (south.firstqtr + south.secondqtr + south.thirdqtr + south.fourthqtr);
	east.annualSales = (east.firstqtr + east.secondqtr + east.thirdqtr + east.fourthqtr);
	west.annualSales = (west.firstqtr + west.secondqtr + west.thirdqtr + west.fourthqtr);

	// Calculates Average 
	north.avg = (north.annualSales / 4);
	south.avg = (south.annualSales / 4);
	east.avg = (east.annualSales / 4);
	west.avg = (west.annualSales / 4);


}

void DisplayCorpInfo(const Division& north, const Division& south, const Division& east, const Division& west)

{

Division north, south, east, west;

	// Display total sales

	cout << "\nTotal Annual Sales:" << endl;
	cout << "\t North Division: " << north.annualSales << endl;
	cout << "\t South Division: " << south.annualSales << endl;
	cout << "\t East Division: " << east.annualSales << endl;
	cout << "\t West Division: " << west.annualSales << endl;

	// Displays Average Sales

	cout << "\n Average Quarter Sales:" << endl;
	cout << "\t North Division: " << north.avg << endl;
	cout << "\t South Division: " << south.avg << endl;
	cout << "\t East Division: " << east.avg << endl;
	cout << "\t West Division: " << west.avg << endl;

}
Last edited on
My functions have to look like this:

None of your functions look like that. Perhaps you should begin there.
thanks cire

I made some changes and edited the original post... I'm getting an error...

c2080 redifition of formal parameter 'div'
c2080 redifition of formal parameter 'north'
c2080 redifition of formal parameter 'south'
c2080 redifition of formal parameter 'east'
c2080 redifition of formal parameter 'west'

any help in resolving the errors and getting the program to work would be greatly appreciated!
On line 181 you have parameters named north, south, east and west

On line 185 you try to define objects with the same names. Why?

You make a good point, I took them out and now the program runs, I'm just now getting memory addresses for the values entered instead of the values themselves.... not sure where to go from here...
I'm just now getting memory addresses for the values entered instead of the values themselves.

No, you're not. You're just modifying local variables in GetDivisionSales. In main and your other functions, you're dealing with uninitialized variables which contain junk values and which are completely unrelated to the variables modified in GetDivisionSales despite the fact that you've chosen to use the same name for them.
Topic archived. No new replies allowed.