If/Else Help
Apr 19, 2016 at 5:42pm UTC
I am creating a geography test with a menu system. I'm using if/else statements to either ask the user to re-submit their answer or move on to the next question. When I ran the code, it only asked the first question. After I entered the correct answer, it didn't move on to the next question. I'm probably making a simple mistake but I can't seem to figure out what I'm doing wrong. Any help is appreciated!
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
// 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?\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 answerE;
cout << messageE;
cout << "1) North America\n" ;
cout << "2) Antarctica\n" ;
cout << "3) Asia\n" ;
cout << "4) Europe\n\n" ;
cin >> answerE;
if (answerE != 3 && answerE != 4)
{
cout << "That is incorrect! Please try again.\n" ;
cout << messageE;
cin >> answerE;
}
else
{
cout << "The test is over! Press any key to exit." ;
}
}
}
}
}
cin.get();
return 0;
}
Apr 19, 2016 at 6:08pm UTC
Worked fine for me as long as I entered the correct answers (subject to the below comments):
Lines 27-32: If you enter the wrong answer, you prompt for the answer again, but ignore whatever is entered.
Line 81: That if statement is not going to work as you expect. If answerD is 1 (correct), answerD != 3 will be true, and you will display "incorrect answer".
Line 99: Instructions say enter TWO letters. You're reading one number.
I'd suggest using a function to get the correct answer.
1 2 3 4 5 6 7 8 9 10 11 12
void get_correct_answer (int correct_ans)
{ int ans;
while (1)
{ cin >> ans;
if (ans == correct_ans)
{ cout << "That's correct" << endl;
return ;
}
cout << That's incorrect" << endl;
}
}
That way, you can get rid of your nested ifs.
Doesn't handle multiple answers though (not that your logic is correct for multiple answers).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Question A.
const string questionA = "A. What continent is Estonia in? \n\n" ;
cout << questionA;
cout << "1) South America\n" ;
cout << "2) Europe\n" ;
cout << "3) Asia\n" ;
cout << "4) North America\n\n" ;
get_correct_answer (2);
// Question B.
const string messageB = "B. What is the capital of Canada?\n\n" ;
cout << messageB;
cout << "1) Vancouver, BC\n" ;
cout << "2) Calgary, AB\n" ;
cout << "3) Ottawa, ON\n" ;
cout << "4) Toronto, ON\n\n" ;
get_correct_answer (3);
Topic archived. No new replies allowed.