Program Not Responding Correctly - Guidance Needed

I am trying to fine-tune a Quiz game that asks multiple-choice questions. (EDITED) I was able to figure out a few questions I had, but I am still stuck on the rest.

REQUIREMENTS:
1) 2-Round Game (Warmup Round + Full Game)
2) Multiple Choice Questions

WHAT I'VE COMPLETED SO FAR:
1) Questions are stored in a text file
2) Scores are stored in a text file
3) Full game of 10 questions pulled from text file
4) High Score stored in a text file

ISSUES:
1) No Warmup Round created
4) No reset score created.

QUESTIONS:
1) How can I add a WARMUP Round to my code that will ask the player 3 simple questions and if the user gets 2 of 3 questions correct, they move onto the full game. If they get 2 of the 3 questions wrong, they get placed back at the Main Menu?
2) How can I add a RESET SCORE function that erases the high score from the text file?

I figured out the highscore issue (it was my antivirus not allowing my highscore text file to be updated.)

UPDATED 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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <time.h>

using namespace std;

void help();
void display_score();
int highest_score();
void update_score(string name, int score);

int main() {


	char start_option;
	string name;
	cout << "Enter your name" << endl;
	cin >> name;



Home:
	system("CLS");
	cout << "\t      C++ PROGRAM QUIZ GAME" << endl;
	cout << "    _________________________________________" << endl;
	cout << "\t\t    WELCOME " << endl;
	cout << "\t\t       to" << endl;
	cout << "\t\t    THE GAME" << endl;
	cout << "    _________________________________________\n";
	cout << "    _________________________________________\n";
	cout << "\t   BECOME A MILLIONAIRE!!!!!!!\n";
	cout << "    _________________________________________\n";
	cout << "    _________________________________________\n";

	cout << "\t > Press S to start the game\n";
	cout << "\t > Press V to view the highest score\n";
	cout << "\t > Press R to reset score\n";
	cout << "\t > Press H for help\n";
	cout << "\t > Press Q to quit\n";
	cout << "    _________________________________________\n";

	cin >> start_option;
	start_option = toupper(start_option);

	
	if (start_option == 'V') {
		display_score();
		goto Home;
	}

	else if (start_option == 'H') {
		help();
		goto Home;
	}

	else if (start_option == 'Q') {
		return 0;
	}

	else if (start_option == 'S') {


		srand(time(0));
		vector<int> check_repeat(30, 0);


		string question;
		string answer;
		string category;
		char user_answer;
		int sports = 0;
		int gk = 0;
		int current_affairs = 0;


		int counter = 0;
		int score = 0;
		


		while (counter < 10) {

			int line_counter = 0;
			int rand_line = rand() % 24;
			if (check_repeat[rand_line] == 0) {

				check_repeat[rand_line] = 1;


				ifstream file("quiz_bank.txt");
				while (rand_line != line_counter) {
					getline(file, question);
					getline(file, answer);
					getline(file, category);
					line_counter++;
				}

				getline(file, question);
				getline(file, answer);
				getline(file, category);

				system("CLS");
				cout << "Question Category: " << category << "      " << "Question Number: " << counter + 1 << endl;
				cout << question << endl;
				cin >> user_answer;
				user_answer = toupper(user_answer);
				if (answer[0] == user_answer) {
					system("CLS");
					cout << "\t\t\t  That is correct!" << endl;
					score += 1;
					system("pause");
					cin.get();
					if (category == "SPORTS")
						sports++;
					else if (category == "GK")
						gk++;
					else
						current_affairs++;
				}
				else {
					system("CLS");
					cout << "Question Category: " << category << "      " << "Question Number: " << counter + 1 << endl;
					cout << question << endl;
					cout << "Correct Answer : " << answer << endl;
					system("pause");
				}


				counter++;
			}

		}
		system("CLS");
		cout << "\t\tCongratulations . You have completed the quiz!!\n\n";
		if (score >= highest_score()) {
			cout << "\t\tNEW RECORD!!\n" << endl;
			update_score(name, score);
		}
		cout << "Your Score is: " << score << endl;
		cout << "\n\n";
		cout << "Category Wise Score -:" << endl;
		cout << "\tSports  : " << sports * 1 << endl;
		cout << "\tGK  : " << gk * 1 << endl;
		cout << "\tCurrent Affairs  : " << current_affairs * 1 << endl;

		cout << "To play again press S to Quit press any other Key " << endl;
		cin >> start_option;
		start_option = toupper(start_option);
		if (start_option == 'S') {
			goto Home;
		}
		else
			exit(1);


	}
	else
		exit(1);


	return 0;

}


