Trivia Program

Hello all. I am writing a trivia program. The code is finished, I'm trying to call the "questionAnswers" function, but I have these last eight errors. I've been looking at this for so long, and cannot figure it out. Please help.

Error 1 error C2065: 'answer1' : undeclared identifier c:\users\michael\documents\visual studio 2012\projects\trivialgame\trivialgame\main.cpp 117 1 TrivialGame
Error 2 error C2065: 'answer2' : undeclared identifier c:\users\michael\documents\visual studio 2012\projects\trivialgame\trivialgame\main.cpp 117 1 TrivialGame
Error 3 error C2065: 'answer3' : undeclared identifier c:\users\michael\documents\visual studio 2012\projects\trivialgame\trivialgame\main.cpp 117 1 TrivialGame
Error 4 error C2065: 'answer4' : undeclared identifier c:\users\michael\documents\visual studio 2012\projects\trivialgame\trivialgame\main.cpp 117 1 TrivialGame
5 IntelliSense: identifier "answer1" is undefined c:\Users\Michael\Documents\Visual Studio 2012\Projects\TrivialGame\TrivialGame\Main.cpp 117 34 TrivialGame
6 IntelliSense: identifier "answer2" is undefined c:\Users\Michael\Documents\Visual Studio 2012\Projects\TrivialGame\TrivialGame\Main.cpp 117 43 TrivialGame
7 IntelliSense: identifier "answer3" is undefined c:\Users\Michael\Documents\Visual Studio 2012\Projects\TrivialGame\TrivialGame\Main.cpp 117 52 TrivialGame
8 IntelliSense: identifier "answer4" is undefined c:\Users\Michael\Documents\Visual Studio 2012\Projects\TrivialGame\TrivialGame\Main.cpp 117 61 TrivialGame

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
#include <iomanip>
#include "TrivialGame.h"

void questionAnswers(question i[], int, string, string, string, string);

