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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <limits>
//#include <vector>
//#include <iterator>
class questions//base class
{
protected:
std::string question;
std::string answer;
int value;
std::string questionType;
public:
std::string getQuestion()
{
return question;
}
virtual int getValue()
{
return value;
}
virtual std::string getQuestionType()
{
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 tfQuestion : public questions
{
private:
std::string question;
std::string options;
public:
void setQuestion(std::string theQuestion, int pointValue)
{
std::string theAnswer;
questionType = "TF";
question = theQuestion;
value = pointValue;
options = "true/false";
std::getline(std::cin, theAnswer);
answer = theAnswer;
}
int getValue()
{
return value;
}
std::string getQuestionType()
{
return questionType;
}
std::string getQuestion()
{
return question;
}
void printOptions()
{
std::cout << question << std::endl;
std::cout << answer << std::endl;
}
std::string getAnswer()
{
return answer;
}
};
class mcQuestion : public questions//subclass of questions class for multiple choice questions
{
private:
int numberOfOptions;
//std::vector<std::string> options;
std::string mcAnswers[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());
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);
mcAnswers[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);
mcAnswers[count] = line;
}
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 << mcAnswers[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:
std::string fileName;
//std::vector<std::string> myquestions;
questions *myQuestions[10];
int numquestions;
public:
int loadQuiz()
{
std::ifstream infile;
infile.clear();
//checking to see if user specified file exist, as well as making sure the user has used the correct format
while (!infile.is_open())
{
std::cout << "Please enter the .txt file name: " << std::endl;
std::getline(std::cin, fileName);
//fileName += ".txt";
infile.open(fileName.c_str(), std::ios::in | std::ios::binary);
if (infile)
break;
std::cout << "Either the file doesn't exist or you forgot the .txt extension. ";
std::cout << "Please try again. " << std::endl << std::endl;//promt user to try again is the file name is invalid
}
//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(infile, line);
std::getline(std::cin, line);
numquestions = atoi(line.c_str());
for (int count = 0; count < numquestions; count++)
{
//std::getline(infile, line);
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")
{
std::getline(infile, theQuestion);
myQuestions[count] = new QuestionTF ();
}*/
if (questiontype == "TF")
{
myQuestions[count] = new tfQuestion;
//std::getline(infile, line);
std::getline(std::cin, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
if (questiontype == "MC")
{
myQuestions[count] = new mcQuestion;
//std::getline(infile, line);
std::getline(std::cin, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
}
std::cin.rdbuf(cinbuf);//restore cin to standard input
infile.close();//close the file stream
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";
delete myQuestions[count];//dealocate
}
}
};
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)//while loop to ensure user selects an option on the menu
{
std::cin >> selection;
if (std::cin.fail())//if() to make sure an endless loop doesn't happen if a char is entered.
{
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)//if() to make sure only the options on the menu are avaible to select
{
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 <<std::endl;
std::cout << "========== Quiz: ==========" << std::endl;
theQuiz.displayQuizQuestions(numquestions);
break;
case 3:
break;
default: std::cout << "Invalid selection. Please try again.\n";
}
std::cout << std::endl << std::endl;
std::cout << "Thank you for using this program. Goodbye.";
std::cin.get();
return 0;
}
|