trying to load from file

So, for my homework assignment I am to:

Write a C++ program that is menu-driven that allows for the following menu choices:

1.Load an exam: Loading an exam should prompt the user for an exam file. If no file exists, it should allow the user to specify a different file. Upon a successful load of an exam, the user should be presented with the menu again.

2.Display exam: The program should simply display each question, its point value, and the answer to the screen. (The functionality of actually taking the exam will be created in Week 5). Upon displaying the exam to the screen, the user should be presented with the menu again.

3.Quit: Quit the program gracefully by displaying a "thank you" message to the user, and ensure that all files have been closed along with any other housekeeping that should be done as your program shuts down.

Whenever I run the program I get to the menu but once I input 1 to load the file I get a runtime error and the program tells me that there was an unknown question type. I am guessing that it is getting stuck in the loadExam function in class Exam

**I am not expecting anyone to do this for me but if someone could help point me in the right direction I would be grateful!!!

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

class Question
{
protected:
	string question;
	int value;

public:
	Question()
	{}

	Question(string theQuestion, int points)
	{
		question = theQuestion;
		value = points;
	}

	string getQuestion()
	{
		cout << question << endl;
		return question;
	}

	int getValue()
	{
		return value;
	}

	virtual void printOptions() = 0;
	virtual string getAnswer() = 0;

};

class QuestionTF : public Question
{
private:
	string answer;

public:
	QuestionTF(string theQuestion, int points, string theAnswer)
		:Question(theQuestion, points)
	{
		answer = theAnswer;
	}

	void printOptions()
	{
		cout << "(true/false)" << endl;
	}

	string getAnswer()
	{
		cout << "The Answer is: " << answer << endl;
		return answer;
	}

};

class QuestionMC : public Question
{
private:
	string answer;
	vector <string> options;

public:
	QuestionMC(string theQuestion, int points, string theAnswer)
		:Question(theQuestion, points)
	{
		answer = theAnswer;
	}

	void addOption(string anOption)
	{
		options.push_back(anOption);
	}

	void printOptions()
	{
		for (int i = 0; i < options.size(); ++i)
		{
			cout << options[i] << endl;
		}
	}

	string getAnswer()
	{
		cout << "The answer is: " << answer << endl;
		return answer;
	}

};

class Exam
{
protected:
	vector <Question*> questions;
	int question;

public:
	Exam() {}

	bool loadExam(std::string filename)
	{
		Question* question;
		vector <string> answerChoices;
		ifstream filein;
		int numQuestions, pointValue;
		string questionType, strQuestion, strAnswer, choice, num;

		filein.open(filename);

		if (!filein.is_open())
		{
			return false;
		}

		else
		{
			getline(filein, num);
			numQuestions = stoi(num);


			for (int i = 0; i < numQuestions; i++)
			{
				getline(filein, questionType, ' ');				
				getline(filein, num);
				pointValue = stoi(num);
				getline(filein, strQuestion);


				if (questionType == "TF")
				{
					QuestionTF*question = new QuestionTF(strQuestion, pointValue, strAnswer);
					questions.push_back(question);
				}

				else if (questionType == "MC")
				{
					QuestionMC *question = new QuestionMC(strQuestion, pointValue, strAnswer);
					questions.push_back(question);
				}

				else
				{
					cout << "Error, unknown question type!\n";
				}
			}

			filein.close();
			cout << "Exam loaded successfully!\n";

		}
		return true;
	}

	void displayExam()
	{
		for (int i = 0; i < questions.size(); ++i)
		{
			cout << "Question " << i + 1 << ":";
			cout << questions[i]->getQuestion() << endl;
			cout << "Point Value: " << questions[i]->getValue() << endl;
			cout << "Answer: " << questions[i]->getAnswer() << endl; 
		}
	}


};



int main()
{
	Exam theExam;
	string filename;
	char menuSelection;

	do
	{
			cout << "Welcome to the Testing Center" << endl << endl;
			cout << "  --------Menu--------" << endl << endl;
			cout << "What would you like to do?" << endl;
			cout << "1 - Load an exam" << endl;
			cout << "2 - Display and exam" << endl;
			cout << "Q - Quit" << endl;

			cin >> menuSelection;

			switch (menuSelection)
			{
			case '1':
				cout << "You selected Load an exam\n";
				cout << "What is the name of the exam file?\n";
				cin >> filename;

				theExam.loadExam(filename);

				if (theExam.loadExam(filename) == true)
					cout << "Exam questions have been loaded succesfully!" << endl;

				if (theExam.loadExam(filename) == false)
				{
					cout << "Load exam failed. Please check testbank file!" << endl;
				}

				break;

			case '2':
				cout << "You selected display exam\n";
				theExam.displayExam();
				break;

			case 'q':
			case 'Q':
				cout << "Thank you and good-bye\n";
				break;

			default:
				cout << "Please try again\n";

			}
		

	} 
	
	while (tolower(menuSelection) != 'q');

	system("pause");

    return 0;
}



The text file is named testbank and is as follows:

3
TF 5
There exist birds that cannot fly?
True
MC 10
Who was the President of the USA in 1991?
6
Richard Nixon
Gerald Ford
Jimmy Carter
Ronald Reagan
George Bush Sr.
Bill Clinton
E
TF 10
The city of Boston hosted the 2004 Summer Olympics?
False

You're not reading in all the information for the questions.
For the TF type of question you need to read the True or False line.
For the MC type of question you need to read the number of options, all the options, and the correct answer.

Also note that you are calling loadExam three times in case '1'. It should be more like this:
1
2
3
4
5
6
7
8
9
10
11
			case '1':
				cout << "You selected Load an exam\n";
				cout << "What is the name of the exam file?\n";
				cin >> filename;

				if (theExam.loadExam(filename))
					cout << "Exam questions have been loaded succesfully.\n";
				else
					cout << "Load exam failed. Please check testbank file.\n";

				break;

tpb,

Thanks for the quick reply and getting me pointed in the right direction! I am going to start looking into what you posted and see if I can't get it sorted out. Ill post with the results in the next day or so.

Thanks Again!!!!!
Topic archived. No new replies allowed.