Function to find largest number out of a group

So, i put together a quick code, and i need to be able to find the highest value out of a bunch of int variables (va, vb, vc...) and i chose to use an array. I initialized the variable winningValue as 1, and it seems to stay that way even after the function. Any ideas. Here are the important parts. Thanks!


int array[26] = {va, vb, vc, vd, ve, vf, vg, vh, vi, vj, vk, vl, vm, vn, vo, vp, vq, vr, vs, vt, vu, vv, vw, vx, vy, vz};

for (int count = 1; count < 26; ++count)
{
if (array[count] > winningValue)
{
winningValue = array[count];
}

}

Sorry! I'm not sure why I couldn't put the code in a box.
I initialized the variable winningValue as 1, and it seems to stay that way even after the function.

Logically, that would happen if all the array values were either 1 or less. Perhaps all are zero?

As an aside, why do you have twenty-six separate but clearly related variables? Wouldn't it be simpler to just use an array in the first place?

I suggest you show the rest of the code so we can see what is going on.


Sorry! I'm not sure why I couldn't put the code in a box.

http://www.cplusplus.com/articles/jEywvCM9/
I'm pretty inexperienced so this may be a horribly slow way to do it, but this it the code, it shouldn't be too hard to read, its just redundant.
a# is the answer for each number question
va, vb, vc, is the value for each occupation (aka their rating)
a, b, c, represents the name of the occupation

