Write a C++ program to read in various types of test questions (multiple choice
ID: 3821570 • Letter: W
Question
Write a C++ program to read in various types of test questions (multiple choice and True/False) from a test bank (text file), and load the questions into an array of questions. You will need to implement the following class hierarchy (given in UML):
Once the test bank has been loaded, simply iterate over the array of questions and have each question printed out to the screen.
The test bank (text file) will have the following format:
Line 1 will be an integer value, indicating how many questions in the file
Each question will start with a line that starts with either "MC" or "TF" followed by a space and then the point value of the question.
The next line will be the actual question.
If the question was True/False, the following line will be the answer, either "True" or "False"
If the question was multiple choice, the following line will be an integer value indicating how many choices will be provided for that question. The following lines will be the choices. There will never be more than 6 choices. The final line following the choices will be the answer: "A" or "B" or "C" or "D" or "E" or "F".
A sample test bank file is as follows:
3
TF 5
Are their 50 states in the US?
true
MC 10
Which one is not a state in the US?
6
Georgia
Florida
New York
Rio
Alabama
California
D
TF 10
Does the state Florida have a city called Panama City?
true
Question question string value integer Question the Question string pointvalue integer getQuestion string get value integer virtual prin toptions string virtual get Answer string Question Question TF MC string options string Question theQuestion string, pointvalue integer, TF Question TF theg tion string, pointvalue integer, theAnswer string) printoptions string always returns (true/false)" the Answer string) addoption Option string) getAnswer string t print options string getAnswer stringExplanation / Answer
C++ program for the problem is given below. The program consists of three classes (namely Question, QuestionTF and QuestionMC) with properties and methods as mentioned in the UML diagram. In main method data is read from given question bank file and loaded into an array of questions. After that, each question is printed along with corresponding options.
Comments are also added in the code for your understanding.
File: QuestionBank.cpp
#include <iostream>
#include <fstream>
using namespace std;
#define MAX_NUM_OPTS 6
// Question (Base Class)
class Question {
private:
string question;
int value;
public:
Question(string theQuestion, int pointValue) : question(theQuestion), value(pointValue) {}
string getQuestion() { return question; }
int getValue() { return value; }
virtual string printOptions() {}
virtual string getAnswer() {}
};
// True or False Question (Derived Class)
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; }
};
// Multiple Choice Question (Derived Class)
class QuestionMC : public Question {
private:
string answer;
string options[MAX_NUM_OPTS];
int numOptions;
public:
QuestionMC(string theQuestion, int pointValue, string theAnswer) : Question(theQuestion, pointValue), answer(theAnswer) {
numOptions = 0;
}
void addOption(string anOption) {
if (numOptions < MAX_NUM_OPTS) {
options[numOptions++] = anOption;
}
}
string printOptions() {
string optList = "";
for (int i = 0; i < numOptions; i++) {
if (i == 0) {
optList = optList.append(options[i]);
} else {
optList = optList.append(" " + options[i]);
}
}
return optList;
}
string getAnswer() { return answer; }
};
int main(int argc, char *argv[]) {
if (argc < 2) {
cout << "usage: " << argv[0] << " <filename>" << endl;
return -1;
}
ifstream fin(argv[1]);
int numQs;
int pointValue;
int numOpts;
string line;
string type;
string question;
string answer;
string anOption;
// Read number of questions
fin >> numQs;
getline(fin, line);
Question **questions = new Question*[numQs];
string opts[MAX_NUM_OPTS];
for (int i = 0; i < numQs; i++) {
// Read type and point value
fin >> type >> pointValue;
getline(fin, line);
// Read question
getline(fin, question);
if (type == "TF") {
// True/False type question
// Read answer
getline(fin, answer);
// Create True or False Question object
questions[i] = new QuestionTF(question, pointValue, answer);
} else {
// Multiple choice type question
// Read number of options
fin >> numOpts;
getline(fin, line);
numOpts = min(numOpts, MAX_NUM_OPTS);
for (int j = 0; j < numOpts; j++) {
getline(fin, anOption);
opts[j] = anOption;
}
// Read answer
getline(fin, answer);
// Create Multiple Choice Question object
questions[i] = new QuestionMC(question, pointValue, answer);
// Add options to Multiple Choice Question object
for (int j = 0; j < numOpts; j++) {
((QuestionMC *) questions[i])->addOption(opts[j]);
}
}
}
// Print question along with options
for (int i = 0; i < numQs; i++) {
cout << questions[i]->getValue() << ") " << questions[i]->getQuestion() << endl;
cout << questions[i]->printOptions() << endl << endl;
}
}
Compilation:
$ g++ QuestionBank.cpp
Sample Execution Output:
$ ./a.out testdata.txt
5) Are their 50 states in the US?
(true/false)
10) Which one is not a state in the US?
Georgia
Florida
New York
Rio
Alabama
California
10) Does the state Florida have a city called Panama City?
(true/false)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.