Write a C++ program to read in various types of test questions (multiple choice
ID: 3859383 • 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".
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
struct Question{
string type;
int value;
string content;
int no_of_options;
string options[6];
string answer;
};
int main(){
ifstream fin;
int count;
string line;
string type;
int value;
string content;
int no_of_options;
string options[6];
string answer;
int index;
Question *list;
fin.open("sample.txt"); // Assuming test bank is sample.txt
if (fin){
getline(fin,line);
istringstream iss0(line);
iss0 >> count;
list = new Question[count];
for (int j = 0; j<count; j++){
getline(fin,line);
if (line.substr(0,2) == "MC"){
istringstream iss1(line);
iss1 >> list[j].type >> list[j].value;
getline(fin,list[j].content);
getline(fin,line);
istringstream iss2(line);
iss2 >> list[j].no_of_options;
for (int i = 0; i<list[j].no_of_options; i++)
getline(fin,list[j].options[i]);
getline(fin,list[j].answer);
}
if (line.substr(0,2) == "TF"){
istringstream iss3(line);
iss3 >> list[j].type >> list[j].value;
getline(fin,list[j].content);
getline(fin,list[j].answer);
}
}
fin.close();
for (int i = 0; i<count; i++){
cout << list[i].type << " " << list[i].value << endl;
if (list[i].type == "MC"){
cout << list[i].content << endl;
for (int j = 0; j<list[i].no_of_options; j++){
cout << list[i].options[j] << endl;
}
cout << list[i].answer << endl;
}
if (list[i].type == "TF"){
cout << list[i].content << endl;
cout << list[i].answer << endl;
}
}
}
else {
cout << "Error in opening file ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.