Question
I need help with this I am unsure how to implement this. Implement the following into your program. The program should first ask the user
I need help with this I am unsure how to implement this.
Implement the following into your program. The program should first ask the user how many questions he or she wishes to create. This quantity will be the first line in the test bank. The user should now be prompted for all information for each question, and then that question is written out to the test bank in the exact format specified:
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".
For each question, the following information should be gathered:
*Question type: Multiple choice (MC) or True/False (TF)
*Question value
*Actual question
*If the question is TF, then get answer
*If the question is MC, then get the number of options followed by each option and finally the answer
This program should be robust and allow users who make mistakes the ability to correct them (exception handling).
My code:
#include
#include
#include
#include
#include
using namespace std;
class Question // super class
{
public:
string getQuestion()//gets the question
{
return question;
}
int getValue() //gets the point value of the question
{
return value;
}
virtual void setQuestion(string answer, int value, istream& inf)
{
}
virtual void printOptions()
{
}
virtual string getAnswer()
{
return answer;
}
private:
string question, answer;
int value;
};
class QuestionTF : public Question// class for true and false questions
{
public:
void setQuestion(string theQuestion, int pointValue, istream& inf)
{
string theAnswer;
question = theQuestion;
points = pointValue;
options = "true/false";
//get the answer from the file
getline(inf, theAnswer);
answer = theAnswer;
}
void printOptions()//prints the options for that question
{
cout << question << endl;
cout << answer << endl;
}
string getAnswer()//outputs the answer for that question
{
return answer;
}
private:
string question;
string answer;
int points;
string options;
};
class QuestionMC : public Question//class for multiple choice
{
public:
void setQuestion(string answerarray, int pointValue, istream& inf)
{
stringstream ss(answerarray);
string tok;
getline(ss, tok, ',');
numberOfOptions = stoi(tok);
getline(ss, tok, ',');
int i = 0;
while (getline(ss, tok, ','))
{
if (i>numberOfOptions)
break;
options[i] = tok;
i++;
}
answer = tok;
}
void printOptions()// prints the questions, options, and answer
{
char first = 'A';
cout << question << endl;
for (int count = 0; count cout << first++ << ". " << options[count] << endl; } cout << "Answer: " << answer << endl; } string getAnswer()// prints the answer { return answer; } private: int value, numberOfOptions; string question, answer; string options[6]; }; int main() { ofstream outf; ifstream inf; Question *myQuestions[10]; string questiontype, questiontxt; string answertxt, optiontxt; int numquestions = 4, questionvalue; // this is something to write the test bank test file from the text given Week 2 // You might use a text file that you create separately and avoid this // in Week 3 the focus is developing a test file section that can replace this. outf.open("c:\\temp\\IP2_testbank.txt"); if (outf.is_open()) { outf << "3 "; outf << "TF 5 "; outf << "There exists birds that cannot fly? "; outf << "true "; outf << "MC 10 "; outf << "Who was the President of the USA in 1991? "; outf << "6 "; outf << "Richard Nixon "; outf << "Gerald Ford "; outf << "Jimmy Carter "; outf << "Ronald Reagan "; outf << "George Bush Sr. "; outf << "Bill Clinton "; outf << "E "; outf << "TF 10 "; outf << "The city of Boston hosted the 2004 Summer Olympics? "; outf << "false "; outf.close(); } else cout << "Unable to open file"; //opening the testbank file and processing as a question of each type inf.open("c:\\temp\\IP2_testbank.txt"); string line, theQuestion, theAnswer; if (inf.is_open()) { //get the number of questions from the first line in the file getline(inf, line); numquestions = stoi(line); for (int count = 0; count getline(inf, line); //get the next line with the question type and the value of the question int npos = line.size(); int prev_pos = 0; int pos = 0; while (line[pos] != ' ') pos++; questiontype = line.substr(prev_pos, pos - prev_pos); prev_pos = ++pos; questionvalue = stoi(line.substr(prev_pos, npos - prev_pos)); //Last word if (questiontype == "TF") //process a true/false question { myQuestions[count] = new QuestionTF; getline(inf, theQuestion); myQuestions[count]->setQuestion(theQuestion, questionvalue, inf); } //process a multiple choice question if (questiontype == "MC") { myQuestions[count] = new QuestionMC; getline(inf, theQuestion); //myQuestions[count]->setQuestion(theQuestion, questionvalue); string str = ""; string line; //get the number of choices from the file getline(inf, line); str = str + line + ","; int numberOfOptions = stoi(line); //question = theQuestion; //value = pointValue; //get the individual choice lines and load to options array // count variable is already defined, so I changed it to _count for (int _count = 0; _count getline(inf, line); str = str + line + ","; //options[count] = line; } //get the answer from the file and load into answer getline(inf, line); str = str + line; myQuestions[count]->setQuestion(str, 0, inf); } } } //print out the questions that have been processed for (int count = 0; count { myQuestions[count]->printOptions(); cout << endl; } getchar(); return 0; } Please assist.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started