Need help Stuck on C++: we have to add the ability to take an exam to our progra
ID: 3903552 • Letter: N
Question
Need help Stuck on C++: we have to add the ability to take an exam to our programs for my class. I have added a student class and added the option to select take exam. I need help adding the functionality of taking the exam to the Exam class. Below is my code:
My code:
//=============================================================================
// Name: CS215_Joseph_Hoffman_IP4
// Author: Joseph Hoffman
// Description: This program allows the user to read in an exam file, display
// said exam file and loop through infinitely until the user exits the program
// by pressing 'q' or 'Q'. This causes the program to gracefully end.
//=============================================================================
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// helper method
string trim(string str) {
if (str[str.size() - 1] == ' ')
str.resize(str.size() - 1);
if (str[str.size() - 1] == ' ')
str.resize(str.size() - 1);
return str;
}
class Question {
protected:
string question;
int value;
public:
Question() {
}
Question(string theQuestion, int points) {
question = theQuestion;
value = points;
}
string getQuestion() {
return question;
}
int getValue() {
return value;
}
virtual void print() = 0;
virtual void printOptions() = 0;
virtual string getAnswer() = 0;
};
class QuestionTF : public Question {
private:
string answer;
public:
// store answers and pass question and points back to the base class -
// Question
QuestionTF(string theQuestion, int points, string theAnswer)
: Question(theQuestion, points) {
answer = theAnswer;
}
void print() {
cout << getQuestion() << endl;
cout << "Point Value: " << getValue() << endl;
cout << "The answer is: " << getAnswer() << endl;
cout << endl;
}
string getAnswer() {
return answer;
}
void printOptions() {
cout << "(true/false)" << endl;
}
};
class QuestionMC : public Question {
private:
string answer;
vector options;
public:
// store answer and pass question and points back to the base class -
// Question
QuestionMC(string theQuestion, int points, string theAnswer)
: Question(theQuestion, points) {
answer = theAnswer;
}
// stores multiple choice options received from reading a file
void addOption(string anOption) {
options.push_back(anOption);
}
void print() {
cout << getQuestion() << endl;
cout << "Point Value: " << getValue() << endl;
cout << "Options: ";
printOptions();
cout << "Answer: " << getAnswer() << endl;
cout << endl;
}
// print all elements in vector
void printOptions() {
for (int i = 0; i < options.size(); ++i) {
cout << options[i] << endl;
}
}
string getAnswer() {
return answer;
}
};
class Student
{
private:
int pointsPossible;
int pointsScored;
public:
Student();
void addPointsPossible(int);
void addPointsScored(int);
int getPointsPossible();
int getPointsScored();
};
Student::Student()
{
pointsPossible = 0;
pointsScored = 0;
}
void Student::addPointsPossible(int points)
{
pointsPossible = pointsPossible + points;
}
void Student::addPointsScored(int points)
{
pointsScored = pointsScored + points;
}
int Student::getPointsPossible()
{
cout << "The maximum points possible is: " << pointsPossible << endl;
return pointsPossible;
}
int Student::getPointsScored()
{
cout << "You scored: " << pointsScored << " on this exam." << endl;
return pointsScored;
};
class Exam {
protected:
vector questions;
int question;
public:
Exam() {}
bool loadExam(std::string filename) // function takes a file name
{
Question *question;
vector answerChoices;
ifstream filein;
int numQuestions, pointValue, numChoices;
string questionType, strQuestion, strAnswer, choice, num;
filein.open(filename);
while (!filein.is_open()) {
cout << endl << "Unable to open the file."
<< "Try again." << endl;
cout << "Enter filename: ";
cin >> filename;
filein.open(filename);
}
getline(filein, num); // get number of questions
numQuestions = stoi(trim(num)); // convert string num to integer num
for (int i = 0; i < numQuestions; i++) {
questionType = " ";
filein >> questionType;
questionType = trim(questionType); // get question type
getline(filein, num); // get question points
pointValue = stoi(trim(num)); // convert points to int
getline(filein, strQuestion); // get the question
if (questionType == "TF" || questionType == "tf") {
getline(filein, strAnswer); // get the answer
// create new object
QuestionTF *obj =
new QuestionTF(trim(strQuestion), pointValue, trim(strAnswer));
questions.push_back(obj); // push the object into the vector
}
else if (questionType == "MC" || questionType == "mc") {
vector options;
// read answerChoices
getline(filein, num); // get question points
numChoices = stoi(trim(num)); // convert points to int
for (int j = 0; j
getline(filein, choice);
options.push_back(trim(choice));
}
getline(filein, strAnswer); // get the answer
QuestionMC *obj =
new QuestionMC(trim(strQuestion), pointValue, trim(strAnswer));
questions.push_back(obj);
for (int j = 0; j
obj->addOption(options[j]);
}
}
else // Question type not TF or MC
{
cout << "Error, unknown question type! ";
}
}
filein.close();
cout << "Exam loaded successfully! ";
return true;
}
void displayExam() {
for (int i = 0; i < questions.size(); ++i) {
cout << "Question " << i + 1 << ":";
questions[i]->print();
}
}
};
int main() {
Exam theExam;
string filename;
Student student;
char menuSelection;
do {
cout << "Welcome to the Testing Center" << endl;
cout << "=============================" << endl;
cout << " Main Menu " << endl;
cout << "_____________________________" << endl;
cout << "What would you like to do? " << endl;
cout << "1 - Load an exam" << endl;
cout << "2) Take Exam " << endl;
cout << "3) Show Exam Results " << endl;
cout << "Q - Quit" << endl;
cout << "Please Enter Option : ";
cin >> menuSelection;
switch (menuSelection) {
case '1':
cout << endl;
cout << "You selected Load an exam ";
cout << "What is the name of the exam file? ";
cout << "Enter filename: ";
cin >> filename;
if (theExam.loadExam(filename) == false) {
cout << "Load exam failed.Please check testBank file!" << endl;
}
break;
case '2':
cout << "Take exam ";
theExam.takeExam(student);
break;
case '3':
cout << endl;
cout << "Show exam Results ";
cout << endl << "=========================== Exam Resaults =================================" << endl << endl;
theExam.displayExam();
cout << "________________________________________________________________________" << endl << endl;
break;
case 'q':
case 'Q':
cout << endl << "Thank you and good-bye " << endl;
break;
default:
cout << endl << "Please try again"
<< " ";
}
cout << endl;
} while (tolower(menuSelection) != 'q');
system("pause");
return 0;
};
Example from proffesor:
class Exam
{
protected:
vector <Question*> questions;
int question;
public:
Exam() {}
bool loadExam(string)
void displayExam(Student &std)
{
std.getPointsPossible();
std.getPointsScored();
}
bool takeExam(Student &);
};
bool Exam::loadExam(string filename)
{
// WRITE YOUR CODE HERE!!!
}
bool Exam::takeExam(Student &std)
{
// WRITE YOUR CODE HERE!!!
}
Please help me add this functionality to my code. Please use my code and only add what is needed. I do not want it completely changed if can be. Please help ASAP!
Explanation / Answer
Your code looks almost correct so I need to know what exact functionality you are looking to add. In the good case you are doing the right thing just be more specific.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.