I need help to modify the program; Part 2: Modify the program from part 1 to cha
ID: 3663901 • Letter: I
Question
I need help to modify the program;
Part 2: Modify the program from part 1 to change the menu to the following:
Load an exam
Take an exam
Show exam results
Quit
Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file.
Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g., "Good job" or "Better luck next time") Upon completion of the exam, the program should return the user to the menu.
Choice 3: The program should display the total points available and the total points scored during that exam. A percentage score should also be displayed. (Optional: if you choose to track which problems were missed, you could display that information for the user.)
Choice 4: No change to this functionality from the Phase 4 IP
this is the code that needs to be modified
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
class Question // super class
{
public:
string getQuestion()//gets the question
{
return question;
}
virtual int getValue() //gets the point value of the question
{
return value;
}
virtual string getQuestionType()// gets the type of question
{
return questiontype;
}
virtual void setQuestion(string answer, int value)
{
}
virtual void setNewQuestion(string answer, int value)
{
}
virtual void printOptions()
{
}
virtual string getAnswer()
{
return answer;
}
private:
string question, answer;
int value;
string questiontype;
};
class QuestionTF: public Question// class for true and false questions
{
public:
void setQuestion(string theQuestion, int pointValue)
{
string theAnswer;
questiontype = "TF";
question = theQuestion;
value = pointValue;
options = "true/false";
//get the answer from the file
getline(cin,theAnswer);
answer = theAnswer;
}
void setNewQuestion(string theQuestion, int pointValue)
{
string theAnswer;
questiontype = "TF";
question = theQuestion;
value = pointValue;
options = "true/false";
//get the answer from user
cout<<"Enter answer true/false ";
getline(cin,theAnswer);
answer = theAnswer;
}
int getValue() //gets the point value of the question
{
return value;
}
string getQuestionType()// gets the type of question
{
return questiontype;
}
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, questiontype;
string answer;
string options;
int value;
};
class QuestionMC: public Question//class for multiple choice
{
public:
void setQuestion(string theQuestion, int pointValue)
{
string line;
questiontype = "MC";
getline(cin,line);
numberOfOptions = atoi(line.c_str());
cout<<numberOfOptions<<endl;
question = theQuestion;
value = pointValue;
//get the individual choice lines and load to options array
for (int count = 0; count<numberOfOptions;count++){
getline(cin,line);
options[count] = line;
}
//get the answer from the file and load into answer
getline(cin, line);
answer = line;
}
void setNewQuestion(string theQuestion, int pointValue)
{
string line;
questiontype = "MC";
//get the number of choices from the user
cout<<"Enter the number of choices: ";
getline(cin,line);
numberOfOptions = atoi(line.c_str());
question = theQuestion;
value = pointValue;
//get the individual choice lines and load to options array
for (int count = 0; count<numberOfOptions;count++){
cout<<" Enter next option: ";
getline(cin,line);
options[count] = line;
}
//get the answer from the user and load into answer
cout<<" Enter Answer: ";
getline(cin, line);
answer = line;
}
void printOptions()// prints the questions, options, and answer
{
//char first = 'A';
cout<<question<<endl;
for(int count = 0; count<numberOfOptions;count++){
cout<<options[count]<<" ";
}
cout<< answer << " ";
}
int getValue() //gets the point value of the question
{
return value;
}
string getQuestionType()// gets the type of question
{
return questiontype;
}
string getAnswer()// prints the answer
{
return answer;
}
private:
int numberOfOptions;
string question, answer;
string options[6];
string questiontype;
int value;
};
class Exam
{
public:
int loadExam()
{
//ifstream infile;
//string examName = exam;
ifstream infile("exam.txt"); ////////////////////////////////////////////////////////////////////////////added code
streambuf *cinbuf = cin.rdbuf(); //save old buf
cin.rdbuf(infile.rdbuf()); //redirect std::cin to infile.txt!
string line, theQuestion, questiontype, theAnswer;
int questionvalue;
//get the number of questions from the first line in the file
getline(cin,line);
numquestions = atoi(line.c_str());
for(int count = 0; count<numquestions;count++){
getline(cin,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 = atoi(line.substr(prev_pos, npos-prev_pos).c_str()); // Last word
//process a true/false question
if (questiontype == "TF")
{
myQuestions[count] = new QuestionTF;
getline(cin,theQuestion);
myQuestions[count]->setQuestion(theQuestion,questionvalue);
}
//process a multiple choice question
if (questiontype =="MC")
{
myQuestions[count] =new QuestionMC;
getline(cin,theQuestion);
myQuestions[count]->setQuestion(theQuestion,questionvalue);
}
}
cin.rdbuf(cinbuf); //restore cin to standard input
return numquestions;
}
void displayExamQuestions(int numquestions)
{
string qtype;
//print out the questions that have been processed
for(int count = 0; count<numquestions;count++)
{
qtype=myQuestions[count]->getQuestionType();
cout<<qtype<<" "<<myQuestions[count]->getValue()<<" ";
myQuestions[count]->printOptions();
cout<<" ";
}
}
private:
Question *myQuestions[10];
int numquestions;
};
int displayMenu(); //function prototype
int main() {
Exam myExam;
int numquestions, choice;
string examName = "exam.txt";
while((choice = displayMenu())!=3)
switch(choice)
{
case 1: numquestions = myExam.loadExam();
break;
case 2: myExam.displayExamQuestions(numquestions);
break;
default: cout<<"Invalid choice. Try again. ";
}
getchar();
return 0;
}
int displayMenu()
{
int choice;
cout<< "Enter your choice for this Exam."<<endl;
cout<< "1. Load Exam "<<endl;
cout<< "2. Display Exam "<<endl;
cout<< "3. Quit"<<endl;
cin>>choice;
return choice;
}
Explanation / Answer
#include #include #include #include #include #include #include #include #include using namespace std; //the files used ofstream outfile; // ERROR MESSAGERelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.