Very strange unexpected output

Sorry, this code is long, but it is redundant and very readable. It was building off a past post of mine, but it won't let me post because the topic is too long. Anyways, the problem revolves around the qy variable, which counts the number of "yes" responses. However, when the final percentage is calculated using winningValue and qy, it makes a number which could only come from a value of 26, even though there are only 20 questions. This leads me to think qy may have accidentally been somehow linked with the a,b,c variables, or the va,vb,vc variables. Adding to the strangeness, I added a "cout << qy << endl" to the end, but it seems to simply skip over that line. If anybody is willing to read it, and give me any ideas, it would be appreciated, its been on my mind too long. Can't fit the code, so it will be in the reply below.
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#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, qy = 0;

	//Occupation ratings initalized ^^

	std::string winningJob;
	double winningValue = 1;

	//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;
				cout << endl;
				break;
			case 2:
				cout << "Are you creative?" << endl;
				std::cin >> a2;
				cout << endl;
				break;
			case 3:
				cout << "Do you like to exercise?" << endl;
				std::cin >> a3;
				cout << endl;
				break;
			case 4:
				cout << "Are you good at math?" << endl;
				std::cin >> a4;
				cout << endl;
				break;
			case 5:
				cout << "Do you like to work with your hands?" << endl;
				std::cin >> a5;
				cout << endl;
				break;
			case 6:
				cout << "Are you good with kids?" << endl;
				std::cin >> a6;
				cout << endl;
				break;
			case 7:
				cout << "Are you competitive?" << endl;
				std::cin >> a7;
				cout << endl;
				break;
			case 8:
				cout << "Do you value justice?" << endl;
				std::cin >> a8;
				cout << endl;
				break;
			case 9:
				cout << "Are you good with technology?" << endl;
				std::cin >> a9;
				cout << endl;
				break;
			case 10:
				cout << "Are you easily afraid?" << endl;
				std::cin >> a10;
				cout << endl;
				break;
			case 11:
				cout << "Do you like to work with moving parts?" << endl;
				std::cin >> a11;
				cout << endl;
				break;
			case 12:
				cout << "Are you often kind or generous to others?" << endl;
				std::cin >> a12;
				cout << endl;
				break;
			case 13:
				cout << "Do you like to solve riddles or puzzles?" << endl;
				std::cin >> a13;
				cout << endl;
				break;
			case 14:
				cout << "Is money important to you?" << endl;
				std::cin >> a14;
				cout << endl;
				break;
			case 15:
				cout << "Do you like to be outside?" << endl;
				std::cin >> a15;
				cout << endl;
				break;
			case 16:
				cout << "Do you like to tell stories?" << endl;
				std::cin >> a16;
				cout << endl;
				break;
			case 17:
				cout << "Do you often feel the need to express yourself?" << endl;
				std::cin >> a17;
				cout << endl;
				break;
			case 18:
				cout << "Are you good at persuasion?" << endl;
				std::cin >> a18;
				cout << endl;
				break;
			case 19:
				cout << "Do you like animals?" << endl;
				std::cin >> a19;
				cout << endl;
				break;
			case 20:
				cout << "Do you get angry easily?" << endl;
				std::cin >> a20;
				cout << endl;
				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;
		++qy;
	}
	
	if (a2 == "yes")
	{
		++vl;
		++vm;
		++vv;
		++qy;
	}
	
	if (a3 == "yes")
	{
		++vn;
		++vy;
		++qy;
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

	//Time to find the largest value.


	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)
	{
		cout << array[count] << endl;

		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 / qy) << "% match!" << endl;
	cout << qy << endl;

	//Program is finished 

	system("pause");

	return(0);

}
What is the purpose of this program?

1
2
3
4
5
6
7
8
9
10
11
12
if (a1 == "yes")
{
		++vb;
		++vd;
		++vg;
		++vi;
		++vl;
		++vn;
		++vo;
		++vy;
		++qy;
}


Can you explain what the variables vb, vd, vq, vi, vn, vo, vy, qy are?
You have a misplaced comma and semi-colon here (line 61):
for (int qs = 1; qs <= 20, ++qs;)
As a result, your for loop is infinite.

Should be:
for (int qs=1; qs <=20; ++qs)

