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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
class questions
{
private:
std::string question;
std::string answer;
int value;
std::string questionType;
public:
std::string getQuestion()//gets the question
{
return question;
}
virtual int getValue() //gets the point value of the question
{
return value;
}
virtual std::string getQuestionType()// gets the type of question
{
return questionType;
}
virtual void setQuestion(std::string answer, int value)
{
}
virtual void setNewQuestion(std::string answer, int value)
{
}
virtual void printOptions()
{
}
virtual std::string getAnswer()
{
return answer;
}
};
class QuestionTF : public questions// class for true and false questions
{
private:
std::string question;
std::string questiontype;
std::string answer;
std::string options;
int value;
public:
void setQuestion(std::string theQuestion, int pointValue)
{
std::string theAnswer;
questiontype = "TF";
question = theQuestion;
value = pointValue;
options = "true/false";
//get the answer from the file
std::getline(std::cin, theAnswer);
answer = theAnswer;
}
void setNewQuestion(std::string theQuestion, int pointValue)
{
std::string theAnswer;
questiontype = "TF";
question = theQuestion;
value = pointValue;
options = "true/false";
//get the answer from user
std::cout << "Enter answer true/false\n";
std::getline(std::cin, theAnswer);
answer = theAnswer;
}
int getValue() //gets the point value of the question
{
return value;
}
std::string getQuestionType()// gets the type of question
{
return questiontype;
}
void printOptions()//prints the options for that question
{
std::cout << question << std::endl;
std::cout << answer << std::endl;
}
std::string getAnswer()//outputs the answer for that question
{
return answer;
}
};
class QuestionMC : public questions//class for multiple choice
{
private:
int numberOfOptions;
std::string question, answer;
std::string options[6];
std::string questiontype;
int value;
public:
void setQuestion(std::string theQuestion, int pointValue)
{
std::string line;
questiontype = "MC";
std::getline(std::cin, line);
numberOfOptions = atoi(line.c_str());
std::cout << numberOfOptions << std::endl;
question = theQuestion;
value = pointValue;
//get the individual choice lines and load to options array
for (int count = 0; count < numberOfOptions; count++) {
std::getline(std::cin, line);
options[count] = line;
}
//get the answer from the file and load into answer
std::getline(std::cin, line);
answer = line;
}
void setNewQuestion(std::string theQuestion, int pointValue)
{
std::string line;
questiontype = "MC";
//get the number of choices from the user
std::cout << "Enter the number of choices: ";
std::getline(std::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++) {
std::cout << "\nEnter next option: ";
std::getline(std::cin, line);
options[count] = line;
}
//get the answer from the user and load into answer
std::cout << "\nEnter Answer: ";
std::getline(std::cin, line);
answer = line;
}
void printOptions()// prints the questions, options, and answer
{
//char first = 'A';
std::cout << question << std::endl;
for (int count = 0; count < numberOfOptions; count++) {
std::cout << options[count] << "\n";
}
std::cout << answer << "\n";
}
int getValue() //gets the point value of the question
{
return value;
}
std::string getQuestionType()// gets the type of question
{
return questiontype;
}
std::string getAnswer()// prints the answer
{
return answer;
}
};
class Quiz
{
private:
questions *myQuestions[10];
int numquestions;
public:
int loadQuiz()
{
//ifstream infile;
//string examName = exam;
std::ifstream infile("quiz.txt");
std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
std::cin.rdbuf(infile.rdbuf()); //redirect std::cin to infile.txt!
std::string line;
std::string theQuestion;
std::string questiontype;
std::string theAnswer;
int questionvalue;
//get the number of questions from the first line in the file
std::getline(std::cin, line);
numquestions = atoi(line.c_str());
for (int count = 0; count < numquestions; count++) {
std::getline(std::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
if (questiontype == "TF")
{
myQuestions[count] = new QuestionTF;
std::getline(std::cin, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
if (questiontype == "MC")
{
myQuestions[count] = new QuestionMC;
std::getline(std::cin, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
}
std::cin.rdbuf(cinbuf); //restore cin to standard input
return numquestions;
}
void displayQuizQuestions(int numquestions)
{
std::string qtype;
//print out the questions that have been processed
for (int count = 0; count < numquestions; count++)
{
qtype = myQuestions[count]->getQuestionType();
std::cout << qtype << " " << myQuestions[count]->getValue() << "\n";
myQuestions[count]->printOptions();
std::cout << "\n";
}
}
};
int menu()
{
int selection = 0;
std::cout << "Please select an option from the list below." << std::endl;
//display the menu
std::cout << "Menu";
std::cout << "======" << std::endl;
std::cout << "1 - Load quiz." << std::endl;
std::cout << "2 - Display quiz." << std::endl;
std::cout << "3 - Press 3 to exit." << std::endl << std::endl;
std::cout << "Enter selection: ";
// read user selection
//std::cin >> selection;
while (true)
{
std::cin >> selection;
if (std::cin.fail())
{
std::cerr << "Please enter an integer number." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
if (selection == 1 || selection == 2 || selection == 3) {
break;
}
std::cout << "The options are: \'1\'\'2\' or \'3\': ";
}
std::cin.ignore();
return selection;
}
int main()
{
Quiz theQuiz;
int numquestions;
int selection = 0;
std::string quizName = "quiz.txt";
while ((selection = menu()) !=3)
switch (selection)
{
case 1:
std::cout << "You selected - Load quiz." << std::endl;
numquestions = theQuiz.loadQuiz();
break;
case 2:
std::cout << "You selected - Display quiz." << std::endl;
theQuiz.displayQuizQuestions(numquestions);
break;
case 3:
break;
//if 1, 2, or 3 is not selected then go to the default case
default: std::cout << "Invalid selection. Please try again.\n";
// no break in the default case
}
std::cout << std::endl << std::endl;
return 0;
}
|