IF statment not working!! SOLUTIONS?

I am creating code for a console game and one of my if functions isn't working.
If I put in 2 separate numbers then it will, but I don't want that. The code is below and the problematic If Statment is on line 129.


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
if(a==1){
			string name;
	int cheat;
	string chi;
	for(string z="yes";z=="yes";){
	cout<<"Do You Know the Cheat Code?                                               V 0.3\n";
	cin>>chi;
	if(chi=="yes"){
	   cout<<"What is the Cheat Code?\n";
	   cout<<"Code:";
	   cin>>cheat;
		  if(cheat==7252){
	cout<<"Welcome to DICE!\n";
	cout<<"This is a Game of Chance!\n";
	cout<<"You Start With 100 Creidts!\n";
	cout<<"Choose a Number 1-6!\n";
	cout<<"If You Guess Correctly You Gain 40 Credits!\n";
	cout<<"If You Guess Incorrectly You Lose 10 Credits!\n";
	cout<<"If You Achevie 1000 Credits You WIN!\n";
	cout<<"The Cheat Code Activates at 200 Credits!\n";
	cout<<"Please Enter Your Name!\n";
	cout<<"Name:";
	cin>>name; 
	for(int cred=100;cred<999&&cred>0;){
		int creds=cred;
	cout<<"\n";
	srand(time(0));
	int b=1+(rand()%6);
	int a;
	cout<<name;
	cout<<" Chooses ";
	cin>>a;
if(a==5272&&cred>199){
	cout<<"Cheat Code Accepted!\n";
	cout<<"50 Credits Added!\n";
	cred=cred+50;
}
else{
if(a--==b){
		cout<<"The Answer is: ";
		cout<<b;
		cout<<"\n";
		cout<<"You Win!\n";
		 cred=cred+40;
	}
	else{
		cout<<"The Correct Answer is:";
		cout<<b;
		cout<<"\n";
		cout<<"You Lost!\n";
		cout<<"Try Again!\n";
		 cred=cred-10;
	}
}
	cout<<"You Have ";
	cout<<cred;
	cout<<" Credits\n";
}
if(cred>299)
cout<<"The Cheat Code is 5272!\n";
if(cred>999){
	cout<<"Congratulations!\n";
	cout<<"You WON!\n";
	cout<<"Play Again?";
	cin>>z;
}
if(cred<1){
cout<<"Sorry You Ran Out of Credits!\n";
cout<<"Play Again?\n";
}
}
cin>>z;
}
if(chi!="yes"){
cout<<"Welcome to DICE!\n";
	cout<<"This is a Game of Chance!\n";
	cout<<"You Start With 100 Creidts!\n";
	cout<<"Choose a Number 1-6!\n";
	cout<<"If You Guess Correctly You Gain 40 Credits!\n";
	cout<<"If You Guess Incorrectly You Lose 10 Credits!\n";
	cout<<"If You Achevie 1000 Credits You WIN!\n";
	cout<<"There is No Cheat Code on This Mode!\n";
	cout<<"Please Enter Your Name!\n";
	cout<<"Name:";
	cin>>name;
	for(int cred=100;cred<999&&cred>0;){
		int creds=cred;
	cout<<"\n";
	srand(time(0));
	int b=1+(rand()%6);
	int a;
	cout<<name;
	cout<<" Chooses ";
	cin>>a;
if(a--==b){
		cout<<"The Answer is: ";
		cout<<b;
		cout<<"\n";
		cout<<"You Win!\n";
		 cred=cred+40;
	}
	else{
		cout<<"The Correct Answer is:";
		cout<<b;
		cout<<"\n";
		cout<<"You Lost!\n";
		cout<<"Try Again!\n";
		 cred=cred-10;
	}
	cout<<"You Have ";
	cout<<cred;
	cout<<" Credits\n";
}
if(cred>299){
	cout<<"The Cheat Code is 5272!";
}
if(cred>999){
	cout<<"Congratulations!\n";
	cout<<"You WON!\n";
	cout<<"Play Again?";
	cin>>z;
}
if(cred<1){
cout<<"Sorry You Ran Out of Credits!\n";
cout<<"Play Again?\n";
}	
}	
cin>>z;
if(cheat!=7252){	
cout<<"Welcome to DICE!\n";
	cout<<"This is a Game of Chance!\n";
	cout<<"You Start With 100 Creidts!\n";
	cout<<"Choose a Number 1-6!\n";
	cout<<"If You Guess Correctly You Gain 40 Credits!\n";
	cout<<"If You Guess Incorrectly You Lose 10 Credits!\n";
	cout<<"If You Achevie 1000 Credits You WIN!\n";
	cout<<"There is No Cheat Code in This Mode!\n";
	cout<<"Please Enter Your Name!\n";
	cout<<"Name:";
	cin>>name;
	for(int cred=100;cred<999&&cred>0;){
		int creds=cred;
	cout<<"\n";
	srand(time(0));
	int b=1+(rand()%6);
	int a;
	cout<<name;
	cout<<" Chooses ";
	cin>>a;
if(a--==b){
		cout<<"The Answer is: ";
		cout<<b;
		cout<<"\n";
		cout<<"You Win!\n";
		 cred=cred+40;
	}
	else{
		cout<<"The Correct Answer is:";
		cout<<b;
		cout<<"\n";
		cout<<"You Lost!\n";
		cout<<"Try Again!\n";
		 cred=cred-10;
	}
	cout<<"You Have ";
	cout<<cred;
	cout<<" Credits\n";
}
if(cred>299){
	cout<<"The Cheat Code is 5272!\n";
}
if(cred>999){
	cout<<"Congratulations!\n";
	cout<<"You WON!\n";
	cout<<"Play Again?";
	cin>>z;
}
if(cred<1){
cout<<"Sorry You Ran Out of Credits!\n";
cout<<"Play Again?\n";

}
cin>>z;
}
if(z!="yes"){
	cout<<"Good Bye!\n";
}
}
}
Last edited on
Please explain what you mean by not working. Also, format your code; it's an eyesore as it is.

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
if (a == 1) {
  string name;
  int cheat;
  string chi;
  for (string z = "yes"; z == "yes";) {
    cout << "Do You Know the Cheat Code?                                               V 0.3\n";
    cin >> chi;
    if (chi == "yes") {
      cout << "What is the Cheat Code?\n";
      cout << "Code:";
      cin >> cheat;
      if (cheat == 7252) {
        cout << "Welcome to DICE!\n";
        cout << "This is a Game of Chance!\n";
        cout << "You Start With 100 Creidts!\n";
        cout << "Choose a Number 1-6!\n";
        cout << "If You Guess Correctly You Gain 40 Credits!\n";
        cout << "If You Guess Incorrectly You Lose 10 Credits!\n";
        cout << "If You Achevie 1000 Credits You WIN!\n";
        cout << "The Cheat Code Activates at 200 Credits!\n";
        cout << "Please Enter Your Name!\n";
        cout << "Name:";
        cin >> name;
        for (int cred = 100; cred < 999 && cred > 0;) {
          int creds = cred;
          cout << "\n";
          srand(time(0));
          int b = 1 + (rand() % 6);
          int a;
          cout << name;
          cout << " Chooses ";
          cin >> a;
          if (a == 5272 && cred > 199) {
            cout << "Cheat Code Accepted!\n";
            cout << "50 Credits Added!\n";
            cred = cred + 50;
          } else {
            if (a-- == b) {
              cout << "The Answer is: ";
              cout << b;
              cout << "\n";
              cout << "You Win!\n";
              cred = cred + 40;
            } else {
              cout << "The Correct Answer is:";
              cout << b;
              cout << "\n";
              cout << "You Lost!\n";
              cout << "Try Again!\n";
              cred = cred - 10;
            }
          }
          cout << "You Have ";
          cout << cred;
          cout << " Credits\n";
        }
        if (cred > 299)
          cout << "The Cheat Code is 5272!\n";
        if (cred > 999) {
          cout << "Congratulations!\n";
          cout << "You WON!\n";
          cout << "Play Again?";
          cin >> z;
        }
        if (cred < 1) {
          cout << "Sorry You Ran Out of Credits!\n";
          cout << "Play Again?\n";
        }
      }
      cin >> z;
    }
    if (chi != "yes") {
      cout << "Welcome to DICE!\n";
      cout << "This is a Game of Chance!\n";
      cout << "You Start With 100 Creidts!\n";
      cout << "Choose a Number 1-6!\n";
      cout << "If You Guess Correctly You Gain 40 Credits!\n";
      cout << "If You Guess Incorrectly You Lose 10 Credits!\n";
      cout << "If You Achevie 1000 Credits You WIN!\n";
      cout << "There is No Cheat Code on This Mode!\n";
      cout << "Please Enter Your Name!\n";
      cout << "Name:";
      cin >> name;
      for (int cred = 100; cred < 999 && cred > 0;) {
        int creds = cred;
        cout << "\n";
        srand(time(0));
        int b = 1 + (rand() % 6);
        int a;
        cout << name;
        cout << " Chooses ";
        cin >> a;
        if (a-- == b) {
          cout << "The Answer is: ";
          cout << b;
          cout << "\n";
          cout << "You Win!\n";
          cred = cred + 40;
        } else {
          cout << "The Correct Answer is:";
          cout << b;
          cout << "\n";
          cout << "You Lost!\n";
          cout << "Try Again!\n";
          cred = cred - 10;
        }
        cout << "You Have ";
        cout << cred;
        cout << " Credits\n";
      }
      if (cred > 299) {
        cout << "The Cheat Code is 5272!";
      }
      if (cred > 999) {
        cout << "Congratulations!\n";
        cout << "You WON!\n";
        cout << "Play Again?";
        cin >> z;
      }
      if (cred < 1) {
        cout << "Sorry You Ran Out of Credits!\n";
        cout << "Play Again?\n";
      }
    }
    cin >> z;
    if (cheat != 7252) {
      cout << "Welcome to DICE!\n";
      cout << "This is a Game of Chance!\n";
      cout << "You Start With 100 Creidts!\n";
      cout << "Choose a Number 1-6!\n";
      cout << "If You Guess Correctly You Gain 40 Credits!\n";
      cout << "If You Guess Incorrectly You Lose 10 Credits!\n";
      cout << "If You Achevie 1000 Credits You WIN!\n";
      cout << "There is No Cheat Code in This Mode!\n";
      cout << "Please Enter Your Name!\n";
      cout << "Name:";
      cin >> name;
      for (int cred = 100; cred < 999 && cred > 0;) {
        int creds = cred;
        cout << "\n";
        srand(time(0));
        int b = 1 + (rand() % 6);
        int a;
        cout << name;
        cout << " Chooses ";
        cin >> a;
        if (a-- == b) {
          cout << "The Answer is: ";
          cout << b;
          cout << "\n";
          cout << "You Win!\n";
          cred = cred + 40;
        } else {
          cout << "The Correct Answer is:";
          cout << b;
          cout << "\n";
          cout << "You Lost!\n";
          cout << "Try Again!\n";
          cred = cred - 10;
        }
        cout << "You Have ";
        cout << cred;
        cout << " Credits\n";
      }
      if (cred > 299) {
        cout << "The Cheat Code is 5272!\n";
      }
      if (cred > 999) {
        cout << "Congratulations!\n";
        cout << "You WON!\n";
        cout << "Play Again?";
        cin >> z;
      }
      if (cred < 1) {
        cout << "Sorry You Ran Out of Credits!\n";
        cout << "Play Again?\n";

      }
      cin >> z;
    }
    if (z != "yes") {
      cout << "Good Bye!\n";
    }
  }
}
When the variable cheat doesn't equal 7252 it doesn't do anything in the console. The if statement says if cheat doesn't equal 7252 to run.
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
cout << "Welcome to DICE!\n";
      cout << "This is a Game of Chance!\n";
      cout << "You Start With 100 Creidts!\n";
      cout << "Choose a Number 1-6!\n";
      cout << "If You Guess Correctly You Gain 40 Credits!\n";
      cout << "If You Guess Incorrectly You Lose 10 Credits!\n";
      cout << "If You Achevie 1000 Credits You WIN!\n";
      cout << "There is No Cheat Code in This Mode!\n";
      cout << "Please Enter Your Name!\n";
      cout << "Name:";
      cin >> name;
      for (int cred = 100; cred < 999 && cred > 0;) {
        int creds = cred;
        cout << "\n";
        srand(time(0));
        int b = 1 + (rand() % 6);
        int a;
        cout << name;
        cout << " Chooses ";
        cin >> a;
        if (a-- == b) {
          cout << "The Answer is: ";
          cout << b;
          cout << "\n";
          cout << "You Win!\n";
          cred = cred + 40;
        } else {
          cout << "The Correct Answer is:";
          cout << b;
          cout << "\n";
          cout << "You Lost!\n";
          cout << "Try Again!\n";
          cred = cred - 10;
        }
        cout << "You Have ";
        cout << cred;
        cout << " Credits\n";
      }
      if (cred > 299) {
        cout << "The Cheat Code is 5272!\n";
      }
      if (cred > 999) {
        cout << "Congratulations!\n";
        cout << "You WON!\n";
        cout << "Play Again?";
        cin >> z;
      }
      if (cred < 1) {
        cout << "Sorry You Ran Out of Credits!\n";
        cout << "Play Again?\n";

      }
      cin >> z;
    }

Last edited on
if (cheat != 7252) {

cout << "The Cheat Code is 5272!\n";

You sure you're not getting confused somewhere?
Last edited on
I am surprised that you don't use the "else"
1
2
3
4
5
6
7
8
9
10
int theCorrectCheatCode = 1234; // by using a variable you can prevent some confusion 

if (condition) // for example (cheat == theCorrectCheatCode)
{
      // do great stuff
}
else
{
      // do other stuff like cout << "wrong cheat code, you entered " << cheat << " it should be " << theCorrectCheatCode << ".\n";
}


That way you can let your program tell you why it is not doing what you expected.
Of course this only works if you can figure out which brackets belong to what, but that should be easy if you format your code.

Kind regards, Nico
Topic archived. No new replies allowed.