Why do you have the loop at all? Lines 61-170 are the same as:
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
    cout << "Do you like to work with other people?" << endl;
    std::cin >> a1;
    cout << endl;

    cout << "Are you creative?" << endl;
    std::cin >> a2;
    cout << endl;

    cout << "Do you like to exercise?" << endl;
    std::cin >> a3;
    cout << endl;

    cout << "Are you good at math?" << endl;
    std::cin >> a4;
    cout << endl;

    cout << "Do you like to work with your hands?" << endl;
    std::cin >> a5;
    cout << endl;

    cout << "Are you good with kids?" << endl;
    std::cin >> a6;
    cout << endl;

    cout << "Are you competitive?" << endl;
    std::cin >> a7;
    cout << endl;

    cout << "Do you value justice?" << endl;
    std::cin >> a8;
    cout << endl;

    cout << "Are you good with technology?" << endl;
    std::cin >> a9;
    cout << endl;

    cout << "Are you easily afraid?" << endl;
    std::cin >> a10;
    cout << endl;

    cout << "Do you like to work with moving parts?" << endl;
    std::cin >> a11;
    cout << endl;

    cout << "Are you often kind or generous to others?" << endl;
    std::cin >> a12;
    cout << endl;

    cout << "Do you like to solve riddles or puzzles?" << endl;
    std::cin >> a13;
    cout << endl;

    cout << "Is money important to you?" << endl;
    std::cin >> a14;
    cout << endl;

    cout << "Do you like to be outside?" << endl;
    std::cin >> a15;
    cout << endl;

    cout << "Do you like to tell stories?" << endl;
    std::cin >> a16;
    cout << endl;

    cout << "Do you often feel the need to express yourself?" << endl;
    std::cin >> a17;
    cout << endl;

    cout << "Are you good at persuasion?" << endl;
    std::cin >> a18;
    cout << endl;

    cout << "Do you like animals?" << endl;
    std::cin >> a19;
    cout << endl;

    cout << "Do you get angry easily?" << endl;
    std::cin >> a20;
    cout << endl;


Any time you find yourself writing lots of code that is almost identical you should consider factoring the code. In this case, you should have data that represents the questions, the careers, and how the questions affect the potential careers. Then the code just presents the questions and computes the best career. Ideally, the data should all go in a configuration file so you can add careers and questions without having to recompile the code.
closed account (48T7M4Gy)
This is a rough start to how you can save a lot of duplication. there are a couple of aspects to add which are not too complicated:
1. Error checking answers to force only y or n (Y or N) answers
2. Rewrite the business rules on career ratings.

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

int main ()
{
    std::string career[] =
    {
        "doctor","teacher","scientist","zookeeper","veterinarian","firefighter.",
        "therapist","babysitter","construction worker","mechanic","car salesman.",
        "filmmaker","artist","sports player","businessman","computer programmer.",
        "engineer","mathematician","farmer","astronaut","pilot","author.",
        "policeman","detective","physical therapist","lawyer."
    };
    size_t no_careers = sizeof(career)/sizeof(std::string);
    int *career_rating = new int[no_careers]; // OR USE HARD CODED CONST ARRAY SIZE
    
    std::string question[] =
    {
        "Do you like to work with other people?",
        "Are you creative?",
        "Do you like to exercise?",
        "Are you good at math?",
        "Do you like to work with your hands?",
        "Are you good with kids?",
        "Are you competitive?",
        "Do you value justice?",
        "Are you good with technology?",
        "Are you easily afraid?",
        "Do you like to work with moving parts?",
        "Are you often kind or generous to others?",
        "Do you like to solve riddles or puzzles?",
        "Is money important to you?",
        "Do you like to be outside?",
        "Do you like to tell stories?",
        "Do you often feel the need to express yourself?",
        "Are you good at persuasion?",
        "Do you like animals?",
        "Do you get angry easily?"
    };
    size_t no_questions = sizeof(question)/sizeof(std::string);
    
    std::string winning_job;
    double winning_value = 1;
    
    char* answer = new char[no_questions]; // OR USE HARD CODED CONST ARRAY SIZE
    
    std::cout
    << "Don't know what you want for a career?\n"
    << "Take this short quiz and find out what's best for you.\n"
    << "Answer Y/N \n";
    
    //Ask the questions
    for(int i = 0; i < no_questions; i++)
    {
        std::cout << "Question: " << i + 1 << " - " << question[i] << ": ";
        std::cin >> answer[i];
    }
    
    
    //Time to increase each rating based on answers
    
    
    //Winning job has now been set.
    
    //Time to report the results to the user.
    //std::cout << "Based on your answers, a good choice for a career would be " << winningJob << '\n';
    //std::cout << "It is a " << 100 * (winning_value / qy) << "% match!" << ,\n';
    //std::cout << qy << '\n';
    
    //Program is finished
    delete [] career_rating;
    delete [] answer;
    
    return(0);
}


PS You can use vectors and save a lot of bother. I'd be surprised if the final program this way exceeds 120 lines so only 30 0r 40 to go tops. :)
Last edited on
Topic archived. No new replies allowed.