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.
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";
}
}
}
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.