Write a C++ program to read in various types of test questions (multiple choice
ID: 3799705 • 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
// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <stdlib.h> /* srand, rand */
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <string.h>
using namespace std;
int main()
{
int totalQuestion;
int totalPoints = 0;
string input, type, question, answer;
int totalOptions;
string c;
int points;
ifstream inFile ("input.txt");
if (inFile.is_open())
{
inFile >> totalQuestion;
while(true)
{
for (int i = 0; i < totalQuestion; ++i)
{
cout << "Question: "<< (i+1) << endl;
inFile >> type >> points;
// read type of question
if(type == "TF")
{
// get the question
inFile.ignore();
getline(inFile, question);
// get the answer
inFile >> answer;
cout << "Question: " << question << endl;
cout << "Input answer: ";
cin >> input;
if(input == answer)
{
cout << "Right Answer ";
totalPoints = totalPoints + points;
}
else
cout << "Wrong answer ";
}
else if(type == "MC")
{
string options;
inFile.ignore();
getline(inFile,question);
inFile >> totalOptions;
cout << "Question: " << question << endl;
for (int j = 0; j < totalOptions; ++j)
{
getline(inFile , options);
cout << options << endl;
}
inFile >> answer;
cout << "Enter your choice(A-F): ";
cin >> c;
inFile.ignore();
if(c == answer)
{
cout << "Right Answer ";
totalPoints = totalPoints + points;
}
else
cout << "Wrong answer ";
}
if(inFile.eof())
break;
}
}
inFile.close();
}
else cout << "Unable to open file";
cout << "Total points: " << totalPoints << endl;
return 0;
}
/*
input.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
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.