Classes and reading text file

Hello,

I've been banging my head against this for a bit now. The assignment is to make a Question class with children QuestionTF and QuestionMC. It will read in the text file as formatted, then just display the questions on the console (it doesn't ask anything yet). Here is what I have:
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//question class
class Question
{
protected:
	string question;
	int value;
public:
	Question(string theQuestion, int pointValue)
	{
		question = theQuestion;
		value = pointValue;
	}
	string getQuestion(string qu)
	{
		question = qu;
		return question;
		cout << "\t" << question << endl;
	}
	int getValue(int val)
	{
		value = val;
		return value;
		cout << "Worth " << value << " points" << endl;
	}
	virtual ~Question(){}
	virtual string printOptions()
	{
		return NULL;
	}
	virtual string getAnswer()
	{
		return NULL;
	}
}; //end question class

//true false class
class QuestionTF : public Question
{
private:
	string answer;
public:
	QuestionTF(string theQuestion, int pointValue, string theAnswer) : Question(theQuestion, pointValue)
	{
		answer = theAnswer;
	}
	string printOptions() const
	{
		cout << "\t(true/false)" << endl;
		return NULL;
	}
	string getAnswer(string ans)
	{
		answer = ans;
		return answer;
		cout << "\tAnswer - " << answer << endl;
	}
}; //end true false class

//multiple choice class
class QuestionMC : public Question
{
private:
	string answer, options[];
public:
	QuestionMC(string theQuestion, int pointValue, string theAnswer) : Question(theQuestion, pointValue)
	{
		theAnswer = answer;
	}
	string addOption(string anOption)
	{
		for (int i = 0; i < 4; i++)
		{
			anOption = options[i];
		}
		return 0;
	}
	string printOptions()
	{
		return 0;
	}
	string getAnswer(string ans)
	{
		ans = answer;
		return answer;
		cout << "\tAnswer - " << answer << endl;
	}
}; //end multiple choice class

//function to read the bank in
void testBank()
{
	//declarations
	int numQuestions, numOptions, points;
	string questionType, questionText, answerText, mcOption;

	//arrays
	string questionBank[numQuestions] = {}, answerBank[numQuestions] = {}, mcOptions[numOptions] = {};

	//class objects
	Question test(questionText, points);
	QuestionTF tf(questionText, points, answerText);
	QuestionMC mc(questionText, points, answerText);

	//read number of questions
	fstream infile;
	infile >> numQuestions;

	//loop for test questions
	for (int i = 1; i <= numQuestions; i++)
	{
		infile >> questionType >> points;
		cout << "Question " << i << ":" << endl;
		test.getValue(points);
		test.getQuestion(questionType);

		//true/false
		if (questionType == "TF")
		{
			getline(infile, questionText);
			getline(infile, answerText);
			test.getQuestion(questionText);
			tf.printOptions();
			tf.getAnswer(answerText);
		}//end true/false

		//multiple choice
		else if (questionType == "MC")
		{
			getline(infile, questionText);
			infile >> numOptions;
			for (int j = 1; j <= numOptions; j++)
			{
				getline(infile, mcOption);
				mc.addOption(mcOption);
			}
			getline(infile, answerText);
			mc.getAnswer(answerText);
		}//end multiple choice
	}//end loop
};//end test bank function

int main()
{
	//open test bank file
	ifstream infile;
	infile.open ("testbank.txt");

	//error if file can't open
	if (!infile)
	{
		cout << "File could not be opened" << endl;
	}
	testBank();
	infile.close();

	return 0;
}//end main 


And the text file:

5
MC 20
Which of these does not affect a satellite's orbit around Earth?
4
Mars' gravity
Solar radiation
Atmospheric drag
Shape of the Earth
A
TF 20
Polar orbits have an inclination of 90 degrees.
true
MC 20
__________ orbits are highly eccentric Earth orbits with periods of approximately 12 hours.
4
Polar
Molniya
Walking
Geosynchronous
B
TF 20
Apogee is the point in a satellite's orbit closest to the Earth.
false
MC 20
Which of the following is not an orbital element?
4
Inclination
Eccentricity
Semi-major axis
Semi-minor axis
D

I get no compile errors, but the exe crashes as soon as it starts. I'm not that knowledgeable or proficient, especially with class. Any help with what I have wrong will be appreciated.

Thank you!
Last edited on
The infile you declared and opened in main is unrelated to the infile you declared in testBank().
So I should just move opening and closing the file into testBank(), got it. I still can't get the exe to work. I'm not looking for anyone to do the work for me, but basically explain it and/or give me an example to where I can understand it.

Thank you!
Unfortunately some compiler take line 68 even without a warning. You cannot use options within QuestionMC because it has no size.

Further more returning 0 (line 80/84/...) where the return type is a std::string may lead to crash or undefined behavior depending on the implementation of std::string.

Line 72 makes no sense. I would guess that you mean it the other way round.
Thank you for your response! I did forget to put the size in. I will fix the returns when I get home from work, and yes, I just got line 72 backwards.

Thanks again, and I'll post how it goes when I get home.
I made the changes and the exe still just stops working as soon as it runs. This class is 5.5 weeks long and is pretty brutal for me, and I feel like I don't have time to fully understand C++.
Another problem is line 102: Neither numQuestions nor numOptions contains anything valid. Hence your arrays will have an invalid size. Instead of variable length make it fixed length (100 questions/options should suffice).

Another point:
1
2
		return answer;
		cout << "\tAnswer - " << answer << endl; // Note: you will never see this cout because the function ends with return 


Another problem will be mixing >> and getline(). See Notes:

http://en.cppreference.com/w/cpp/string/basic_string/getline

If there are more problem, please post your changes.
I just wanted to thank you for your help. Once I really started to understand return, I got it figured out.
Topic archived. No new replies allowed.