quiz score not displaying properly

I am working on a quiz for my class project but I am having some issues displaying the grand total. I created a class to set and ask the questions as well as tally the score along the way. When I get to the end and want to print the grand total it displays 0 regardless of the score. Im not sure what I am doing wrong but I think it might be that I set the variable total as a static int, I did this because I was getting an error of multiple definitions. Any pointers of where I should look in the textbook to solve this would be very helpful or is there a better way then the static int to solve the problem?

Quiz.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #pragma once
#include <iostream>
#include <string>
using namespace std;

static char guess; //Answer user inputs for question.
static int total;  //Total score.

class Question {
public:
	void setValues(string, string, string, string, string, char, int);
	void askQuestion();
	

private:
	string Question_Text;
	string answer_1;
	string answer_2;
	string answer_3;
	string answer_4;

	char correct_answer;
	int Question_Score;
};


Quiz.cpp
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
 #include "Quiz.h"
using namespace std;

void Question::setValues(std::string q, std::string a1, std::string a2, std::string a3, std::string a4, char ca, int pa)
{
	Question_Text = q;
	answer_1 = a1;
	answer_2 = a2;
	answer_3 = a3;
	answer_4 = a4;
	correct_answer = ca;
	Question_Score = pa;
}
//Format for possible answers displayed when program executes. 
void Question::askQuestion()
{
	cout << "\n";
	cout << Question_Text << "\n";
	cout << "a. " << answer_1 << "\n";
	cout << "b. " << answer_2 << "\n";
	cout << "c. " << answer_3 << "\n";
	cout << "d. " << answer_4 << "\n";
	cout << "\n";

	//User enters their answer.
	cout << "What is your answer?" << "\n";
	cin >> guess;
	//If their answer is correct, message is displayed and 4 points are added to their score.
	if (guess == correct_answer) {
		cout << "\n";
		cout << "Correct!" << "\n";
		total = total + Question_Score;
		cout << "\n";
		cout << "Your score is" << total;
		cout << "\n";
		cout << "Press enter to continue." << "\n";
		cin.get();
		cin.ignore();
	}
	else //If their answer is incorrect, message is displayed, no points added. 
		 //Correct answer displayed. 
	{
		cout << "\n";
		cout << "Sorry, you're wrong..." << "\n";
		cout << "The correct answer is " << correct_answer << "." << "\n";
		cout << "\n";
		cout << "Press enter to continue." << "\n";
		cin.get();
		cin.ignore();
	}
}


Main.cpp
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
 #include "Quiz.h"

using namespace std;

// function prototypes

void menu();
void selection();
void studySec1();
void studySec2();
void studySec2one();
void studySec2two();
void studySec2three();
void studySec2four();
void studySec3();
void lec1();
void lec2();

