I cannot figure this out

*Disclaimer* -In no way am I remotely good at programming. The following code has taken me four weeks to figure out, and I'm at my wit's end. I just need advice/guidance on how to do the following:

--Add a series of questions split into rounds (1 and 2).
--If the player fails round 1, they won't advance to 2.

I've, for the most part been able to get a handle on functions listed and being able to "beautify" it.

ANY help/advice/guidance will be appreciated!

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

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>

using namespace std;

int scores = 0;

void edit_score()
{
scores++;
return;
}
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 ten thousand points($$).\n";
cout << "\t when you reach ONE MILLION points($$$$), YOU WIN!!\n";
}
void reset_score() // reset score funtion
{
scores = 0;
return;
}
void show_record(string name) // show_record function
{
cout << name << " has scored the highest score of " << scores * 100000 << endl;
}
int main()
{
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";

char ch;
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";

while (cin >> ch)
{
if (ch == 'Q')
{
cout << "See you next time!\n";
return 0; // quit the game if character is 'Q'
}
if (ch == 'V')
{
void show_score(); // show the user his/her score
}

if (ch == 'S') // start the game if user enters character s;
{
cout << "Who is playing today?" << endl;
cin >> name; // take the user name as input
}
if (ch == 'R') // reset the score
{
reset_score();
cout << "Your score has been reset!\n";
}
if (ch == 'H') // call the help function when user enters the 'H'
{
help();
}
}
return 0;
}
Last edited on
Lots of things to note first:

1. You shouldn't use headers that end in .h. For example, you have <string.h>, you should instead use <string>. If you find there's a C string function you can't live without, then use <cstring>.


2. In your (ch == 'S') if statement, you try to read into a variable "name" which does not exist - this code should not compile. I assume you wanted to create a string called name but forgot.


3. The while loop doesn't make sense - you keep looping on user input but you don't output their options again. The user might play a long game then see the program isn't closing - not knowing it's waiting for input!



Alright. Before you can ask questions and see if they user is wrong, you need to have questions and answers!

You can create String array and an Integer array. The string array can contain the questions, the Integer array can contain the answers. The way this normally works is if you ask the question in string array index 3, then the answer will be found in the integer array index 3.



If you don't know how to use arrays, I recommend you try to learn more about programming and then return to this program LATER when you're better equipped.


However, here's your code with the issues I presented earlier addressed, with proper indentation, and an example of how you may want to ask the user questions:


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
//

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int scores = 0;

void edit_score()
{
	scores++;
	return;
}

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 ten thousand points($$).\n";
	cout << "\t when you reach ONE MILLION points($$$$), YOU WIN!!\n";
}

void reset_score() // reset score funtion
{
	scores = 0;
	return;
}

void show_record(string name) // show_record function
{
	cout << name << " has scored the highest score of " << scores * 100000 << endl;
}

// Bool so it can return if the player won or lost
bool playGame()
{
	cout << "Does paper come from trees?\n";
	cout << "A. Yes\nB. No\n\nYour answer: ";

	char in;
	cin >> in;

	if (in == 'B')
	{
		cout << "That's wrong... Sorry!";
		return false;
	}

	cout << "Correct! Round 2:\n\n";

	//Ask more questions..


	//If they get to the end!
	return true;
}

int main()
{
	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";

	char ch;
	string name;

	while (true)
	{
		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";

		cin >> ch;

		if (ch == 'Q')
		{
			cout << "See you next time!\n";
			return 0; // quit the game if character is 'Q'
		}
		if (ch == 'V')
		{
			void show_score(); // show the user his/her score
		}

		if (ch == 'S') // start the game if user enters character s;
		{
			cout << "Who is playing today?" << endl;
			cin >> name; // take the user name as input

			bool won = playGame();

			if (won) cout << "Congrats! You won!\n";
			else cout << "Sorry, you lost!\n";
		}
		if (ch == 'R') // reset the score
		{
			reset_score();
			cout << "Your score has been reset!\n";
		}
		if (ch == 'H') // call the help function when user enters the 'H'
		{
			help();
		}
	}
	return 0;
}
}
Topic archived. No new replies allowed.