There seems to be a logic error in my code (mayby)

Hello and best wishes to all. I'm trying to self-study C++ (yes, I'm an psycho, I know it :) ) and now I'm at the very beginning of the endless programming road (I'm an historian with the very poor math / logic / technical sciences knowledge). Trying to understand different objects and operations with them, I created the following program which is the very simple quiz with only 4 questions about the famous scientific district of Novosibirsk, Siberia - Akademgorodok (https://en.wikipedia.org/wiki/Akademgorodok). Starting with 0 points, user must receive 1 point after each right answer. Program works, all looks OK but only in case of 4, 3 or 1 right answers. When I input the first right answer and then two wrong answers follows(in any order) and one right, I receive zero points instead of two. What is wrong in my code? I understand that trying to "save" received points in "a", "b" and "c" is really a bad way because there are too many variants even after inputing the second answer (it may be: both answers right and so points = 2, right first and wrong second or wrong first and right second and so points = 1, both wrong and so points =0) but don't see the clear solution (it should be the one without arrays or other more complicated ways of C++ coding - I don't know it now). Please, can anybody help me? Great thanks in advance!

P.S. I use Visual Studio 2017 at Windows 10.

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
 #include "stdafx.h" 
#include <iostream> 
#include <string> 
using namespace std;


int main()
{
	string right1;
	string otvet; // user’s answer 1
	string right2;
	string otvet2; // user’s answer 2
	string right3;
	string right4;
	string otvet3; // user’s answer 3
	string otvet4; // user’s answer 4

	int points = 0; // starting points
	int a = 0;  // I tried to save first received point
	int b = 0; // I tried to save second received point
	int c = 0; // I tried to save third received point

	right1 = "M.A.Lavrentiev";
	right2 = "The Round Table";
	right3 = "The letter of 46";
	right4 = "Neil Armstrong";

	cout << "Here are the questions, try to answer all of them! " << endl;
	cout << endl;

	cout << "1. Who had honored kids as musketeers at ''Viktoria'' club during 60th? " << endl;
	cout << endl;
   
	getline(cin, otvet); 
	
	if (otvet.compare(right1) == 0) {
		cout << otvet << " Is correct, nicely done! " << '\n';
		points++; 
		a = points; 
	}
	else {
		cout << otvet << " Is not correct, try again " << '\n';
		points = 0; 

	}

	cout << endl;

	cout << "2. What had united the imaginary knights of one of the misty Albion rulers and the real colleagues of the great nuclear physic scholar from Novosibirsk? " << endl;
	cout << endl;
    
	getline(cin, otvet2);
	
	if (otvet2.compare(right2) == 0) {
		cout << otvet2 << " Is correct, nicely done! " << '\n';
		points++; 
		b = points; 
	}
	else {
		cout << otvet2 << " Is not correct, try again " << '\n';
		points = a; 
	}
	
	cout << endl;

	cout << "3. What unites USSR dissident movement, ''White Book'', ''Phoenix-66'', ''The case of the four'', ''Voice of America'' and Akademgorodok scientist of 60th? " << endl;
	cout << endl;
    
	getline(cin, otvet3);
	
	if (otvet3.compare(right3) == 0) {
		cout << otvet3 << " Is correct, nicely done! " << '\n';
		points++; 
		c = points; 
	}
	else {
		cout << otvet3 << " Is not correct, try again " << '\n';
		points = b; 
	}
	
	cout << endl;

	cout << "4. One of them had been named ''The Jazz ambassador'', other is worldwide known for his step and leap and also had visited Akademgorodok as the honorable guest at 60th - who is that other? " << endl;
	cout << endl;
	
	getline(cin, otvet4);

	if (otvet4.compare(right4) == 0) {
		cout << otvet4 << " Is correct, nicely done! " << '\n';
		points++; 
	}
	else {
		cout << otvet4 << " Is not correct, try again " << '\n';
		points = c; 
	}

	cout << endl;
	cout << "Your score  = " << points << endl;

	return 0;
}
Last edited on
You have made a very simple mistake. Don’t hit yourself too hard once you see it.

If I choose a correct answer for question 1, a is assigned a value other than zero.

If I then choose an incorrect answer for question 2, b is NOT assigned a value.

I then choose an incorrect answer for question 3, and points is set to the not-assigned-a-value value of b.

Finally, I choose a correct answer for question 4, points is already incorrect, so the maximum value of points is 0 + 1 (which is really b + 1).

To fix it, get rid of a, b, and c. They are not necessary. Only add to points when an answer is correct. Do nothing to points when an answer is incorrect. This way you are correctly counting only correct answers.

1
2
3
4
5
6
7
8
9
10
  if (correct) points++;
  else ...

  if (correct) points++;
  else ...

  if (correct) points++;
  else ...

  cout << "Number of correct answers is " << points << "\n";

Hope this helps. :O)
Last edited on
1) Did you notice you're repeating the same code for each question? That a good indication you need to use a function call.

2) If the use gets the answer wrong, you say "try again", but don't give the user the opportunity to do so.

3) There is a more intuitive way to compare strings.
 
if (otvet == right1) 


4) I don't get the point of a, b or c.

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
#include <iostream> 
#include <string> 
using namespace std;

bool ask_question (const string & q, const string & correct_ans)
{   string  ans;
    int attempts = 0;
    
    while (attempts < 3) 
    {   cout << q << ": ";
        getline (cin, ans);
        if (ans == correct_ans)
            return true;
        cout << "That's incorrect, try again" << endl;
    }        
    return false;
}
            
int main()
{   string right1;
	string right2;
	string right3;
	string right4;
    string question;   
	int points = 0; // starting points

	right1 = "M.A.Lavrentiev";
	right2 = "The Round Table";
	right3 = "The letter of 46";
	right4 = "Neil Armstrong";

	cout << "Here are the questions, try to answer all of them! " << endl;
	cout << endl;

	question = "1. Who had honored kids as musketeers at ''Viktoria'' club during 60th? ";
	if (ask_question (question, right1))
	    points++;

	question = "2. What had united the imaginary knights of one of the misty Albion rulers and the real colleagues of the great nuclear physic scholar from Novosibirsk? ";
	if (ask_question (question, right2))
	    points++;
    
	question = "3. What unites USSR dissident movement, ''White Book'', ''Phoenix-66'', ''The case of the four'', ''Voice of America'' and Akademgorodok scientist of 60th? ";
	if (ask_question (question, right3))
	    points++;

	question = "4. One of them had been named ''The Jazz ambassador'', other is worldwide known for his step and leap and also had visited Akademgorodok as the honorable guest at 60th - who is that other? ";
	if (ask_question (question, right4))
	    points++;

	cout << "Your score  = " << points << endl;
    system ("pause");
	return 0;
}
Last edited on
The warmest thanks to all!
Topic archived. No new replies allowed.