int main()
{
	char response, start;
	
	do
	{
		question Game[10];
		int count = 0;
		int player1Choice, player2Choice;					// Holds the player's answers.
		int player1Points = 0, player2Points = 0;			// Holds the player's total points.

		// Describe the game to the user.
		cout << "Welcome to:" << endl << endl;
		cout << "-------------the FANTABULOUS TRIVIA GAME!-------------" << endl << endl;
		cout << "This is the TWO PLAYER GAME where you will be challenged by TRIVIA QUESTIONS!" << endl;
		cout << "You will be asked a total of 10 questions." << endl;
		cout << "First one to get them all right with the most points WINS!" << endl << endl;

		// Ask the user if they would like to start the game.
		cout << "Ready to start?" << endl;
		cout << "Please type 'Y' or 'y' for YES." << endl;
		cout << "Alternatively type 'N' or 'n' for NO." << endl;
		cin >> start;

		// If loop to start the game.
		if(start == 'Y' || start == 'y')
		{
			// First question.
			Game[0].setQuestions("What is the capital of New Jersey?");
				Game[0].setAnswer1("1. Alaska");
				Game[0].setAnswer2("2. Toronto");
				Game[0].setAnswer3("3. applesauce");
				Game[0].setAnswer4("4. Trenton");
				Game[0].setCorrectAnswer(4);

			// Second question.
			Game[1].setQuestions("What is America's favorite past time?");
				Game[1].setAnswer1("1. War");
				Game[1].setAnswer2("2. Football");
				Game[1].setAnswer3("3. Baseball");
				Game[1].setAnswer4("4. Soccer");
				Game[1].setCorrectAnswer(3);

			// Third question.
			Game[2].setQuestions("What makes the grass green?");
				Game[2].setAnswer1("1. Green");
				Game[2].setAnswer2("2. Chlorophyll");
				Game[2].setAnswer3("3. Germs");
				Game[2].setAnswer4("4. Yellow");
				Game[2].setCorrectAnswer(2);

			// Fourth question.
			Game[3].setQuestions("What year did Germany invade Poland at the start of WW2?");
				Game[3].setAnswer1("1. 1939");
				Game[3].setAnswer2("2. 1856");
				Game[3].setAnswer3("3. 1400");
				Game[3].setAnswer4("4. 1946");
				Game[3].setCorrectAnswer(1);

			// Fifth question.
			Game[4].setQuestions("What is the eight planet of the solar system?");
				Game[4].setAnswer1("1. Mars");
				Game[4].setAnswer2("2. Jupiter");
				Game[4].setAnswer3("3. Poseidon");
				Game[4].setAnswer4("4. Neptune");
				Game[4].setCorrectAnswer(4);

			// Sixth question.
			Game[5].setQuestions("Which famous superhero was from the planet Krypton?");
				Game[5].setAnswer1("1. Batman");
				Game[5].setAnswer2("2. Deadpool");
				Game[5].setAnswer3("3. Superman");
				Game[5].setAnswer4("4. Wolverine");
				Game[5].setCorrectAnswer(3);

			// Seventh question.
			Game[6].setQuestions("What 2010 film had to do with entering people's dreams?");
				Game[6].setAnswer1("1. Shutter Island");
				Game[6].setAnswer2("2. Hugo");
				Game[6].setAnswer3("3. The Matrix");
				Game[6].setAnswer4("4. Inception");
				Game[6].setCorrectAnswer(4);

			// Eighth question.
			Game[7].setQuestions("What is 44 divided by 4");
				Game[7].setAnswer1("1. 11");
				Game[7].setAnswer2("2. 89");
				Game[7].setAnswer3("3. 6");
				Game[7].setAnswer4("4. 7");
				Game[7].setCorrectAnswer(1);

			// Ninth question.
			Game[8].setQuestions("When is Independence Day?");
				Game[8].setAnswer1("1. February 27");
				Game[8].setAnswer2("2. July 4");
				Game[8].setAnswer3("3. May 5");
				Game[8].setAnswer4("4. July 2");
				Game[8].setCorrectAnswer(2);\

			// Tenth question.
			Game[9].setQuestions("What is the Law of Conservation of Energy?");
				Game[9].setAnswer1("1. That energy can be created, but not destroyed.");
				Game[9].setAnswer2("2. That energy is simply an illsuion.");
				Game[9].setAnswer3("3. That energy cannot be created nor destroyed.");
				Game[9].setAnswer4("4. That energy cannot be created, but can be destroyed.");
				Game[9].setCorrectAnswer(2);

			// While loop for the actual game.
			while(count < 9)
			{
				cout << setw(10) << "Question #" << (count + 1) << "." << endl;
				questionAnswers(Game, count, answer1, answer2, answer3, answer4);

				cout << "Player 1 Answer: ";
				cin >> player1Choice;

				cout << "Player 2 Answer: ";
				cin >> player2Choice;

				// If Player 1 is right, they get a point.
				if(Game[count].getCorrectAnswer() == player1Choice)
				{
					player1Points++;
				}

				// If Player 2 is right, they get a point.
				if(Game[count].getCorrectAnswer() == player2Choice)
				{
					player2Points++;
				}
				count++;
			}

			// If loops to determine who wins.
			if (player1Points > player2Points)
			{
				cout << "Player 1 WINS!" << endl;
				cout << "Congratulations Player 1!!" << endl;
			}

			if (player1Points < player2Points)
			{
				cout << "Player 2 WINS!" << endl;
				cout << "Congratulations Player 2!!" << endl;
			}

			if (player1Points == player2Points)
			{
				cout << "Player 1 and Player 2 are now tied!" << endl;
				cout << "You both LOST!!" << endl;
			}

		}
		else if(start == 'n' || start == 'N')
		{
				cout << "Thanks for your participation in the FANTABULUS TRIVIA GAME!" << endl;
		}

		// Ask the user if they would like to rerun the program.
		cout << endl << "\nWould you like to run the program again?" << endl;
		cin >> response;
		cout << endl;
	} 
	
	while (response == 'y' || response == 'Y');

	return 0;
}

