I have the following assignment: Part 1: Begin with the running program from you
ID: 3880566 • Letter: I
Question
I have the following assignment:
Part 1: Begin with the running program from your Phase 4 Individual Project where the examination question class hierarchy was fully implemented in a menu-driven program. An exam class was developed to load the exam from a file and display each question to the screen.
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.
You should consider creating an additional class Student that will track student's score through methods such as addPointsPossible, addPointsScored, getPointsPossible, and getPointsScored. You should also enhance your Exam class to include methods getPointValue and getAnswer. You may also want to add a method to only display one question at a time, such as displayQuestion.
THIS IS MY CODE SO FAR. I AM GETTING THE ERRORS THAT I HAVE PUT NOTES ON THE SIDE FOR.
Any help in correcting this would be greatly appreciated. Thank you.
#include <iostream>
#include <cstdlib>
#include <exception>
#include <string>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
//the files used
ofstream outfile; <-------------------ERROR MESSAGE: ofstream incomplete does not name a type
ifstream infile;
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)
{
}
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)
{
string theAnswer;
question = theQuestion;
points = pointValue;
options = "true/false";
//get the answer from the file
getline(infile, 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 theQuestion, int pointValue)
{
string line;
//get the number of choices from the file
getline(infile, line);
numberOfOptions = stoi(line); <--------------ERROR MESSAGE: stoi not declared
question = theQuestion;
value = pointValue;
//get the individual choice lines and load to options array
for (int count = 0; count<numberOfOptions; count++){
getline(infile, line);
options[count] = line;
}
//get the answer from the file and load into answer
getline(infile, 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 << 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() {
Question *myQuestions[10];
string questiontype, questiontxt;
string answertxt, optiontxt;
int numquestions, questionvalue;
// this is the test bank test file
outfile.open("testbank2.txt");
if (outfile.is_open())
{
outfile << "4 ";
outfile << "TF 25 ";
outfile << "There are 50 states in America? ";
outfile << "true ";
outfile << "MC 25 ";
outfile << "Donald Trump is the President of the United States? ";
outfile << "4 ";
outfile << "A 1st ";
outfile << "B 30th ";
outfile << "C 40th ";
outfile << "D 45th ";
outfile << "D ";
outfile << "TF 25 ";
outfile << "1+2=3? ";
outfile << "true ";
outfile << “TF 25 ”;
outfile << “The ocean is orange? ”;
outfile << “false ”;
outfile.close();
}
else cout << "Unable to open file";
//opening the testbank file and processing as a question of each type
infile.open("testbank2.txt");
string line, theQuestion, theAnswer;
if (infile.is_open())
{
//get the number of questions from the first line in the file
getline(infile, line);
numquestions = stoi(line); <---------------------Error MESSAGE: stoi not declared
for (int count = 0; count<numquestions; count++){
getline(infile, 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)); <-------------------ERROR MESSAGE: stoi not declared
//process a true/false question
if (questiontype == "TF")
{
myQuestions[count] = new QuestionTF;
getline(infile, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
//process a multiple choice question
if (questiontype == "MC")
{
myQuestions[count] = new QuestionMC;
getline(infile, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
}
}
//print out the questions that have been processed
for (int count = 0; count<numquestions; count++)
{
myQuestions[count]->printOptions();
cout << endl;
}
getchar();
return 0;
}
Explanation / Answer
The problem is solved u compile the program with the below statement
g++ -std=c++0x filename.cpp
where u can solve the error stoi was not declared in this scope
The modified code is shown below
#include <iostream>
#include <cstdlib>
#include <exception>
#include <string>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
//the files used
ofstream outfile;
ifstream infile;
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)
{
}
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)
{
string theAnswer;
question = theQuestion;
points = pointValue;
options = "true/false";
//get the answer from the file
getline(infile, 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 theQuestion, int pointValue)
{
string line;
//get the number of choices from the file
getline(infile, line);
numberOfOptions=stoi(line);
question = theQuestion;
value = pointValue;
//get the individual choice lines and load to options array
for (int count = 0; count<numberOfOptions; count++){
getline(infile, line);
options[count] = line;
}
//get the answer from the file and load into answer
getline(infile, 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 << 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() {
Question *myQuestions[10];
string questiontype, questiontxt;
string answertxt, optiontxt;
int numquestions, questionvalue;
// this is the test bank test file
outfile.open("testbank2.txt");
if (outfile.is_open())
{
outfile << "4 ";
outfile << "TF 25 ";
outfile << "There are 50 states in America? ";
outfile << "true ";
outfile << "MC 25 ";
outfile << "Donald Trump is the President of the United States? ";
outfile << "4 ";
outfile << "A 1st ";
outfile << "B 30th ";
outfile << "C 40th ";
outfile << "D 45th ";
outfile << "D ";
outfile << "TF 25 ";
outfile << "1+2=3? ";
outfile << "true ";
outfile << "TF 25 ";
outfile << "The ocean is orange? ";
outfile << "false ";
outfile.close();
}
else
cout << "Unable to open file";
//opening the testbank file and processing as a question of each type
infile.open("testbank2.txt");
string line, theQuestion, theAnswer;
if (infile.is_open())
{
//get the number of questions from the first line in the file
getline(infile, line);
numquestions=stoi(line);
for (int count = 0; count<numquestions; count++){
getline(infile, 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));
//process a true/false question
if (questiontype == "TF")
{
myQuestions[count] = new QuestionTF;
getline(infile, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
//process a multiple choice question
if (questiontype == "MC")
{
myQuestions[count] = new QuestionMC;
getline(infile, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
}
}
//print out the questions that have been processed
for (int count = 0; count<numquestions; count++)
{
myQuestions[count]->printOptions();
cout << endl;
}
getchar();
return 0;
}