Need help allowing user to take a quiz.

Hi, I have a program I have been building to allow a user to take a quiz. Up until now I have only been load the quiz from a file and display everything. Now I need to allow a user to take the quiz. As well as score the user and display the results. So as a result I have to change some things as well as add some things to make that happen. I have already started attempting this but can't seem to figure it out. If anyone wouldn't mind taking a look at my current code and give me some advise as to how I can go about doing this that would be awesome. Thank you in advance for any help given.
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <limits>

class questions
{
protected: 
	std::string question;
	std::string answer;
	int value;
	std::string questionType;

public:

	std::string getQuestion()
	{
		return question;
	}

	virtual int getValue()
	{
		return value;
	}

	virtual std::string getQuestionType()
	{
		return questionType;
	}


	virtual void setQuestion(std::string answer, int value)
	{
	}
	virtual void setNewQuestion(std::string answer, int value)
	{
	}
	virtual void printOptions()
	{
	}

	virtual std::string getAnswer()
	{
		return answer;
	}
};


class tfQuestion : public questions
{
private:
	std::string options;
public:
	void setQuestion(std::string theQuestion, int pointValue)
	{
		std::string theAnswer;
		questionType = "TF";
		question = theQuestion;
		value = pointValue;
		options = "true/false";
		//get the answer from the file
		std::getline(std::cin, theAnswer);
		answer = theAnswer;
	}

	int getValue() //gets the point value of the question
	{
		return value;
	}

	std::string getQuestionType()// gets the type of question
	{
		return questionType;
	}

	std::string getQuestion()
	{
		return question;
	}

	void printOptions()
	{
		std::cout << question << std::endl;
		//std::cout << answer << std::endl;//this stops output of answer for TF
	}

	std::string getAnswer()//outputs the answer for that question
	{
		return answer;
	}
};



class mcQuestion : public questions
{
private:
	int numberOfOptions;
	std::string mcAnswers[6];
public:
	void setQuestion(std::string theQuestion, int pointValue)
	{
		std::string line;
		questionType = "MC";
		std::getline(std::cin, line);
		numberOfOptions = atoi(line.c_str());
		question = theQuestion;
		value = pointValue;
		//get the individual choice lines and load to options array
		for (int count = 0; count < numberOfOptions; count++)
		{
			std::getline(std::cin, line);
			mcAnswers[count] = line;
		}
		//get the answer from the file and load into answer
		std::getline(std::cin, line);
		answer = line;
	}
	void printOptions()// prints the questions, options, and answer
	{
		//char first = 'A';
		std::cout << question << std::endl;
		for (int count = 0; count < numberOfOptions; count++)
		{
			std::cout << mcAnswers[count] << "\n";
		}
		//std::cout << answer << "\n";//This stops output of answer
	}

	int getValue() //gets the point value of the question
	{
		return value;
	}

	std::string getQuestionType()// gets the type of question
	{
		return questionType;
	}

	std::string getAnswer()// prints the answer
	{
		return answer;
	}
};

class Quiz : questions
{
private:
	std::string fileName;
	questions *myQuestions[10];
	int numberOfQuestions;

public:
	int loadQuiz()
	{
		std::ifstream infile;
		infile.clear();
		while (!infile.is_open())
		{
			std::cout << "Please enter the .txt file name: " << std::endl;
			std::getline(std::cin, fileName);
			infile.open(fileName.c_str(), std::ios::in | std::ios::binary);
			if (infile)
				break;
			std::cout << "Either the file doesn't exist or you forgot the .txt extension. ";
			std::cout << "Please try again. " << std::endl << std::endl;
		}
		std::streambuf *cinbuf = std::cin.rdbuf();       //save old buf
		std::cin.rdbuf(infile.rdbuf());             //redirect std::cin to infile.txt!

		std::string line;
		std::string theQuestion;
		std::string questiontype;
		//std::string theAnswer;
		int  questionvalue;

		while (true)
		{
			//std::getline(infile, line);
			std::getline(std::cin, line);
			numberOfQuestions = atoi(line.c_str());
			if (numberOfQuestions != 6)
			{
				std::cout << "Error! The quiz has too many or too litte questions. The desired amount is \'6\'" << std::endl;
				std::cout << "Please check the file to ensure there is an acceptable amount of questions." << std::endl;
				throw;
			}
			else
			{
				break;
			}
		}
		for (int count = 0; count < numberOfQuestions; count++)
		{
			//std::getline(infile, line);
			std::getline(std::cin, line);
			//get the next line with the question type and the value of the question
			int npos = line.size();
			int prev_pos = 0;
			int pos = 0;
			while (line[pos] != ' ')
				pos++;
			questiontype = line.substr(prev_pos, pos - prev_pos);
			prev_pos = ++pos;
			questionvalue = atoi(line.substr(prev_pos, npos - prev_pos).c_str()); // Last word

			if (questiontype == "TF")
			{
				myQuestions[count] = new tfQuestion;
				std::getline(std::cin, theQuestion);
				myQuestions[count]->setQuestion(theQuestion, questionvalue);
			}

			if (questiontype == "MC")
			{
				myQuestions[count] = new mcQuestion;
				std::getline(std::cin, theQuestion);
				myQuestions[count]->setQuestion(theQuestion, questionvalue);
			}

		}
		std::cin.rdbuf(cinbuf);//restore cin to standard input
		infile.close();
		return numberOfQuestions;
	}