void questionAnswers(question i[], int count, string answer1, string answer2, string answer3, string answer4)
{
	cout << i[count].getQuestions() << endl;
	cout << i[count].getAnswer1(answer1) << endl;
	cout << i[count].getAnswer2(answer2) << endl;
	cout << i[count].getAnswer3(answer3) << endl;
	cout << i[count].getAnswer4(answer4) << endl << endl;
}
Not seeing where answer1, answer2, answer3 and answer4 are defined in main. Error messages indicate compiler is having problems finding them as well.

questionAnswers(Game, count, answer1, answer2, answer3, answer4);

They are defined in my ".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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
using namespace std;

#ifndef	QUESTION_H
#define QUESTION_H

class question
{
private:
	string questions, answer1, answer2, answer3, answer4;
	int correctAnswer;

public:
	question();
	question(string, string, string, string, string, int);		// Constructor

	// "Set" and "Get" the Questions function.
	void setQuestions(string);
	string getQuestions() const;

	// "Set" and "Get" the Answer1 function.
	void setAnswer1(string);
	string getAnswer1(string) const;

	// "Set" and "Get" the Answer2 function.
	void setAnswer2(string);
	string getAnswer2(string) const;

	// "Set" and "Get" the Answer3 function.
	void setAnswer3(string);
	string getAnswer3(string) const;

	// "Set" and "Get" the Answer4 function.
	void setAnswer4(string);
	string getAnswer4(string) const;

	// "Set" and "Get" the Correct Answer function.
	void setCorrectAnswer(int);
	int getCorrectAnswer() const;
};
#endif 
This is my accessor/mutator .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
#include "TrivialGame.h"

question::question()
{
		questions = " ";
		answer1 = " ";
		answer2 = " ";
		answer3 = " ";
		answer4 = " ";
	correctAnswer = 0.0;
}

question::question(string Q, string A1, string A2, string A3, string A4, int CA)	// Constructor
	{
		questions = Q;
		answer1 = A1;
		answer2 = A2;
		answer3 = A3;
		answer4 = A4;
		correctAnswer = CA;
	}

void setQuestions(string questions, string Q)
{
	questions = Q;
}

string question::getQuestions() const
{
	return questions;
}

void setAnswer1(string answer1, string A1)
{
	answer1 = A1;
}

string question::getAnswer1(string) const
{
	return answer1;
}

void setAnswer2(string answer2, string A2)
{
	answer2 = A2;
}

string question::getAnswer2(string) const
{
	return answer2;
}

void setAnswer3(string answer3, string A3)
{
	answer3 = A3;
}

string question::getAnswer3(string) const
{
	return answer3;
}

void setAnswer4(string answer4, string A4)
{
	answer4 = A4;
}

string question::getAnswer4(string) const
{
	return answer4;
}

void setCorrectAnswer(string correctAnswer, int CA)
{
	correctAnswer = CA;
}

int question::getCorrectAnswer() const
{
	return correctAnswer;
}
Main is unable to invoke the answer1, answer2 (etc) strings because they are not local to main. You can access them with the dot operator for the question objects, which you do in the questionAnswer() function.

From a logic standpoint, should questionAnswers() be passed the answers as strings? You already set the answers from line 34 to 111 and passed it an array of question objects, which I think is sufficient to print the answers.

1
2
3
4
5
6
7
8
void questionAnswers(question i[], int count)
{
	cout << i[count].getQuestions() << endl;
	cout << i[count].getAnswer1() << endl;
	cout << i[count].getAnswer2() << endl;
	cout << i[count].getAnswer3() << endl;
	cout << i[count].getAnswer4() << endl << endl;
}

I think you're saying, get rid of the function "questionAnswers", and from line 34 to 111 put a ".getAnswer" for answers one through four for each question?
Not quite. The questionAnswers() function is useful because it displays the question and answer options to the user each round.

But it looks like you're also trying to force a string in to the getAnswser() function incorrectly with:
cout << i[count].getAnswer1(answer1) << endl;

Typically get() functions just return something, so you don't need to pass it anything. Calling getAnswer() with no arguments ought to be enough:
cout << i[count].getAnswer1() << endl;

That way, when main calls questionAnswer(), it does not need to pass the answer1, answer2 (etc) strings. This is what we want, since main() doesn't know about those strings anyhow.
Topic archived. No new replies allowed.