Quiz Program

Hi all,

I am having an issue with compiling my quiz program. Could you all take a look at it?

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

class Question
{
private:
	string question;
	int value;
public:
	Question() {};
	Question(string theQuestion, int pointValue)
	{
		question = theQuestion;
		value = pointValue;
	}
	string getQuestion()
	{
		return question;
	}
	int getValue()
	{
		return value;
	}
	virtual string printOptions() {return ""; };
	virtual string getAnswer() {return ""; };
};

class QuestionMC : public Question
{
private:
	string answer;
	string options[6];
public:
	QuestionMC(string theQuestion, int pointValue, string theAnswer) : Question(theQuestion, pointValue)
	{
		answer = theAnswer;
		for (int i = 0; i<6; i++)
		{
			options[i] = "";
		}
	}
	void addOption(string anOption)
	{
		for (int i = 0; i<6; i++)
		{
			if (options[i] == "")
			{
				options[i] = anOption;
				break;
			}
		}
	}
	string printOptions()
	{
		string str = "";
		for (int i = 0; i<6; i++)
		{
			if (options[i] != "")
			{
				str = str + (char)(65 + i) + ". " + options[i] + "\n";
			}
			else
			{
				break;
			}
		}
		return str;
	}
	string getAnswer()
	{
		return answer;
	}
};

class QuestionTF : public Question
{
private:
	string answer;
public:
	QuestionTF(string theQuestion, int pointValue, string theAnswer) : Question(theQuestion, pointValue)
	{
		answer = theAnswer;
	}
	string printOptions()
	{
		return "True / False";
	}
	string getAnswer()
	{
		return answer;
	}
};

class Examination
{
private:
	vector<Question*> questions;
public:
	void LoadQuestions(ifstream &fin)
	{
		questions.clear();
		int numberofquestions;
		string questiontype;
		string questiontext;
		string correctanswer;
		
		int pointvalue;
		
		fin >> numberofquestions;
		
		string extra;
		char ex;
		for (int i = 0; i<numberofquestions; i++)
		{
			fin >> questiontype;
			if (questiontype == "TF")
			{
				fin >> pointvalue;
				fin >> extra;
				getline(fin, questiontext);
				
				fin >> correctanswer;
				QuestionTF *questiontf = new QuestionTF(questiontext, pointvalue, correctanswer);
				questions.push_back(questiontf);
				question[i] = questiontf;
 			}
 			else if (questiontype == "MC")
 			{
 				fin >> pointvalue;
 				fin.ignore();
 				getline(fin, questiontext);
 				int numberofoptions;
 				fin >> numberofoptions;
 				string *answers;
 				answers = new string[numberofoptions];
 				fin.ignore();
 				for (int i=0; i<numberofoptions; i++)
 				{
 					getline(fin, answers[i]);
 					
				 }
				fin >> correctanswer;
				QuestionMC *questionmc = new QuestionMC(questiontext, pointvalue, correctanswer);
				for (int i=0; i<numberofoptions; i++)
				{
					questionmc ->addOption(answers[i]);
				}
				questions.push_back(questionmc);
				question[i] = questionmc
			 }
		}
	}
	void DisplayQuestions()
	{
		for (int i=0; i<questions.size(); i++)
		{
			cout << "Question #" << (i + 1) << ": ";
			cout << questions[i]->getQuestion() << endl;
			cout << questions[i]->printOptions() << endl;
			cout << "Correct answer: " << questions[i];
		}
	}
};

char DisplayMenu();

int main ()
{
	Examination exam;
	char option;
	option = DisplayMenu();
	while (option != 'Q')
	{
		switch (option)
		{
		case 'L':
		{
			string filename;
			cout << "Enter Filename: ";
			cin >> filename;
			ifstream fin;
			fin.open(filename.c_str());
			while (!fin)
			{
				cout << "Error: Input file does not exist. Please try again. " << endl;
				cout << "Enter filename: ";
				cin >> filename;
				fin.open(filename.c_str());
			}
			exam.LoadQuestions(fin);
			fin.close();
		}
		break;
		case 'D':
		{
			exam.DisplayQuestions();
		}
		break;
		}
		option = DisplayMenu();
	}
	return 0;
}

char DisplayMenu()
{
	char option;
	while (true)
	{
		cout << endl << "\tL - Load an Exam" << endl;
		cout << "\tD - Display Exam" << endl;
		cout << "\tQ - Quit" << endl;
		cout << "Enter an option: ";
		cin >> option;
		option = toupper(option);
		if (option == 'L' || option == 'D' || option == 'Q')
			return option;
		else
			cout << "ERROR: Input a valid option." << endl;	
	}
}
What did the compiler error messages tell you?

Line 128: question[i] is undefined. Did you mean questions[i]? If so, why? You already pushed the pointer in the previous line.

Line 152: ditto

Line 152: i is out of scope.

Line 152: Missing ;

Abstract,

I see, I forgot to delete those lines, using some code from my instructor. I wasn't looking at my error message correctly, was tired and all the code started to look the same.

So everything worked except the correct answer. It is showing the memory location, not the answer. The only address location pointer is in line 102, could this be my issue?
In cout << "Correct answer: " << questions[i]; questions[i] is a pointer-to-Question and cout doesn't know what to do with such a pointer. In situations where cout encounters a pointer-to-some-type and the user hasn't provided directions in the form of an overloaded operator>> operator<< taking a pointer-to-that type, cout outputs the address the pointer represents. Instead:

cout << "Correct answer: " << questions[i]->getAnswer();
Last edited on
Cire,

Thank you for the quick lesson! I see what I did, I forgot to add the ->getanswers(); sad part it is right there next to the rest of my couts and didn't look close enough. I appreciate your help!

Topic archived. No new replies allowed.