Getting a not-initialized error but there's nothing to be initialized! Or so I think ?

Hello !

I'm working on this code for practice and (yes I know it's messy please ignore all the if-statements I'm trying to keep it in simple [and therefore really messy] - in fact maybe there's an issue in the messiness???) it keeps giving me the error below (all located in int main() ).

Could someone please tell me where the error is? As in, what do I need to fix?


Error 1 error C4700: uninitialized local variable 'OverAll' used
Error 2 error C4700: uninitialized local variable 'total' used



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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <iostream>
#include <cmath>
using namespace std;



struct MonthlyBudget
{
	double Housing,
	Utilities,
	Household,
	Transport,
	Food,
	Medical,
	Insurance,
	Ent,
	Clothing,
	Misc, total,
	resulth, resultu,
	resulthd, resultt,
	resultf, resultm,
	resulti, resulte,
	resultc, resultmi,
	OverAll, IdealOverall;
};



void Calculate(MonthlyBudget&, MonthlyBudget&, MonthlyBudget&, MonthlyBudget&);
void displayData(MonthlyBudget, MonthlyBudget, MonthlyBudget, MonthlyBudget);
void getUserInput(MonthlyBudget&);




int main()
{
	MonthlyBudget infor, total, hold, OverAll;


	
	hold.Housing = 500.00;
	hold.Utilities = 150.00;
	hold.Household = 65.00;
	hold.Transport = 50.00;
	hold.Food = 250.00;
	hold.Medical = 30.00;
	hold.Insurance = 100.00;
	hold.Ent = 150.00;
	hold.Clothing = 75.00;
	hold.Misc = 50.00;
	hold.OverAll = 1420.0;


	getUserInput(infor);
	displayData(infor, total, hold, OverAll);

	system("pause >nul");
	return 0;


    
}


void Calculate(MonthlyBudget &infor, 
MonthlyBudget &total, MonthlyBudget &hold, MonthlyBudget &OverAll)
{

	total.resulth = hold.Housing - infor.Housing;
	total.resultu = hold.Utilities - infor.Utilities;
	total.resulthd = hold.Household - infor.Household;
	total.resultt = hold.Transport - infor.Transport;
	total.resultf = hold.Food - infor.Food;
	total.resultm = hold.Medical - infor.Medical;
	total.resulti = hold.Insurance - infor.Insurance;
	total.resulte = hold.Ent - infor.Ent;
	total.resultc = hold.Clothing - infor.Clothing;
	total.resultmi = hold.Misc - infor.Misc;

	OverAll.OverAll = total.resulth + total.resultu +
 total.resulthd + total.resultt + total.resultf + 
total.resultmi + total.resulti + total.resulte + 
total.resultc + total.resultm;
	OverAll.IdealOverall = hold.OverAll - OverAll.OverAll;
}


void getUserInput(MonthlyBudget &infor)
{

	cout << "How much money did you spend on housing this month? ";
	cin >> infor.Housing;
	cout << "How much money did you spend on utilities this month? ";
	cin >> infor.Utilities;
	cout << "How much money did you spend on household expenses this month? ";
	cin >> infor.Household;
	cout << "How much money did you spend on transportation this month? ";
	cin >> infor.Transport;
	cout << "How much money did you spend on food this month? ";
	cin >> infor.Food;
	cout << "How much money did you spend on medical expenses this month? ";
	cin >> infor.Medical;
	cout << "How much money did you spend on insurance this month? ";
	cin >> infor.Insurance;
	cout << "How much money did you spend on entertainment this month? ";
	cin >> infor.Ent;
	cout << "How much money did you spend on clothing this month ? ";
	cin >> infor.Clothing;
	cout << "How much money did you spend on ???? this month? ";
	cin >> infor.Misc;
}