int main()
{            

	string name;
	int choice;    // for section selection

	//call main menu

	menu();

	cout << "Please enter your first name ";
	cin >> name;
	cout << "Thank you " << name << endl;

	//call selection

	selection();

	cin >> choice;

	if (choice == 1)
	{
		// call study section one

		studySec1();


		cout << "press 1 to continue or any other key to exit" << endl;
		cin >> choice;
		if (choice == 1)
		{
			// call study section two

			studySec2();

			cout << "Which loops would you like to learn about? " << endl;
			cout << " while (1)\n do-while(2)\n for (3)\n or the if /else (4)" << endl;
			cout << " or 5 to continue" << endl;
			cin >> choice;

			while (choice <= 5)
			{

				if (choice == 1)
				{
					studySec2one();

					cout << "please make another selection" << endl;
					cin >> choice;
				}
				else if (choice == 2)
				{
					studySec2two();

					cout << "please make another selection" << endl;
					cin >> choice;
				}
				else if (choice == 3)
				{
					studySec2three();

					cout << "please make another selection" << endl;
					cin >> choice;
				}
				else if (choice == 4)
				{
					studySec2four();

					cout << "please make another selection" << endl;
					cin >> choice;

				}

				else if (choice == 5)
				{
					studySec3();

					cout << " Thank you for studying!";
					break;
				}
			}

		}

	}
	else if (choice == 2)
	{
		// this is the lectures section
		// call lecture 1

		lec1();
	
		cout << "press 1 to continue or any other key to exit" << endl;
		cin >> choice;

		if (choice == 1) {
			lec2();
			cout << endl << endl;
			cout << "Thank you for using the lecture section";

		}

	}

	else if (choice == 3)
	{
		// this is the quiz section
		cout << "Press enter to start...\n";
		cin.get();


		//Ask if user wants to start quiz.
		string respond;
		cout << "Are you ready to start the quiz, " << name << "? Yes/No.\n";
		cin >> respond;

		//If user says yes, the quiz will start.
		if (respond == "Yes" || respond == "yes") {
			cout << "\n";
			cout << "Good luck!\n";
			cout << "\n";
			cout << "Press enter to continue.";
			cin.get();
			cin.ignore();

			//Instances of the questions. 
			Question q1;
			Question q2;
			Question q3;
			Question q4;
			Question q5;
			Question q6;
			Question q7;
			Question q8;
			Question q9;
			Question q10;

			q1.setValues("1. What command prints something to the screen?",
				"cin",
				"cout",
				"char",
				"print",
				'b',
				10);

			q2.setValues("2. Which of the following categories does C++ belong to?",
				"Operating System",
				"High-level programming language",
				"low-level programming language",
				"Compiler",
				'b',
				10);

			q3.setValues("3. Which command is correctly written?",
				"cout >>",
				"cin <<",
				"cout <>",
				"cin >>",
				'd',
				10);

			q4.setValues("4. What is this called, <iostream>?",
				"directive",
				"pre-processor directive",
				"file",
				"command",
				'b',
				10);

			q5.setValues("5. What punctuation ends most lines of code?",
				" . ",
				" ; ",
				" : ",
				" ' ",
				'b',
				10);

			q6.setValues("6. Which of the following is a correct comment?",
				"*/ Comments */",
				"** Comment **",
				"/* Comment */",
				"{ Comment }",
				'c',
				10);

			q7.setValues("7. Which of the following is the boolean operator for logical-and?",
				"&",
				"|",
				"&&",
				"|&",
				'c',
				10);

			q8.setValues("8. Which of the following shows the correct syntax for an if statement?",
				"if expression",
				"if {expression",
				"if (expression)",
				"expression if",
				'c',
				10);

			q9.setValues("9. How many times is a do while loop guaranteed to loop?",
				"1",
				"0",
				"Infinitely",
				"Variable",
				'a',
				10);

			q10.setValues("10. A subscipt is a(n) __________ .",
				"element in an array",
				"alternate name for an array",
				"number that represents the highest value stored within an array",
				"number that indicates the position of the particular item in an array",
				'd',
				10);

			//call questions
			q1.askQuestion();
			q2.askQuestion();
			q3.askQuestion();
			q4.askQuestion();
			q5.askQuestion();
			q6.askQuestion();
			q7.askQuestion();
			q8.askQuestion();
			q9.askQuestion();
			q10.askQuestion();

			cout << "your total is" << total << "out of 100";
		
			cout << endl;
			system("pause");
		}
		else {
			cout << endl;
			cout << "Goodbye!\n";
			cin.ignore();
			std::cin.get();
			return 0;
		}
	}
	else

		cout << "you have entered an invalid selection" << endl;
		return 0;
	
	system("pause");
	return 0;
}
Last edited on
Hello PermThesis,

While I look into your program, just wanted to let you know the closing code tag used a forward slash not a back slash.

You can edit your post and change that.

Andy
@PermThesis,
I honestly don't know for sure why your program always ends with a total of zero. My guess is that since you set the values of total inside Question, this automatically resets to zero when the Question goes out of scope i.e when ~Question(the destructor) is called.

However, i would suggest that each Question should know if it has been answered correctly so that after Question::askQuestion(), one can call Question::isCorrect() to get boolean If such boolean is true, we call Question::getQuestionScore (you currently lack this in your class) (see my suggestion below)

In addition, i would recommend that you have a constructor in Question class that can set all the values during the creation of the object. (see my suggestion below)

This is what i think you can do to fix your program

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
class Question {
public:
    Question(std::string q, std::string a1, std::string a2, std::string a3, std::string a4, char ca, int pa);
	void setValues(string, string, string, string, string, char, int);
	void askQuestion();
    
    int getQuestionScore() {return Question_Score;}
    void setQuestionScore(int sc){Question_Score  = sc;}
    bool isCorrect() {return correct;} // after each question, we call this
    void setCorrect(bool value){correct = value;}
private:
	string Question_Text;
	string answer_1;
	string answer_2;
	string answer_3;
	string answer_4;

	char correct_answer;
	int Question_Score;
       bool correct; // <== This is added
};
Question::Question(std::string q, std::string a1, std::string a2,
                   std::string a3, std::string a4, char ca, int pa)
    :Quesiont_Text(q), answer_1(a1), answer_2(a2), answer_3(a3), 
        answer_4(a4), correct_answer(ca), Question_Score(pa), correct(false)
        {}