Heres the code:

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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char**argv)
{

	//Declaration of Variables

	std::string a = "doctor.";
	std::string b = "teacher.";
	std::string c = "scientist.";
	std::string d = "zookeeper.";
	std::string e = "veterinarian.";
	std::string f = "firefighter.";
	std::string g = "therapist.";
	std::string h = "babysitter.";
	std::string i = "construction worker.";
	std::string j = "mechanic.";
	std::string k = "car salesman.";
	std::string l = "filmmaker.";
	std::string m = "artist.";
	std::string n = "sports player.";
	std::string o = "businessman.";
	std::string p = "computer programmer.";
	std::string q = "engineer.";
	std::string r = "mathematician.";
	std::string s = "farmer.";
	std::string t = "astronaut.";
	std::string u = "pilot.";
	std::string v = "author.";
	std::string w = "policeman.";
	std::string x = "detective.";
	std::string y = "physical therapist.";
	std::string z = "lawyer.";

	//Occupations initialized ^^

	std::string a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;

	//Answer strings initalized ^^

	int va = 0, vb = 0, vc = 0, vd = 0, ve = 0, vf = 0, vg = 0, vh = 0, vi = 0, vj = 0, vk = 0, vl = 0, vm = 0, vn = 0, vo = 0, vp = 0, vq = 0, vr = 0, vs = 0, vt = 0, vu = 0, vv = 0, vw = 0, vx = 0, vy = 0, vz = 0;

	//Occupation ratings initalized ^^

	std::string winningJob;
	int array[26] = {va, vb, vc, vd, ve, vf, vg, vh, vi, vj, vk, vl, vm, vn, vo, vp, vq, vr, vs, vt, vu, vv, vw, vx, vy, vz};
	int winningValue = 0;

	//Time to introduce the program and instructions
	
	cout << "Don't know what you want for a career?  Take this short quiz and find out what's best for you.  Answer yes or no." << endl;
	cout << endl;

	system("pause");

	cout << endl;
	
	//Time to ask the questions

	for (int qs = 1; qs <= 20, ++qs;)
	{

		//Ask each question and return the answer as a#

		switch (qs)
		{
			case 1:
				cout << "Do you like to work with other people?" << endl;
				std::cin >> a1;
				break;
			case 2:
				cout << "Are you creative?" << endl;
				std::cin >> a2;
				break;
			case 3:
				cout << "Do you like to exercise?" << endl;
				std::cin >> a3;
				break;
			case 4:
				cout << "Are you good at math?" << endl;
				std::cin >> a4;
				break;
			case 5:
				cout << "Do you like to work with your hands?" << endl;
				std::cin >> a5;
				break;
			case 6:
				cout << "Are you good with kids?" << endl;
				std::cin >> a6;
				break;
			case 7:
				cout << "Are you competitive?" << endl;
				std::cin >> a7;
				break;
			case 8:
				cout << "Do you value justice?" << endl;
				std::cin >> a8;
				break;
			case 9:
				cout << "Are you good with technology?" << endl;
				std::cin >> a9;
				break;
			case 10:
				cout << "Are you easily afraid?" << endl;
				std::cin >> a10;
				break;
			case 11:
				cout << "Do you like to work with moving parts?" << endl;
				std::cin >> a11;
				break;
			case 12:
				cout << "Are you often kind or generous to others?" << endl;
				std::cin >> a12;
				break;
			case 13:
				cout << "Do you like to solve riddles or puzzles?" << endl;
				std::cin >> a13;
				break;
			case 14:
				cout << "Is money important to you?" << endl;
				std::cin >> a14;
				break;
			case 15:
				cout << "Do you like to be outside?" << endl;
				std::cin >> a15;
				break;
			case 16:
				cout << "Do you like to tell stories?" << endl;
				std::cin >> a16;
				break;
			case 17:
				cout << "Do you often feel the need to express yourself?" << endl;
				std::cin >> a17;
				break;
			case 18:
				cout << "Are you good at persuasion?" << endl;
				std::cin >> a18;
				break;
			case 19:
				cout << "Do you like animals?" << endl;
				std::cin >> a19;
				break;
			case 20:
				cout << "Do you get angry easily?" << endl;
				std::cin >> a20;
				break;
		}
	    //Questions now have answers in the form of a string
	}
	//Exiting for loop

	//Time to increase each rating based on answers

	if (a1 == "yes")
	{
		++vb;
		++vd;
		++vg;
		++vi;
		++vl;
		++vn;
		++vo;
		++vy;
	}
	
	if (a2 == "yes")
	{
		++vl;
		++vm;
		++vv;
	}
	
	if (a3 == "yes")
	{
		++vn;
		++vy;
	}

	if (a4 == "yes")
	{
		++vj;
		++vp;
		++vq;
		++vs;
		++vt;
	}

	if (a5 == "yes")
	{
		++vi;
		++vj;
		++vl;
		++vq;
		++vs;
		++vt;
		++vy;
	}

	if (a6 == "yes")
	{
		++va;
		++vb;
		++vh;
	}

	if (a7 == "yes")
	{
		++vk;
		++vn;
		++vo;
		++vz;
	}

	if (a8 == "yes")
	{
		++vw;
		++vx;
		++vz;
	}

	if (a9 == "yes")
	{
		++vc;
		++vj;
		++vl;
		++vp;
		++vq;
		++vt;
		++vu;
	}

	if (a10 == "yes")
	{
		++vh;
		++vm;
		++vv;
	}

	if (a11 == "yes")
	{
		++vj;
		++vq;
		++vt;
		++vu;
		++vy;
	}

	if (a12 == "yes")
	{
		++va;
		++vb;
		++vd;
		++ve;
		++vh;
		++vy;
	}

	if (a13 == "yes")
	{
		++vc;
		++vj;
		++vp;
		++vq;
		++vr;
		++vx;
		++vz;
	}

	if (a14 == "yes")
	{
		++va;
		++vc;
		++ve;
		++vl;
		++vn;
		++vo;
		++vp;
		++vq;
		++vt;
		++vu;
		++vx;
		++vz;
	}

	if (a15 == "yes")
	{
		++vd;
		++vs;
	}

	if (a16 == "yes")
	{
		++vl;
		++vm;
		++vv;
	}

	if (a17 == "yes")
	{
		++vl;
		++vm;
		++vv;
	}

	if (a18 == "yes")
	{
		++vg;
		++vk;
		++vo;
		++vz;
	}

	if (a19 == "yes")
	{
		++vd;
		++ve;
		++vh;
		++vs;
	}

	if (a20 == "yes")
	{
		--va;
		--vb;
		--vd;
		--ve;
		--vg;
		--vh;
		--vx;
		--vz;
	}


	//Ratings are now set as a value which will correspond to each occupation.

	//Time to find the largest value.

	for (int count = 1; count < 26; ++count)
	{
		if (array[count] > winningValue)
		{
			winningValue = array[count];
		}
			
	}

	//Winning value has been set.

	//Time to find the winning occupation based on the winning value.

	if (winningValue == va)
	{
		winningJob = a;
	}

	if (winningValue == vb)
	{
		winningJob = b;
	}

	if (winningValue == vc)
	{
		winningJob = c;
	}

	if (winningValue == vd)
	{
		winningJob = d;
	}

	if (winningValue == ve)
	{
		winningJob = e;
	}

	if (winningValue == vf)
	{
		winningJob = f;
	}

	if (winningValue == vg)
	{
		winningJob = g;
	}

	if (winningValue == vh)
	{
		winningJob = h;
	}

	if (winningValue == vi)
	{
		winningJob = i;
	}

	if (winningValue == vj)
	{
		winningJob = j;
	}

	if (winningValue == vk)
	{
		winningJob = k;
	}

	if (winningValue == vl)
	{
		winningJob = l;
	}

	if (winningValue == vm)
	{
		winningJob = m;
	}

	if (winningValue == vn)
	{
		winningJob = n;
	}

	if (winningValue == vo)
	{
		winningJob = o;
	}

	if (winningValue == vp)
	{
		winningJob = p;
	}

	if (winningValue == vq)
	{
		winningJob = q;
	}

	if (winningValue == vr)
	{
		winningJob = r;
	}

	if (winningValue == vs)
	{
		winningJob = s;
	}

	if (winningValue == vt)
	{
		winningJob = t;
	}

	if (winningValue == vu)
	{
		winningJob = u;
	}

	if (winningValue == vv)
	{
		winningJob = v;
	}

	if (winningValue == vw)
	{
		winningJob = w;
	}

	if (winningValue == vx)
	{
		winningJob = x;
	}

	if (winningValue == vy)
	{
		winningJob = y;
	}

	if (winningValue == vz)
	{
		winningJob = z;
	}

	//Winning job has now been set.

	//Time to report the results to the user.

	cout << endl;
	cout << "Based on your answers, a good choice for a career would be " << winningJob << endl;
	cout << "It is a " << 100 * (winningValue / 26) << "% match!" << endl;
	cout << endl;

	//Program is finished 

	system("pause");

	return(0);

}
Your problem is the same as the issue illustrated here:

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main() {
    int a = 0;
    int b = a;
    a = 9;

    std::cout << "a: " << a << '\n';
    std::cout << "b: " << b << '\n';
}
a: 9
b: 0

b is not tied to a in any way, so changes to a are not reflected in the value of b. Likewise with your "v_" variables and your array. Changes made to those variables are not reflected in the array.
Oh, I see now. Thanks. So is there a way to fix this? I thought about it and I haven't came up with anything. Could I reorder my code or is it impossible with an array?
You could move the definition/initialization of the array to line 340 for a quick fix. Then it would be created/initialized with the values that have been previously obtained from the user and stored in those variables.
Okay, that sounds good, thanks. I'm looking at sorted maps right now as well. It seems like it might work too. Anyways, thanks for the help!
Topic archived. No new replies allowed.