1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
/* Class files for the Question Classes. Designed so that the base class could be used as the collection class later in the program. Base Question Class has several methods that are identified as virtual as they will be redefined later in the hierarchy to specify the types of questions that will be read from or written to files */
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;
};
//The class for True/False Questions
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\n";
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;
};
//The class for Multiple Choice Questions
class QuestionMC: public Question//class for multiple choice
{
public:
void setQuestion(string theQuestion, int pointValue)
{
string line;
questiontype = "MC";
//get the number of choices from the file
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++){
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<<"\nEnter next option: ";
getline(cin,line);
options[count] = line;
}
//get the answer from the user and load into answer
cout<<"\nEnter 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<<first++ <<". "<<options[count]<<"\n";
}
cout<< answer << "\n";
}
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;
};
// Function prototypes for the functions supporting the main program functionality
int loadArray(Question *myQuestions[]);
int addQuestion(Question *myQuestions[], int numquestions);
void printQuizQuestions(Question *myQuestions[], int numquestions);
void writeExamQuestionFile(Question *myQuestions[], int numquestions);
// Function to load the array of Questions from the input file
int loadArray(Question *myQuestions[])
{
string line;
string questiontype, theQuestion;
int numquestions, 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);
}
}
return numquestions;
}
//Function to add questions by getting from user and using he interactive methods //added to the classes.
int addQuestion(Question *myQuestions[], int numquestions)
{
int questionvalue;
int count = numquestions;
string theQuestion,line, questiontype;
//Asking user how many questions user wants to create
//create loop
for (int count = 0; count<numquestions;count++){
if (numquestions== 0){
cout<<"Enter the amount of questions for the exam\n";
getline(cin,line);
numquestions = atoi(line.c_str());
numquestions[count] = line;
} else
{
cout<<"Enter the amount of questions for the exam\n";
getline(cin,line);
numquestions = atoi(line.c_str());
numquestions[count] = line;
}
}
cout<<"Enter the Question type and value\n";
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;
cout<<" \nEnter the Question: ";
getline(cin,theQuestion);
myQuestions[count]->setNewQuestion(theQuestion,questionvalue);
}
//process a multiple choice question
if (questiontype =="MC")
{
myQuestions[count] =new QuestionMC;
cout<<" \nEnter the Question: ";
getline(cin,theQuestion);
myQuestions[count]->setNewQuestion(theQuestion,questionvalue);
}
return ++numquestions;
}
// Function to print out the Quiz questions from the Array. Uses methods from the
// classes developed to allow this printing to be done generically to screen or file.
void printQuizQuestions(Question *myQuestions[], int numquestions)
{
//print out the questions that have been processed
for(int count = 0; count<numquestions;count++)
{
myQuestions[count]->printOptions();
cout<<"\n";
}
}
|