cant get it to read the text files and display the txt help

#include <string>

#include <iostream>

#include <vector>

#include <fstream>


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[3];

public:

QuestionMC(string theQuestion, int pointValue, string theAnswer): Question(theQuestion, pointValue)

{

answer = theAnswer;

for (int i = 0; i < 3; i++)

{

options[i] = "";

}

}

void addOption(string anOption)

{

for (int i = 0; i < 3; i++)

{

if (options[i] == "")

{

options[i] = anOption;

break;

}

}

}

string printOptions()

{

string str = "";

for (int i = 0; i < 3; 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 or 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;)
{

fin >> questiontype;
if (questiontype == "TF")
{
fin >> pointvalue;
fin >> extra;
getline(fin, questiontext);

fin >> correctanswer;
QuestionTF * questiontf = new QuestionTF(questiontext, pointvalue, correctanswer);
#include <vector>

getline(fin,correctanswer);







} 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]);

}



}

}

}

int GetNumberOfQuestions()

{

return questions.size();

}

string getCorrectAnswer(int index)

{

return questions[index].getAnswer();

}

void DisplayIthQuestion(int index)

{

cout << "Question Number" << (index + 1) << ": ";

cout << questions[index].getQuestion() << endl;

cout << questions[index].printOptions() << endl;

}

int getPointsValue(int i) {
return questions[i].getValue();
}

void DisplayQuestions(int index)

{

for (int i = 0; i < questions.size(); i++)

{
cout << "Question S" << (index + 1) << ": ";

cout << questions[i].getQuestion() << endl;

cout << questions[i].printOptions() << endl;

cout << "Correct Answer: " << questions[i].getAnswer() << endl << endl;

}

}

};

class Student

{

private:

int pointsAvailable;

int pointsScored;

public:

Student()

{

pointsAvailable = 0;

pointsScored = 0;

}

void SetPointsScored(int point)

{

pointsScored = point;

}

int GetPointsScored()

{

return pointsScored;

}

void SetPointsAvailable(int point)

{

pointsAvailable = point;

}

int GetPointsAvailable()

{

return pointsAvailable;

}

};

char DisplayMenu();

string ConvertToupper(string str);

int main()

{

Examination exam;

char option;

int totalpoints = 0;

int totalAvailablePoints = 0;

option = DisplayMenu();

Student student;

while (option != 'Q')

{

switch (option)

{

case 'L':

{

string filename;

cout << "Enter the filename: ";

cin >> filename;

ifstream fin;

fin.open(filename.c_str());

while (!fin)

{

cout << "Error: Input file does not exists. Try again." << endl;

cout << "Enter the filename: ";

cin >> filename;

fin.open(filename.c_str());

}

exam.LoadQuestions(fin);

fin.close();

}

break;

case 'T':

{

string answer;

for (int i = 0; i;)

{

cout << endl;

exam.DisplayIthQuestion(i);

cout << "Enter correct answer: ";

cin >> answer;

answer = ConvertToupper(answer);

string correct = ConvertToupper(exam.getCorrectAnswer(i));

cout << "The correct answer is: " << correct << endl;

if (answer == correct)

{

student.SetPointsScored(student.GetPointsScored() + exam.getPointsValue(i));

cout << "Good job here!" << endl;

} else

{

cout << "Better luck next time...." << endl;

}

student.SetPointsAvailable(student.GetPointsAvailable() + exam.getPointsValue(i));

}

}

break;

case 'S':

{

cout << "Total points available: " << student.GetPointsAvailable() << endl;

cout << "Total points scored: " << student.GetPointsScored() << endl;

cout << "Percentage of points: " << ((double)student.GetPointsScored()/(double)student.GetPointsAvailable()) * 100.0 << endl;

}

break;

}

option = DisplayMenu();

}

return 0;

}

string ConvertToupper(string str)

{

for (int i = 0; i;)

{

str[i] = toupper(str[i]);

}

return str;

}

char DisplayMenu()

{

char option;

while (true)

{

cout << endl << "\tL - Load an Exam" << endl;

cout << "\tT -Take an exam Exam" << endl;

cout << "\tS - Show exam results" << endl;

cout << "\tQ - Quit" << endl;

cout << "Enter an option: ";

cin >> option;

option = toupper(option);

if (option == 'L' || option == 'T' || option == 'S' || option == 'Q')

return option;

else

cout << "ERROR: Input a valid option." << endl;

}

}
Hello dev1243,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


This is going to take a little time to load up and check out.

Andy
cant get it to read the text files and display the txt help
Without knowing the input file format, it's impossible to answer your question. Can you give an example of the input?

