In: Computer Science
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:
For this assignment, you may download this sample code for reference. A sample test bank file 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
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// questions.txt
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
____________________________
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
//Declaring variables
ifstream dataIn;
string type, str, ans;
int cnt = 0, num;
int score;
//Opening the input file
dataIn.open("questions.txt");
//checking whether the file name is valid or not
if (dataIn.fail()) {
cout << "** File Not Found **";
exit(0);
} else {
getline(dataIn, str);
cnt = atoi(str.c_str());
// Creating array dynamically
string * questions = new string[cnt];
for (int i = 0; i < cnt; i++) {
getline(dataIn, str);
int indx = str.find(" ");
type = str.substr(0, indx);
score = atoi(str.substr(indx + 1, str.length()).c_str());
if (type.compare("TF") == 0) {
getline(dataIn, str);
questions[i] = str;
getline(dataIn, ans);
} else if (type.compare("MC") == 0) {
getline(dataIn, str);
questions[i] = str;
getline(dataIn, str);
num = atoi(str.c_str());
for (int i = 0; i < num; i++) {
getline(dataIn, str);
}
getline(dataIn, ans);
}
}
dataIn.close();
cout << "Displaying the questions :" << endl;
for (int i = 0; i < cnt; i++) {
cout << questions[i] << endl;
}
}
return 0;
}
____________________________
Output:
_______________Could you plz rate me well.Thank
You