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
|
#include <iostream>
#include <string>
using namespace std;
// new function to make lower case
string & makeLowerCase ( string & String );
int main ( )
{
string catchGarbage; // This is used for exiting purposes.
string userInput = ""; // changed "start" to "userInput
const int NQS = 10;
string userAnswers[NQS];
string quizQuestions[NQS];
string quizAnswers[NQS];
// Question 1
quizQuestions[0] =
"Question 1:\n"
"What is Effie Trinket's favourite quote?\n\n"
"1 = 'Let the odds be in your favour'\n"
"2 = 'Shall the odds be ever in your favour'\n"
"3 = 'May the odds be ever in your favour'\n"
"4 = 'May the odds last forever in your favour'\n";
quizAnswers[0] = "3";
// Question 2
quizQuestions[1] =
"Question 2:\n"
"What district are Cato and Clove from?\n\n"
"1\n"
"2\n"
"3\n"
"4\n"
"5\n"
"6\n"
"7\n"
"8\n"
"9\n"
"10\n"
"11\n"
"12\n";
quizAnswers[1] = "2";
// Question 3
quizQuestions[2] =
"Question 3:\n"
"How many tributes are chosen each year to participate in the"
" Hunger Games?\n\n"
"2\n"
"4\n"
"6\n"
"8\n"
"10\n"
"12\n"
"14\n"
"16\n"
"18\n"
"20\n"
"22\n"
"24\n"
"26\n"
"28\n"
"30\n";
quizAnswers[2] = "24";
// Question 4
quizQuestions[3] =
"Question 4:\n"
"Who is the first tribute Katniss kills directly?\n\n"
"1 = Marvel\n"
"2 = Glimmer\n"
"3 = Cato\n"
"4 = Clove\n"
"5 = Foxface\n"
"6 = Thresh\n"
"7 = Rue\n";
quizAnswers[3] = "1";
// Question 5
quizQuestions[4] =
"Question 5:\n"
"What is Prim's cat's name?\n\n"
"1 = Furball\n"
"2 = Honeyball\n"
"3 = Buttercup\n"
"4 = Lady\n"
"5 = Butterball\n";
quizAnswers[4] = "3";
// Question 6
quizQuestions[5] =
"Question 6:\n"
"How old was Katniss when she volunteered as tribute?\n\n"
"12\n"
"13\n"
"14\n"
"15\n"
"16\n"
"17\n"
"18\n";
quizAnswers[5] = "16";
// Question 7
quizQuestions[6] =
"Question 7:\n"
"What is Gale's hunting specialty?\n\n"
"1 = Gun\n"
"2 = Bow & Arrow\n"
"3 = Snares\n"
"4 = Machete\n";
quizAnswers[6] = "3";
// Question 8
quizQuestions[7] =
"Question 8:\n"
"Every how many years does a Quarter Quell take place?\n\n"
"5\n"
"10\n"
"15\n"
"20\n"
"25\n"
"30\n"
"35\n"
"40\n"
"45\n"
"50\n"
"55\n";
quizAnswers[7] = "25";
// Question 9
quizQuestions[8] =
"Question 9:\n"
"Which Hunger Games did Haymitch win? Was it a Quarter Quell?\n\n"
"1 = 34th, No\n"
"2 = 34th, Yes\n"
"3 = 48th, No\n"
"4 = 48th, Yes\n"
"5 = 50th, Yes\n"
"6 = 50th, No\n"
"7 = 52nd, No\n"
"8 = 52nd, Yes\n"
"9 = 54th, No\n";
quizAnswers[8] = "5";
// Question 10
quizQuestions[9] =
"Question 10:\n"
"What is Gale's sister's name?\n\n"
"1 = Victoria\n"
"2 = Lily\n"
"3 = Posy\n"
"4 = Emily\n";
quizAnswers[9] = "3";
// Other variables
int a = 0; // set these right away to 0.
int s = 0;
cout << "Type 'Go' to begin the quiz, 'Help' for instructions, or 'Exit' to"
"quit\n" << endl;
cin >> userInput;
cout << "\n" << endl;
if ( makeLowerCase ( userInput ) == "exit" )
{
// Using our "makeLowerCase" function, we can change all the letters
// to lowercase whenever we need.
cout << "Thanks for taking the quiz! Press Enter to exit\n";
// cin.ignore ( );
// cin.get ( );
getline ( cin, catchGarbage );
// Using ignore and get is perfectly fine, and some people will argue
// that it is the better way. I like to use getline.
// It's up to your preference.
//return 0;
// This return is unnecessary, because no other if statement will
// evaluate to true after this.
}
if ( makeLowerCase ( userInput ) == "help" )
{
cout << "All questions require a number as an answer. Some questions"
" ask for a one-digit answer, "
<< "and others a two-digit number. An example of a question"
" that requires a one-digit number "
<< "as an answer could be: 1 = What colour is grass. In this"
" case, you would type '1' to choose "
<< "this answer. Here is an example of a possible answer of a"
" question which requires a two-"
<< "digit number as an answer: 74. Notice that there is no '='"
" or words, but just a number, "
<< "unlike a question that asks for a one-digit number as an"
" answer\n" << endl;
cout << "Type 'Go' to begin the quiz or 'Exit' to quit\n" << endl;
cin >> userInput;
cout << "\n" << endl;
}
if ( makeLowerCase ( userInput ) == "go" )
{
int currentQuestion;
for ( currentQuestion = 0; currentQuestion < NQS; currentQuestion++ )
{
cout << quizQuestions[currentQuestion] << endl;
cin >> userInput;
cout << endl << endl;
// check for "exit" right away.
if ( makeLowerCase ( userInput ) == "exit" )
{
// now we can check if userInput is == "exit".
cout << "Thanks for taking the quiz! Press Enter to exit\n";
getline ( cin, catchGarbage );
break;
}
userAnswers[currentQuestion] = userInput;
if ( userAnswers[currentQuestion] == quizAnswers[currentQuestion] )
{
a++; // you can increment the score like this.
}
}
// If all the questions were successfully asked and answered, display
// the score. Otherwise, the program exits.
if ( currentQuestion == NQS )
{
s = a * 10;
if ( s == 100 )
cout << "Congratulations! You got 100% of the answers right!"
"\n" << endl;
else
cout << "You answered " << s << "% of questions correct!\n"
<< endl;
}
}
return 0;
// Like the other guy said, this isn't needed. However, there is now no
// confusion as to where the program exits.
}
string & makeLowerCase ( string & String )
{
// I used String as the parameter name because I don't know what else to
// call it. You can change the name if you want. Here we need to loop
// through String and make every letter lower case.
for ( int index = 0; index < String.length ( ); index++ )
{
tolower ( String[index] );
}
// After all the letters are converted, return the modified version.
return String;
}
|