void Question::askQuestion()
{
	// display question and options

	//User enters their answer.
	cout << "What is your answer?" << "\n";
	cin >> guess;
	//If their answer is correct, message is displayed and 4 points are added to their score.
	if (guess == correct_answer) {
		cout << "\n";
        correct = true; // or setScore(1);
		// the res is "unnecssary"
	}
	else //If their answer is incorrect, message is displayed, no points added. 
		 //Correct answer displayed. 
	{
		cout << "\n";
		cout << "Sorry, you're wrong..." << "\n";
		cout << "The correct answer is " << correct_answer << "." << "\n";
		
	}
}

// since we ask this many times, just make a function
void waitForKey()
{
    cout << "\n";
	cout << "Press enter to continue." << "\n";
	cin.get();
	cin.ignore();
}
    
int main()
{
    int total = 0;
    std::cout << "Hello, world!\n";
    
    // use Question(std::string q, std::string a1, std::string a2, std::string a3, std::string a4, char ca, int pa);
    // to instantiate a question directly
    
    Question q1("1. What command prints something to the screen?","cin","cout","char","print",'b',10);
    
    // ask the question
    q1.askQuestion(); // from inside here, we know if the user answered correctly
    
    // check if the answer is correct, get the question score and add to total
    if(q1.isCorrect()){
        total += q1.getQuestionScore();
    }
   
    waitForKey();
    
    // if you want to use setValues, here you go
    Question q2;
    q2.setValues("the values", .....);
    q2.askQuestion(); 
    if(q2.isCorrect()){
        total += q2.getQuestionScore();
    }
    
    waitForKey();
    
    Question q3;
    
    // You could also do like this
    /*
        NB: #include<vector>
        
        std::vector<Question> questions;
        Question q1(questionText, ans1, answ2, answ3, answ4, correctAnswer, points);
        questions.push_back(q1);
        Question q2(....);
        questions.push_back(q2);
        
        // load others
        
        // ask the questions
        
        int total{0}; // or  int total = 0 or int total{} 
        for(Question question: questions)
        {
            question.askQuestion();
            if(question.isCorrect()){
                total += question.getQuestionScore();
            }
            waitForKey();
        }
        
        cout << "Your total score is " << total << endl;
      
    */
    
    return 0;
}


Just a side note:
Be consistent with your variable naming conventions {camelCase or snake_case}

Hope this helps.
Last edited on
Hello PermThesis,

I managed to put enough together to get the program to run.

The reason, I believe, that you had to make the two variables "guess" and "total" static is because you defined them in the header file. Then when you include the header file in two different ".cpp" files the compiler sees it as defining the variables twice.

The variable "guess" should be defined in the class function "askQuestion" where it is the only place used. I changed this and it works.

The variable "total" should be defined in "main" and passed by reference to the class function "askQuestion" by reference. The prototype and definition would be: void askQuestion(int& total);. The function Call would be: q1.askQuestion(total);.

When I made these changes the program worked as it should.

I had to make a "menu" function to test the program and offer this suggestion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int menu()
{
	int choice{};

	do
	{
		std::cout << "\n 1. Menu Choice 1.\n";
		std::cout << "\n 3. Quiz Section.\n";
		std::cout << "\n 4. Exit";
		std::cout << "\n Enter Choice: ";
		std::cin >> choice;

		if (choice < 1 || choice > 4)
			std::cout << "\n  Invalid choice. Try again.\n" << std::endl;
	} while (choice < 1 || choice > 4);	

	return choice;
}

By doing the menu this way you only return a valid choice back to "main". You can adjust the error message as you like.

As I went through the quiz I noticed a problem:

4. What is this called, <iostream>?
a. directive
b. pre-processor directive
c. file
d. command

10. A subscipt is a(n) __________ .
a. element in an array
b. alternate name for an array
c. number that represents the highest value stored within an array
d. number that indicates the position of the particular item in an array

What is your answer?
d

Correct!

Your score is: 100

Press enter to continue.

your total is 100 out of 100

Press any key to continue . . .


For question 4 "<iostream>" is a header file not a pre-processor directive. A pre-processor directive is something thet starts with "#" like #include or #define .

Question 10 is there to show the final output.

Your original "cout" statement of cout << "Your score is" << total; I changed to
cout << "Your score is: " << total;.

After that adding a few blank lines, I feel, makes the output easier to read.

In the function "askQuestion" the line: cout << "What is your answer?" << "\n"; works, but I kind of like it this way: cout << "What is your answer? ";. By adding a space after the "?" and leaving out the "\n" puts the "cin" statement on the same line. It seems more natural to me this way.

Hope that helps,

Andy
Thank you it did help, I corrected the mistakes with total and guess. everything works well I also corrected the inaccurate questions and the input verification. I appreciate all the help and advice
Topic archived. No new replies allowed.