	void displayQuizQuestions(int numquestions)
	{
		int a = 0;
		std::string q1, q2, q3, q4, q5, q6;
		std::string a1, a2, a3, a4, a5, a6;
		//print out the questions that have been processed
		//		for (int i = 0; i < numquestions; i++)
		//		{
		//			qtype = myQuestions[i]->getQuestionType();
		//			std::cout << qtype << " " << myQuestions[i]->getValue() << "\n";
		//			myQuestions[i]->printOptions();
		//			std::cout << "\n";
		//			delete myQuestions[i];//dealocate
		//		}//need to change this ^ to print one question at a time so user can take the quiz
		q1 = myQuestions[0]->getQuestionType();
		q2 = myQuestions[1]->getQuestionType();
		q3 = myQuestions[2]->getQuestionType();
		q4 = myQuestions[3]->getQuestionType();
		q5 = myQuestions[4]->getQuestionType();
		q6 = myQuestions[5]->getQuestionType();


		std::cout << q1 << " " << myQuestions[0]->getValue() << "\n";
		myQuestions[0]->printOptions();
		std::cout << "\n";
		std::cout << "Please enter your answer:";
		std::cin >> a1;
		if (a1 == /*stuck here answer doesn't work->*/answer)
		{
			std::cout << "correct" << "\n";
			//connect to student class for scoring purpose?
		}
		else
		{
			std::cout << "Sorry that is incorrect" << std::endl;
			std::cout << "The correct answer is" << /*stuck here*/ std::endl;
		}




	}
};
class Student : Quiz
{
private:
	int possiblePoints;
	int pointsScored;
	std::string name;

	//int totalScore;
public:
	std::string student()
	{
		std::cout << "Please enter your name" << std::endl;
		std::getline(std::cin, name);
		std::cout << "Hello " << name << std::endl;
	}
	int getPointsPossible()
	{

	}
	int getPointsScored()
	{

	}
	int addPointsPossible(int points)
	{

	}
	int addPointsScored(int points)
	{

	}

};

//sorry code is too long so i have to split it in two... this is where it continues from 
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
//continuing on below:
int menu()
{
	int selection = 0;


	std::cout << "Please select an option from the list below." << std::endl;
	//display the menu
	std::cout << "Menu";
	std::cout << "======" << std::endl;
	std::cout << "1 - Load quiz." << std::endl;
	std::cout << "2 - Take quiz." << std::endl;
	std::cout << "3 - Show quiz results." << std::endl;
	std::cout << "4 - Press 4 to exit." << std::endl << std::endl;
	std::cout << "Enter selection: ";
	while (true)
	{
		std::cin >> selection;
		if (std::cin.fail())
		{
			std::cerr << "Please enter an integer number." << std::endl;
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
		if (selection == 1 || selection == 2 || selection == 3 || selection == 4) 
		{
			break;
		}
		std::cout << "The options are: \'1\'\'2\'\'3\' or \'4\': ";
	}
	std::cin.ignore();
	return selection;
}



int main()
{

	Quiz theQuiz;
	int numquestions;

	int selection = 0;
	std::string quizName = "quiz.txt";

	while ((selection = menu()) != 4)
		switch (selection)
		{
		case 1:
			std::cout << "You selected - Load quiz." << std::endl;
			numquestions = theQuiz.loadQuiz();
			break;

		case 2:
			std::cout << "You selected - Take quiz." << std::endl << std::endl;
			std::cout << "===== Quiz: =====" << std::endl;
			theQuiz.displayQuizQuestions(numquestions);
			break;

		case 3:
			std::cout << "You selected - Show quiz results." << std::endl;
			//need to display the results after taking the quiz
			break;

		case 4:
			break;
		default: std::cout << "Invalid selection. Please try again.\n";
			}
	std::cout << std::endl << std::endl;
	std::cout << "Thank you for using this program. Goodbye.";
	std::cin.get();
	return 0;
}
Topic archived. No new replies allowed.