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
|
class Quiz : public questions
{
private:
std::string fileName;
//questions *myQuestions[10];
//int numberOfQuestions;
std::string theAnswer;
std::string qtype;
std::string theAnswerIs;
std::string ans[6];
protected:
int score = 0;
questions *myQuestions[6];
int numberOfQuestions;
int pointValue[6];
int totalPossiblePoints = 0;
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 if 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;
while (true)//error check to ensure the file has an acceptable amount of questions
{
//get the number of questions from the first line in the file
//std::getline(infile, line);
std::getline(std::cin, line);
numberOfQuestions = atoi(line.c_str());
if (numberOfQuestions != 6)
{
std::cout << "Error! The quiz has too many or too litte questions. The desired amount is \'6\'" << std::endl;
std::cout << "Please check the file to ensure there is an acceptable amount of questions." << std::endl;
throw;
}
else
{
break;
}
}
for (int count = 0; count < numberOfQuestions; 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")//do this if questiontype = TF
{
myQuestions[count] = new tfQuestion;
//std::getline(infile, line);
std::getline(std::cin, theQuestion);
myQuestions[count]->setQuestion(theQuestion, questionvalue);
}
if (questiontype == "MC")//do this 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 numberOfQuestions;
}
void displayQuizQuestions(int numquestions)
{
//print out the questions that have been processed
for (int i = 0; i < numquestions; i++)
{
qtype = myQuestions[i]->getQuestionType();
std::cout << qtype << " " << myQuestions[i]->getValue() << "\n";
myQuestions[i]->printOptions();
theAnswerIs = myQuestions[i]->getAnswer();
std::cout << theAnswerIs << std::endl;
std::cout << "\n";
std::cout << "Please enter your answer: ";
std::cin >> ans[i];
std::cout << ans[i].size() << "\n" << theAnswerIs.size() << std::endl;
if (!theAnswerIs.empty() && theAnswerIs[theAnswerIs.size() - 1] == '\r')
theAnswerIs.erase(theAnswerIs.size() - 1);
std::cout << ans[i].size() << "\n" << theAnswerIs.size() << std::endl;
if (theAnswerIs == ans[i])
{
std::cout << "You are correct, the answer is: " << theAnswerIs << '\n';
score++;
std::cout << "Your current score is " << score << std::endl;
}
else
{
std::cout << "Incorrect, the correct answer is: " << theAnswerIs << std::endl;
std::cout << "Your score is " << score << std::endl;
}
}
/* for (int i = 0; i < numquestions; i++)
{
pointValue[i] = myQuestions[i]->getValue();
}
for (int i = 0; i < 6; i++)
{
std::cout << "the point value of each question is " << pointValue[i] << std::endl;
totalPossiblePoints += pointValue[i];
std::cout << "The total possible points are: " << totalPossiblePoints << std::endl;
}
std::cout << totalPossiblePoints << std::endl;
//delete myQuestions[10];
}
*/
//Above works if I uncomment it to display the total points possible based on what the question value is in the file
//but I cant get it to work when trying to implement in the student class...
/*
void getPointValue(int numquestions)
{
for (int i = 0; i < numquestions; i++)
{
pointValue[i] = myQuestions[i]->getValue();
}
for (int i = 0; i < 6; i++)
{
std::cout << "the point value of each question is " << pointValue[i] << std::endl;
totalPossiblePoints += pointValue[i];
std::cout << "The total possible points are: " << totalPossiblePoints << std::endl;
}
std::cout << totalPossiblePoints << std::endl;
}
*// above I was trying to create a function to call from student class which I couldn't get to work either
}
};
class Student : Quiz
{
private:
int pointsPossible = 0;
int pointsScored = 0;
std::string name;
int yourScore = 0;
//int totalScore;
public:
void student()
{
pointsPossible = getPointValue();
std::cout << "Please enter your name" << std::endl;
std::getline(std::cin, name);
std::cout << "Hello " << name << std::endl
<< "The total possible points are: " << pointsPossible << std::endl
<< "You scored " << score << " points out of " << pointsPossible << " possible points." << std::endl;
}
int Score()
{
yourScore = Quiz::score;
return score;
}
int getPointValue()
{
for (int i = 0; i < numberOfQuestions; i++)
{
Quiz::pointValue[i] = Quiz::myQuestions[i]->getValue();
}
for (int i = 0; i < 6; i++)
{
std::cout << "the point value of each question is " << pointValue[i] << std::endl;
totalPossiblePoints += pointValue[i];
std::cout << "The total possible points are: " << totalPossiblePoints << std::endl;
}
std::cout << totalPossiblePoints << std::endl;
return totalPossiblePoints;
}
};
|