Function Selection/Menu System Help
May 1, 2016 at 4:11am UTC
Hey everyone, I'm having trouble with this project I'm working on. I'm writing a program that lets the user choose between 3 different quizzes they can take. I split up the quizzes into their own functions. In the int main() function, I ask the user to select the number for the quiz they want to take. When I run the program and select a quiz, the program just ends rather than going to the function for the selected quiz. Any ideas on how to fix this are greatly appreciated! (I did not include the third function below due to the maximum character limit.)
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string name;
cout << "Hello! Please enter your name: " ;
getline(cin, name);
int quizSelection;
cout << "Please make a selection. Enter 1, 2, or 3\n" ;
cout << "1. Geography Quiz\n" ;
cout << "2. Basic Math Quiz\n" ;
cout << "3. Random Trivia Quiz\n" ;
cin >> quizSelection;
if (quizSelection == 1)
{
void geographyQuiz();
}
else if (quizSelection == 2)
{
void basicMathQuiz();
}
else if (quizSelection == 3)
{
void randomTriviaQuiz();
}
cin.get();
return 0;
}
void geographyQuiz()
{
// Intro to test.
cout << "Let's test your geography skills!\n" ;
cout << "I will ask you five questions.\n" ;
cout << "Press the number(s) on your keypad that corresponds with your answer,\n" ;
cout << "then press 'Enter' to select an answer. Good Luck!\n" ;
cout << "\n\n\n" ;
cout << "Geography Test" << endl;
cout << "--------------" << endl;
// Question A.
const string questionA = "A. What continent is Estonia in? \n\n" ;
int answerA;
cout << questionA;
cout << "1) South America\n" ;
cout << "2) Europe\n" ;
cout << "3) Asia\n" ;
cout << "4) North America\n\n" ;
cin >> answerA;
if (answerA != 2)
{
cout << "That is incorrect! Please try again.\n" ;
cout << questionA;
cin >> answerA;
}
else
{
// Question B.
const string messageB = "B. What is the capital of Canada?\n\n" ;
int answerB;
cout << messageB;
cout << "1) Vancouver, BC\n" ;
cout << "2) Calgary, AB\n" ;
cout << "3) Ottawa, ON\n" ;
cout << "4) Toronto, ON\n\n" ;
cin >> answerB;
if (answerB != 3)
{
cout << "That is incorrect! Please try again.\n" ;
cout << messageB;
cin >> answerB;
}
else
{
// Question C.
const string messageC = "C. In what country is Cape Town?\n\n" ;
int answerC;
cout << messageC;
cout << "1) South Africa\n" ;
cout << "2) China\n" ;
cout << "3) Hungary\n" ;
cout << "4) Egypt\n\n" ;
cin >> answerC;
if (answerC != 1)
{
cout << "That is incorrect! Please try again.\n" ;
cout << messageC;
cin >> answerC;
}
else
{
// Question D. Two are correct. Only select one correct answer.
const string messageD = "D. Which of the following cities is in California? (Two are correct)\n\n" ;
int answerD;
cout << messageD;
cout << "1) Fresno\n" ;
cout << "2) Indianapolis\n" ;
cout << "3) Oakland\n" ;
cout << "4) Chicago\n\n" ;
cin >> answerD;
if (answerD != 1 && answerD != 3)
{
cout << "That is incorrect! Please try again.\n" ;
cout << messageD;
cin >> answerD;
}
else
{
// Question E. Two are correct. Select both correct answers.
const string messageE = "E. Russia is in two continents. Which two? (Select more than one letter)\n\n" ;
int answerE1;
int answerE2;
cout << messageE;
cout << "1) North America\n" ;
cout << "2) Antarctica\n" ;
cout << "3) Asia\n" ;
cout << "4) Europe\n\n" ;
cin >> answerE1;
cin >> answerE2;
if (answerE1 != 3 || answerE1 != 4 && answerE2 != 3 || answerE2 != 4)
{
cout << "That is incorrect! Please try again.\n" ;
cout << messageE;
cin >> answerE1;
cin >> answerE2;
}
else
{
cout << "The test is over! Press any key to exit." ;
}
}
}
}
}
}
void basicMathQuiz()
{
cout << "Welcome! Let's test your basic math skills!\n\n" ;
cout << "I will ask you five questions.\n" ;
cout << "Press the number(s) on your keypad that corresponds with your answer,\n" ;
cout << "then press 'Enter' to select an answer. Good Luck!\n" ;
cout << "\n\n\n" ;
cout << "Basic Math Quiz" << endl;
cout << "---------------" << endl;
// Question A
const string mathMessageA = "A. What is 2+20?\n\n" ;
int mathAnswerA;
cout << mathMessageA << endl;
cout << "1. 22\n" ;
cout << "2. 48\n" ;
cout << "3. 77\n" ;
cout << "4. 15\n" ;
cin >> mathAnswerA;
if (mathAnswerA != 1)
{
cout << "That is incorrect! 20% has been deducted from your score.\n" ;
}
else
{
cout << "That is correct!\n\n" ;
}
// Question B.
const string mathMessageB = "B. What is 2+2?\n\n" ;
int mathAnswerB;
cout << mathMessageB << endl;
cout << "1. 22\n" ;
cout << "2. 48\n" ;
cout << "3. 4\n" ;
cout << "4. 15\n" ;
cin >> mathAnswerB;
if (mathAnswerB != 3)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n" ;
}
else
{
cout << "That is correct!\n\n" ;
}
// Question C.
const string mathMessageC = "What is 5-5?\n\n" ;
int mathAnswerC;
cout << mathMessageC << endl;
cout << "1. 22\n" ;
cout << "2. 48\n" ;
cout << "3. 4\n" ;
cout << "4. 0\n" ;
cin >> mathAnswerC;
if (mathAnswerC != 4)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n" ;
}
else
{
cout << "That is correct!\n\n" ;
}
// Question D
const string mathMessageD = "Pick an equation that equals 0. Two could be right.\n\n" ;
int mathAnswerD;
cout << mathMessageD << endl;
cout << "1. 0x0\n" ;
cout << "2. 1+8\n" ;
cout << "3. 75-75\n" ;
cout << "4. 100+48\n" ;
cin >> mathAnswerD;
if (mathAnswerD != 1 && mathAnswerD != 3)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n" ;
}
else
{
cout << "That is correct!\n\n" ;
}
// Question E.
const string mathMessageE = "Pick the two equations that equal 20.\n\n" ;
int mathAnswerE1;
int mathAnswerE2;
cout << mathMessageE << endl;
cout << "1. 5x4\n" ;
cout << "2. 1+8\n" ;
cout << "3. 20x1\n" ;
cout << "4. 100+48\n" ;
cin >> mathAnswerE1;
cin >> mathAnswerE2;
if (mathAnswerE1 != 1 || mathAnswerE1 != 3 && mathAnswerE2 != 1 || mathAnswerE2 != 3)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n" ;
}
else
{
cout << "That is correct!\n\n" ;
}
}
May 1, 2016 at 4:31am UTC
you need to add the Function prototypes (Because your first function is main) like this:
1 2 3 4 5 6 7 8 9
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Function Prototypes.
void geographyQuiz();
void basicMathQuiz();
void randomTriviaQuiz();
Then take a look at lines 19-30. When you call the function, you do not put the function type. For example:
1 2 3 4 5 6 7 8 9 10 11 12
if (quizSelection == 1)
{
geographyQuiz(); // Delete the function type (void), when calling functions.
}
else if (quizSelection == 2)
{
basicMathQuiz(); // Same here.
}
else if (quizSelection == 3)
{
randomTriviaQuiz(); // Same here.
}
PS: It looks that you figured it out. Great!
Last edited on May 1, 2016 at 4:32am UTC
May 1, 2016 at 4:40am UTC
Thanks for your advice! It's funny, I tried adding function prototypes and I solved my problem and then I noticed that you replied and told me to do exactly what I did!
May 1, 2016 at 6:01am UTC
It does feel better when you can figure it out on your own, though. Good job!!!
Topic archived. No new replies allowed.