Here is your code indented:
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
#include <string>
#include <iostream>
#include <vector>
#include <fstream>

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[3];
  public:
      QuestionMC(string theQuestion, int pointValue,
		 string theAnswer):Question(theQuestion, pointValue)
    {
	answer = theAnswer;
	for (int i = 0; i < 3; i++)
	{
	    options[i] = "";
	}
    }
    void addOption(string anOption)
    {
	for (int i = 0; i < 3; i++) {
	    if (options[i] == "") {
		options[i] = anOption;
		break;
	    }
	}
    }
    string printOptions()
    {
	string str = "";
	for (int i = 0; i < 3; 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 or 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;)
	{
	    fin >> questiontype;
	    if (questiontype == "TF") {
		fin >> pointvalue;
		fin >> extra;
		getline(fin, questiontext);
		fin >> correctanswer;
		QuestionTF *questiontf = new QuestionTF(questiontext, pointvalue, correctanswer);
		getline(fin, correctanswer);

	    } 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]);
		}

	    }
	}
    }
    int GetNumberOfQuestions()
    {
	return questions.size();
    }
    string getCorrectAnswer(int index)
    {
	return questions[index].getAnswer();
    }
    void DisplayIthQuestion(int index)
    {
	cout << "Question Number" << (index + 1) << ": ";
	cout << questions[index].getQuestion() << endl;
	cout << questions[index].printOptions() << endl;
    }
    int getPointsValue(int i)
    {
	return questions[i].getValue();
    }
    void DisplayQuestions(int index)
    {
	for (int i = 0; i < questions.size(); i++) {
	    cout << "Question S" << (index + 1) << ": ";
	    cout << questions[i].getQuestion() << endl;
	    cout << questions[i].printOptions() << endl;
	    cout << "Correct Answer: " << questions[i].getAnswer() << endl << endl;
	}
    }
};

class Student
{
private:
    int pointsAvailable;
    int pointsScored;
public:
    Student()
    {
	pointsAvailable = 0;
	pointsScored = 0;
    }
    void SetPointsScored(int point)
    {
	pointsScored = point;
    }
    int GetPointsScored()
    {
	return pointsScored;
    }
    void SetPointsAvailable(int point)
    {
	pointsAvailable = point;
    }
    int GetPointsAvailable()
    {
	return pointsAvailable;
    }
};

char DisplayMenu();
string ConvertToupper(string str);

int
main()
{
    Examination exam;
    char option;
    int totalpoints = 0;
    int totalAvailablePoints = 0;
    option = DisplayMenu();
    Student student;
    while (option != 'Q') {
	switch (option) {
	case 'L':
	    {
		string filename;
		cout << "Enter the filename: ";
		cin >> filename;
		ifstream fin;
		fin.open(filename.c_str());
		while (!fin) {
		    cout << "Error: Input file does not exists. Try again." << endl;
		    cout << "Enter the filename: ";
		    cin >> filename;
		    fin.open(filename.c_str());
		}
		exam.LoadQuestions(fin);
		fin.close();
	    }
	    break;
	case 'T':
	    {
		string answer;
		for (int i = 0; i;) {
		    cout << endl;
		    exam.DisplayIthQuestion(i);
		    cout << "Enter correct answer: ";
		    cin >> answer;
		    answer = ConvertToupper(answer);
		    string correct = ConvertToupper(exam.getCorrectAnswer(i));
		    cout << "The correct answer is: " << correct << endl;
		    if (answer == correct) {
			student.SetPointsScored(student.GetPointsScored() +
						exam.getPointsValue(i));
			cout << "Good job here!" << endl;
		    } else {
			cout << "Better luck next time...." << endl;
		    }
		    student.SetPointsAvailable(student.GetPointsAvailable() +
					       exam.getPointsValue(i));
		}
	    }
	    break;
	case 'S':
	    {
		cout << "Total points available: " << student.
		    GetPointsAvailable() << endl;
		cout << "Total points scored: " << student.GetPointsScored() << endl;
		cout << "Percentage of points: " << ((double) student.GetPointsScored() /
						     (double) student.
						     GetPointsAvailable()) *
		    100.0 << endl;
	    }
	    break;
	}
	option = DisplayMenu();
    }
    return 0;
}

string
ConvertToupper(string str)
{
    for (int i = 0; i;) {
	str[i] = toupper(str[i]);
    }
    return str;
}

char
DisplayMenu()
{
    char option;
    while (true) {
	cout << endl << "\tL - Load an Exam" << endl;
	cout << "\tT -Take an exam Exam" << endl;
	cout << "\tS - Show exam results" << endl;
	cout << "\tQ - Quit" << endl;
	cout << "Enter an option: ";
	cin >> option;
	option = toupper(option);
	if (option == 'L' || option == 'T' || option == 'S' || option == 'Q')
	    return option;
	else
	    cout << "ERROR: Input a valid option." << endl;
    }
}


Lines 48-51: These aren't necessary since string has a default constructor. But really, why use a fixed-length array for the options? Why not make options a vector<string> and then add_option() can just call options.push_back()
Line 67: Replace 65 with 'A' to make it clearer.
Line 80: You could derive QuestionTF from QuestionMC and set two options to "True" and "False"
Line 103. Questions should be a vector<Question*>. That way it can store any type of question. Don't forget to create a destructor for Examination that will delete the questions. For this to work, you'll need to makevirtual Question::~Question().
Lines 125 & 141: You create questions and then don't do anything with them. You want to store these in the questions vector, which will be possible once you make it a vector<Question*>
Lines 160,161: I'd create Question::display(ostream &) method and replace these lines with a call to questions[index].display();
Liens 170-172: Replace with DisplayIthQuestion(i);
Topic archived. No new replies allowed.