void display_score()

{
	ifstream file("highest_c++_score.txt");
	string name;
	int score;

	file >> name >> score;
	system("CLS");
	cout << name << " has the highest Score of " << score << endl;

	system("pause");

}

int highest_score()
{
	int score;
	string name;
	ifstream file("highest_c++_score.txt");
	file >> name >> score;
	return score;
}

void help()
{

	cout << "THERE ARE 2 ROUNDS IN THIS GAME.\n";
	cout << "[WARM UP ROUND]- You will be asked 3 questions and you must answer 2 of the 3 correctly to advance.\n";
	cout << "[MAIN ROUND]- You will be asked ten questions. Each question is worth $ 100,000.\n";
	cout << "\t      when you reach ONE MILLION, YOU WIN!!\n\n";

	system("pause");
}

void update_score(string name, int score)
{
	ofstream file("highest_c++_score.txt");
	file << name << " " << score;

}
Last edited on
Using goto is STRONGLY discouraged, use a while/do-while loop instead.

What is the difference between a warm-up round and a full game?
@Furry Guy-

Why is the "goto" discouraged?

There really isn't a difference between the WARMUP and FULL GAME, however, the player MUST complete the warmup round (and successfully pass) in order to go into the Full game.

I just don't know how I could implement the warmup round into my current code.

gotos are bad because they lead to spaghetti code.

If you're going to use the weak C library rand() routines, you should move the call to srand() above line 25. Better that you use the C++ random routines in <random>.
Do not call srand() multiple times. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Where are you getting the questions for your warmup round? Are they coming from the text file also?

Part of the problem of why adding the warmup round is difficult is that all your code for the main round is in main. Better that it be in a function. Then your code in main would look something like this:
1
2
3
4
5
6
7
8
bool warmup_round ();    //  returns pass or fail

if (! warmup_round()) 
{  cout << "You failed the warmup round.  Goodbye." << endl;
    return 0;  //  Exit the game
}
main_round ();
...


I would discourage you from having a fixed number (24) questions in the text file. Better that you use a vector and pick a random question from the vector based on the number of questions in the vector. This lets you put as many questions as you like in the text file.

Last edited on
So the difference between a warm-up round and and a full game round is nothing other than what you call it.

You simply have two game rounds, requiring the first round to be won before proceeding to the 2nd. Having the code running each round is in a function, you call the function twice. Returning success or failure.

If the first round ends in failure, no 2nd round.

How you label each round is icing on the cake. Pretty and fattening.

You could even run calling the game round function in a loop so you can (if wanted) extend the number of rounds played, etc.

With loops no need for goto.

There are times (very few) goto can be useful, this isn't one of those times.

https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto
@abstractionAnon-

--That makes sense to the goto.

--Thank you for the srand() suggestion! That made my questions repeat far less than they were.

--For the warmup, I was going to make it simplified by just asking Y/N questions that don't change.
EX: Do dogs bark? Enter your answer (Y/N).

If user gets 2 answers wrong from these 3 questions then they are told they failed and returned to main menu.
If user gets 2 answers correct then it starts the FULL game.

-- Where would I implement that bool code at? Would it be after the "if startup==S"?


Would it be after the "if startup==S"?


Yes.

If you were so inclined you could make warmup_round and main_round into a single function that accepts an argument as to the number of questions to ask (3 or 10). If you did that, I would make it an int function that returns the number of questions answered correctly.
Possibly a little late, but before coding you really should first design the program. Then once you have the design then you code the program from the design. Code small sections at a time and compile and test frequently. Don't write 200+ lines and then start to compile and test. And then ask questions about how to do something. How should be part of the design before coding is even thought about.

My late programming professor always taught for time - 50% design, 30 - 40% code and the remaining 10 - 20% for testing etc.
Consider using a switch instead of multiple if/else tests. Why keep opening and reading quiz_bank.txt? Why not have a suitable struct and read the file once into a vector of that struct?

You could have warm-up round as a special category and for the warm-up round only ask questions in this category and for the follow-up round(s) ignore this category. You then need only a function for a quiz round with appropriate function params and return.

Don't make any assumptions re the name of category (except perhaps for warm-up). Don't hardcode these names in the code. The category names should come from the file. To change the quiz/category etc all that should be needed is a change to the data file. As I mentioned above, design first then code. Don't design as you code.
Topic archived. No new replies allowed.