if statment help!

At the Mob detail part, when I press 1, it goes to the 0 part first. Please tell me how to make it go to 1 when I press 1. Thank you.

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
  #include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

float menuoption;
float minecraftoption;
int mobdetail;
int mobdetailzombie;
int mobdetailzombiepigman;

int main()

{
    start:

    cout << string(300, '\n');
    cout<<("Welcome to my c++ file! Please select one of the following options.\n1-Minecraft") << endl << endl;
    cout<<("\nCHOICE: ") << endl << endl;
    cin>> menuoption;
    if(menuoption==1)
    {
        /*MINECRAFT*/
        cout << string(300, '\n');
        cout<<("Thank you for choosing the minecraft option.\nPlease select what you want to do") << endl << endl;
        cout<<("\n1-Minecraft Item ID's, 2-Mob details") << endl << endl;
        cout<<("\nCHOICE: ") << endl << endl;
        cin>> minecraftoption;

        if(minecraftoption==1)
        {
            /*MINECRAFT ID'S*/
            cout << string(300, '\n');


        }
        else if(minecraftoption==2)
        {
            /*MOB DETAILS*/
            cout << string(300, '\n');
            cout<<("0-zombie, 1-zombie pigman, 2-skeleton, 3-spider, 4-blaze, 5-ghast, 6-wither");
            cout<<("\nPlease state the mob you want to look up: ");
            cin>> mobdetail;
            if(mobdetail==0);
            {
                /*zombie*/
                cout << string(300, '\n');
                cout<<("\n\nName - Zombie\n");
                cout<<("Found - Cave systems and at night\n");
                cout<<("Drops - Rotton Flesh, Iron Ignot, carrot, potato.\n");
                cout<<("Rare drops - Iron shovel, Iron sword, random aromor.\n");
                cout<<("Experiance - 5\n");
                cout<<("Attack strength(hp16-20) - easy - 2, normal - 3, hard - 4.\n");
                cout<<("Attack strength(hp11-15) - easy - 3, normal - 4, hard - 6.\n");
                cout<<("Attack strength(hp6-10) - easy - 3, normal - 5, hard - 7.\n");
                cout<<("Attack strength(hp1-5) - easy - 4, normal - 6, hard - 9.\n");
                cout<<("Health - 20.\n");
                cout<<("Do you want to continue? 1 - yes, 0 - no: ");
                cin>> mobdetailzombie;
                if(mobdetailzombie==0)
                {
                    /*LEAVE EMPTY*/
                }
                if(mobdetailzombie==1)
                {
                   cout << string(300, '\n');
                   goto start;
                }
            }
            if(mobdetail==1);
            {
                /*zombie pigman*/
                cout << string(300, '\n');
                cout<<("\n\nName - Zombie Pigman\n");
                cout<<("Found - Dimely lit areas in the Nether, and when a pig is struck by lightening.\n");
                cout<<("Drops - Rotton flesh, gold nugget.\n");
                cout<<("Rare drops - Golden Sword, Random Armor.\n");
                cout<<("Experiance - 5.\n");
                cout<<("Attack strength - easy - 5, normal - 9, hard - 13.\n");
                cout<<("Health - 20");
                cout<<("Need to know - If attacked, other zombie pigmen will attack you.\n");
                cout<<("Do you want to continue? 1 - yes, 0 - no: ");
                cin>> mobdetailzombiepigman;
                if(mobdetailzombiepigman==0)
                {
                    /*LEAVE EMPTY*/
                }
                else if(mobdetailzombiepigman==1)
                {
                    cout << string(300, '\n');
                    goto start;
                }
            }
            if(mobdetail==2);
            {
                /*skeleton*/
                cout << string(300, '\n');
            }
            if(mobdetail==3);
            {
                /*spider*/
                cout << string(300, '\n');
            }
            if(mobdetail==4);
            {
                /*blaze*/
                cout << string(300, '\n');
            }
            if(mobdetail==5);
            {
                /*ghast*/
                cout << string(300, '\n');
            }
            if(mobdetail==6);
            {
                /*wither*/
                cout << string(300, '\n');
            }
        }
    }
    if(menuoption!=1)
    {
        cout << string(300, '\n');
        cout<<("I'm sorry, the option you entered is not reconised. Application will now close");}


}
Well, don't hold me on it as I'm fairly new to c++ myself, but I think that if statements don't work that way. You should try while loops.
Last edited on
I'd recommend type int rather than float for the user input.

But the main problem is the unnecessary semicolon:
1
2
3
4
if (mobdetail==0);
{
    // detailed code here            
}


The "if" is actually controlling an empty statement, ending at the semicolon. The following code in braces is always executed as it is not dependent on any condition.

Should be like this:
1
2
3
4
if (mobdetail==0)
{
    // detailed code here             
}


1. Avoid the use of goto. It can cause problems.

2. Study if else. It is better than two if clauses when the first being true already means that the other tests will fail.

3. Avoid global variables.

4. The mentioned problem migth relate to how "clean" the stream is before the read. I'm no expert on that, but I've seen the explanation in several threads.
1. Wouldn't typechar be better for user input as all the inputs are relatively small numbers ?

2. Also couldn't you use a switch statement instead of lots of if statements ?

3. Would it not be possible to make this a function and then call it each time goto start is used instead.
Last edited on
@jidder
1) Not necessarily. Comparisons would have to be changed to character literals ('1'). Any possible savings in memory would be minimal or non-existant due to padding for alignment.

2) Yes. Generally preferred.

3) Yes. Cleaner approach.
However, the main advantage of the char type for user input is that it doesn't fail if the user enters a non-numeric character. So it can keep the code simpler as there is less need for error-handling.
Topic archived. No new replies allowed.