If and else if help.

I am coding a simple math game that is using a lot of if and elses. When the first question goes through if the answer != 12 is should say incorrect but it does nothing. Any help is appreciated.
first of all show us your code and PLEASE put it in [ code] [/ code] tags.
so i believe you should have something like this:
1
2
3
if (answer!=12) {
    std::cout<<"incorrect!"<<std::endl;
}
Here. The mess up is at line 42.

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
#include <iostream>
#include <cmath>
int main()

{
using namespace std;
int gamepick;
int aa1;
int aa2;

cout << "Welcome to Tyler's Math game." << endl;
cout << "Enter the number of the game you wish to play, then press enter." << endl;
cout << endl;
cout << "[1]Addition" << endl;
cout << "[2]Subtraction" << endl;
cout << "[3]Multiplication" << endl;
cout << "[4]Division" << endl;

cin >> gamepick;
cout << endl;

if(gamepick == 1){
             cout << "Welcome to Addition." << endl;
             cout << "You may always enter 'Quit' to quit to the game." << endl; 
             cout << "What is 5+7?" << endl;
             cin >> aa1;
             
                 if(aa1 == 12){
                            cout << "Correct!" << endl;
                            cout << "What is 12+24?" << endl;
                            }
                            cin >>aa2;
                            cout << endl;
                            if(aa2 == 36){
                                   cout << "Correct!" << endl;
                                   }
                                   
                                   else{
                                        cout << "Incorrect!" << endl;
                                        }
                            
                 if(aa1 != 12){
                         cout << "Incorrect!" << endl;
                         }
              }
              
if(gamepick == 2) {
             cout << "Welcome to Subraction." << endl;
             cout << "You may always enter 'Quit' to quit to the game." << endl;
             }
             
if (gamepick == 3){
             cout << "Welcome to Multiplication." << endl;  
             cout << "You may always enter 'Quit' to quit to the game." << endl; 
             }
     
if (gamepick == 4){
             cout << "Welcome to Division." << endl;
             cout << "You may always enter 'Quit' to quit to the game." << endl;
             }



system("pause");
return 0;

}
1
2
3
4
if(aa1 == 12){
                            cout << "Correct!" << endl;
                            cout << "What is 12+24?" << endl;
                            }


if ends here so it asks for input of aa2
Ok, so i would move
1
2
if(aa1 != 12){
                         cout << "Incorrect!" <<l end;

Up to the top of the code like this?
1
2
3
4
5
6
 if(aa1 == 12){
                            cout << "Correct!" << endl;
                            cout << "What is 12+24?" << endl;
                 if(aa1 != 12){
                         cout << "Incorrect!" <<l end;
                            }
why would you ask a new question without rating the previous one?
Use like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if(gamepick == 1){
             cout << "Welcome to Addition." << endl;
             cout << "You may always enter 'Quit' to quit to the game." << endl; 
             cout << "What is 5+7?" << endl;
             cin >> aa1;
             
                 if(aa1 == 12){
                            cout << "Correct!" << endl;
                            }
else {
                         cout << "Incorrect!" << endl;
                         }
cout << "What is 12+24?" << endl;
                            cin >>aa2;
                            cout << endl;
                            if(aa2 == 36){
                                   cout << "Correct!" << endl;
                                   }
                                   
                                   else{
                                        cout << "Incorrect!" << endl;
                                        }
                            
              }
I am really new to this so forgive me. Your code is doing the exact same thing as my first piece of code. It is not saying Incorrect! after they get the first question wrong.

EDIT: Never mind. It worked the second time, but not the first time. I apologize.
Last edited on
No Problem.Its good that it worked.
Thank you. Sorry for taking the code.
Topic archived. No new replies allowed.