void displayData(MonthlyBudget infor, 
MonthlyBudget total, MonthlyBudget hold, MonthlyBudget OverAll)
{

	cout << "This Month's Expenses" << endl;

	cout << "\nHousing Expenses: " << infor.Housing;
	cout << "Utility Expenses: " << infor.Utilities;
	cout << "Household Expenses: " << infor.Household;
	cout << "Transportation Expenses: " << infor.Transport;
	cout << "Food Expenses: " << infor.Food;
	cout << "Medical Expenses: " << infor.Medical;
	cout << "Insurance Expenses: " << infor.Insurance;
	cout << "Entertainment Expenses: " << infor.Ent;
	cout << "Clothing Expenses: " << infor.Clothing;
	cout << "Misc Expenses: " << infor.Misc;



	Calculate(infor, total, hold, OverAll);



	cout << "Overall Expenses: " << OverAll.OverAll;
	if (OverAll.IdealOverall > 0)
	{
		cout << 
"Overall Expenses are under total budget by : " 
<< "$" << OverAll.IdealOverall;
	}
	if (OverAll.IdealOverall < 0)
	{
		OverAll.IdealOverall = OverAll.IdealOverall * -1;
		cout << 
"Overall Expenses are over total budget by : " 
<< "$" << OverAll.IdealOverall;
	}
	


	cout << "How much you went over/under budget" << endl;
	if (total.resulth > 0)
	{
		cout << "You were under budget by: " << "$" << total.resulth;

	}
	if (total.resultu > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultu;

	}
	if (total.resulthd > 0)
	{
		cout << "You were under budget by: " << "$" << total.resulthd;

	}
	if (total.resultt > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultt;

	}
	if (total.resultf > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultf;

	}
	if (total.resultm > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultm;

	}
	if (total.resulti > 0)
	{
		cout << "You were under budget by: " << "$" << total.resulti;

	}
	if (total.resulte > 0)
	{
		cout << "You were under budget by: " << "$" << total.resulte;

	}
	if (total.resultc > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultc;

	}
	if (total.resultmi > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultu;

	}
		if (total.resulth > 0)
	{
		cout << "You were under budget by: " << "$" << total.resulth;

	}
	if (total.resultu > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultu;

	}
	if (total.resulthd > 0)
	{
		cout << "You were under budget by: " << "$" << total.resulthd;

	}
	if (total.resultt > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultt;

	}
	if (total.resultf > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultf;

	}
	if (total.resultm > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultm;

	}
	if (total.resulti > 0)
	{
		cout << "You were under budget by: " << "$" << total.resulti;

	}
	if (total.resulte > 0)
	{
		cout << "You were under budget by: " << "$" << total.resulte;

	}
	if (total.resultc > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultc;

	}
	if (total.resultmi > 0)
	{
		cout << "You were under budget by: " << "$" << total.resultmi;

	}


	if (total.resulth < 0)
	{
		total.resulth = total.resulth * -1;
		cout << "You were over budget by: " << "$" << total.resulth;

	}
	if (total.resultu < 0)
	{
		total.resultu = total.resultu * -1;
		cout << "You were over budget by: " << "$" << total.resultu;

	}
	if (total.resulthd < 0)
	{
		total.resulthd = total.resulthd * -1;
		cout << "You were over budget by: " << "$" << total.resulthd;

	}
	if (total.resultt < 0)
	{
		total.resultt = total.resultt * -1;
		cout << "You were over budget by: " << "$" << total.resultt;

	}
	if (total.resultf < 0)
	{
		total.resultf =total.resultf * -1;
		cout << "You were over budget by: " << "$" << total.resultf;

	}
	if (total.resultm < 0)
	{
		total.resultm = total.resultm * -1;
		cout << "You were over budget by: " << "$" << total.resultm;

	}
	if (total.resulti < 0)
	{
		total.resulti = total.resulti * -1;
		cout << "You were over budget by: " << "$" << total.resulti;

	}
	if (total.resulte < 0)
	{
		total.resulte= total.resulte * -1;
		cout << "You were over budget by: " << "$" << total.resulte;

	}
	if (total.resultc < 0)
	{
		total.resultc = total.resultc * -1;
		cout << "You were over budget by: " << "$" << total.resultc;

	}
	if (total.resultmi < 0)
	{
		total.resultmi = total.resultmi * -1;
		cout << "You were over budget by: " << "$" << total.resultmi;

	}


}
Last edited on
That is a lot of repeated code you got there... the problem is that you seem to be initializing a lot of things, in different areas of your code. This is very prone to error, and it's hard for someone else to easily see what's going on.

For example, I wouldn't expect a displayData() function to then call a calculate function that then changes the properties in the parameters. Your code is all over the place, and chances are that you missed something.

Edit: Is your compiler giving you a particular line or two where it says it's uninitialized?

Edit 2: Before you call any other functions, you could try initializing every property in all 4 of your struct instances to be -1.0 or -100000.0. Then you can change it from there and use a debugger or give cout statements to see where the problem is.

Edit 3: Your displayData() function isn't passing any of your MonthlyBudget instances by reference. It probably doesn't like that your total or overAll instances are being passed without being initialized.
Last edited on
Yeah, true. I think in the end I'll have to just scrap this and start again or go through the solution manual and see what they did.

I was trying to emulate what I did in my other code but this got too, too messy and now I have no idea what the heck is going on. I thought maybe someone else could but yeah @__@

Edit: woops, I actually had my error's in there but I guess I accidentally deleted them when I went over the text limit. Added the error's back in. They're all in int main() [both in line 56]
Last edited on
Line 38: You declare total and Overall, but you never initialize these structures before passing them to displayData() at line 56.
THANK YOU to the both of you!! It's fixed. I'll keep in mind for future though to have cleaner code.
Topic archived. No new replies allowed.