Jump to content
  • Advertisement
Sign in to follow this  
DrDev

Logical Problem in If statement

This topic is 3734 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey people, this is my first post here and I hope its in the right forum. Well, for a while now I have been telling myself "I want to be a game developer, I need to do it" so I started learning C(plus plus). So far so good, only a couple problems. I was hoping since this is the beginners forum that some body could explain this problem to me, it's to do with if statements and the unary symbol things (ie && || == <= etc) My question is, why wont a statement such as
if((i==2 || 12) && (n==1))
work for me? can anybody help? UPDATE: Just wrote a really simple piece of code;
#include <iostream>

using namespace std;

int main()
{
    int i = 0;
    int n = 0;

    cin >> i;
    cin >> n;

    if((i == 5 || 6) && (n==2))
     {
         cout << "WORKS!";
         system("PAUSE");
     }
      else
      cout << "DOSENT WORK =(";
      system("PAUSE");
    return 0;
}
and it worked, is it just me or is it that the more complicated the code the less these multi-operator if statements work? [Edited by - DrDev on June 30, 2008 10:29:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
First of all, those aren't "unary symbols". There are many adjectives that apply to those symbols-- "binary", meaning they take left and right arguments, or "boolean", meaning they return true or false, but "unary" means "only takes one argument", which they do not.

Now. I know what you're thinking with if(i==2 || 12). You're parsing it as the English sentence fragment "if i equals two or twelve". But that's wrong. || should be translated as "If either of these boolean conditions are true", making the actual condition "if the condition 'i equals two' is true or the condition 'twelve' is true". And the condition "twelve" is always true, because nonzero integers convert to the boolean value "true". What you really want is if(i==2 || i==12).

Share this post


Link to post
Share on other sites
Quote:
Original post by DrDev
it's to do with if statements and the unary symbol things (ie && || == <= etc)


The phrase you're looking for is "logical operator", not "unary operator".

A unary operator is one that takes one operand eg. !a. The exclamation mark symbol is the unary operator, the single operand it takes is 'a'.

Those logical operators you're referring to are also binary operators because they accept two operands eg. a || b.

Quote:
Original post by DrDev
My question is, why wont a statement such as
if((i==2 || 12) && (n==1))
work for me? can anybody help?


The problem is that 12 always evaluates to 'true'. You want to write this instead:

if((i==2 || i == 12) && (n==1))

Share this post


Link to post
Share on other sites
Thanks for the quick reply's! I kinda feel like an idiot with the whole calling them Unary operators, I just started learning two days ago and knew it wasn't right to call them that, but I couldn't remember what to call them.

Thanks for clearing up my perception on logical operators and how you shouldn't read them like an English sentence =).

The reason I asked this question is because I started writing a craps text based game to test my knowledge on the language, here it is 0.o don't bother reading it because its uber messy, just feel like showing what I know so far.


#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;

//Function Prototypes
int drawMenu(void);
int drawGame(int _funds,bool gameState);
int takeBet(int onRoll, int cashAmmount);
int checkRoll(int betOnNum, float amtBet);
void drawRules(void);

//Global variables
const int indexNum = 80;
bool gameRunning = true;
string msg = " ";



int main()
{
short cashFunds = 1000;


while(gameRunning)
{
drawMenu();
}

return 0;
}

int drawMenu(void)
{
int _select = 4;
int _funds = 1000;

system("CLS"); //Clear screen
cout << " Craps v1.0" << endl;
cout << "\n 1. - Rules" << endl;
cout << " 2. - Play " << endl;
cout << " 3. - Exit " << endl;
cout << "\n Selection: ";

cin >> _select;

switch(_select)
{
case 1: drawRules();
break;
case 2: drawGame(_funds,&gameRunning);
break;
case 3: gameRunning = false;
break;
default: drawMenu();
}
}

void drawRules(void)
{
char resume[80];

system("CLS"); //Clear screen
cout << " -Rules- " << endl;
cout << " You have $1000 to start gambling away." << endl;
cout << "\n You have 3 different possible bets:" << endl;
cout << " You can bet on numbers 2 and 12, they " << endl;
cout << " will give you a 5 to 1 return if you win." << endl;
cout << "\n You can bet on numbers 4 and 10, they" << endl;
cout << " will give you a 2.5 to 1 return if you win." << endl;
cout << "\n The last kind of bet you can do is on the" << endl;
cout << " numbers 6 and 8, this gives you a win ratio of" << endl;
cout << " 1.5 to 1 if you win." << endl;

cout << "\n The minimum ammount you can bet at one time is $10" << endl;
cout << "\n The maximum ammount you can bet at one time is $150" << endl;
cout << "\n If you lose all of your money, you lose and have to walk home =\(" << endl;
cout << " If you win more then $6000 you are a winner and get chased out of the casino" << endl;
cout << "\n HAVE FUN!" << endl;
cout << "\n Enter \'r\' to return to main menu.";
cin >> resume;

if(resume[0]=='r')
{
drawMenu();
}
else
drawRules();
}


int drawGame(int _funds,bool gameState)
{
const int _indexValue = 2;

int _num = 0;
static int _amt = 0;
static float _winR = 1.0;
bool checkingAmt = true;
static int _sel[_indexValue];
int _selTrack = 0;


if(_funds > 0)
{
system("CLS");
cout << " Craps v1.0" << endl;
cout << " Remaining $"<<_funds<<endl;
cout << msg <<endl;
cout << "\n Dealer says:\"All bets please!\""<<endl;
cout << " Dealer says:\"You sir, which number do you want to bet on?\" "<<endl;
cout << " \n I will bet on..."<<endl;
cout << " 1. numbers 2 & 12"<<endl;
cout << " 2. numbers 4 & 10"<<endl;
cout << " 3. numbers 6 & 8 "<<endl;
cout << "\n[SELECT] ";
cin >> _num;
//Rule check - Is the number they want allowed?


switch(_num)
{
case 1: _sel[0] = 2;
_sel[1] = 12;
_selTrack = 1;
break;

case 2: _sel[0] = 4;
_sel[1] = 10;
_selTrack = 2;
break;

case 3: _sel[0] = 6;
_sel[1] = 8;
_selTrack = 3;
break;

default: msg = "\n !!!THAT NUMBER IS INVALID, PICK AGAIN!!! ";
system("CLS");
drawGame(_funds,gameState);
break;
}

cout << "\n Dealer Says: Good choice,numbers "<<_sel[0]<<" & "<<_sel[1]<<" it is."<<endl;

// Start bet ammount check loop to make sure the bet is within range.
while(checkingAmt = true)
{
cout << " And how much would you like to bet?"<<endl;
cout <<"\n I will bet $";
cin >> _amt;

//Begin first part of check, to see if _amt is less then $10 or more then $150.
if( (_amt < 10) || (_amt >= 151) )
{
//Display an error message.
cout << "\n !!! INVALID AMMOUNT, MINIMUM $10 MAXIMUM $150 !!!\n"<<endl;
}
/*If the above checks out, then double check it, if _amt is greater then 10 and less then 150
then break out of the loop and move on */
else if ( (_amt > 10) && (_amt <= 150) )
{
break;
}

checkingAmt = false;
}


checkRoll(_selTrack,_amt);

}


}

int checkRoll(int betOnNum, float amtBet)
{
/*Seed from time*/
srand (time (NULL));
//Woot, the final stretch! Lets roll them dice!!!
int diceRoll = rand() % 10 + 3;

if((diceRoll == 2 || diceRoll == 12)&&(betOnNum==1))
{
cout << "Roll: "<<diceRoll<<" IF 1.";
system("PAUSE");
}
else if((diceRoll == 4 || diceRoll == 10)&&(betOnNum==2))
{
cout << "Roll: "<<diceRoll<<" IF 2.";
system("PAUSE");
}
else if((diceRoll == 6 || diceRoll == 8)&&(betOnNum==3))
{
cout << "Roll: "<<diceRoll<<" IF 3.";
system("PAUSE");
}
else
cout << "LOSER";
system("PAUSE");



}

Share this post


Link to post
Share on other sites
Use [ source][ /source] tags (except without the spaces after the [.

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;

//Function Prototypes
int drawMenu(void);
int drawGame(int _funds,bool gameState);
int takeBet(int onRoll, int cashAmmount);
int checkRoll(int betOnNum, float amtBet);
void drawRules(void);

//Global variables
const int indexNum = 80;
bool gameRunning = true;
string msg = " ";



int main()
{
short cashFunds = 1000;


while(gameRunning)
{
drawMenu();
}

return 0;
}

int drawMenu(void)
{
int _select = 4;
int _funds = 1000;

system("CLS"); //Clear screen
cout << " Craps v1.0" << endl;
cout << "\n 1. - Rules" << endl;
cout << " 2. - Play " << endl;
cout << " 3. - Exit " << endl;
cout << "\n Selection: ";

cin >> _select;

switch(_select)
{
case 1: drawRules();
break;
case 2: drawGame(_funds,&gameRunning);
break;
case 3: gameRunning = false;
break;
default: drawMenu();
}
}

void drawRules(void)
{
char resume[80];

system("CLS"); //Clear screen
cout << " -Rules- " << endl;
cout << " You have $1000 to start gambling away." << endl;
cout << "\n You have 3 different possible bets:" << endl;
cout << " You can bet on numbers 2 and 12, they " << endl;
cout << " will give you a 5 to 1 return if you win." << endl;
cout << "\n You can bet on numbers 4 and 10, they" << endl;
cout << " will give you a 2.5 to 1 return if you win." << endl;
cout << "\n The last kind of bet you can do is on the" << endl;
cout << " numbers 6 and 8, this gives you a win ratio of" << endl;
cout << " 1.5 to 1 if you win." << endl;

cout << "\n The minimum ammount you can bet at one time is $10" << endl;
cout << "\n The maximum ammount you can bet at one time is $150" << endl;
cout << "\n If you lose all of your money, you lose and have to walk home =\(" << endl;
cout << " If you win more then $6000 you are a winner and get chased out of the casino" << endl;
cout << "\n HAVE FUN!" << endl;
cout << "\n Enter \'r\' to return to main menu.";
cin >> resume;

if(resume[0]=='r')
{
drawMenu();
}
else
drawRules();
}


int drawGame(int _funds,bool gameState)
{
const int _indexValue = 2;

int _num = 0;
static int _amt = 0;
static float _winR = 1.0;
bool checkingAmt = true;
static int _sel[_indexValue];
int _selTrack = 0;


if(_funds > 0)
{
system("CLS");
cout << " Craps v1.0" << endl;
cout << " Remaining $"<<_funds<<endl;
cout << msg <<endl;
cout << "\n Dealer says:\"All bets please!\""<<endl;
cout << " Dealer says:\"You sir, which number do you want to bet on?\" "<<endl;
cout << " \n I will bet on..."<<endl;
cout << " 1. numbers 2 & 12"<<endl;
cout << " 2. numbers 4 & 10"<<endl;
cout << " 3. numbers 6 & 8 "<<endl;
cout << "\n[SELECT] ";
cin >> _num;
//Rule check - Is the number they want allowed?


switch(_num)
{
case 1: _sel[0] = 2;
_sel[1] = 12;
_selTrack = 1;
break;

case 2: _sel[0] = 4;
_sel[1] = 10;
_selTrack = 2;
break;

case 3: _sel[0] = 6;
_sel[1] = 8;
_selTrack = 3;
break;

default: msg = "\n !!!THAT NUMBER IS INVALID, PICK AGAIN!!! ";
system("CLS");
drawGame(_funds,gameState);
break;
}

cout << "\n Dealer Says: Good choice,numbers "<<_sel[0]<<" & "<<_sel[1]<<" it is."<<endl;

// Start bet ammount check loop to make sure the bet is within range.
while(checkingAmt = true)
{
cout << " And how much would you like to bet?"<<endl;
cout <<"\n I will bet $";
cin >> _amt;

//Begin first part of check, to see if _amt is less then $10 or more then $150.
if( (_amt < 10) || (_amt >= 151) )
{
//Display an error message.
cout << "\n !!! INVALID AMMOUNT, MINIMUM $10 MAXIMUM $150 !!!\n"<<endl;
}
/*If the above checks out, then double check it, if _amt is greater then 10 and less then 150
then break out of the loop and move on */

else if ( (_amt > 10) && (_amt <= 150) )
{
break;
}

checkingAmt = false;
}


checkRoll(_selTrack,_amt);

}


}

int checkRoll(int betOnNum, float amtBet)
{
/*Seed from time*/
srand (time (NULL));
//Woot, the final stretch! Lets roll them dice!!!
int diceRoll = rand() % 10 + 3;

if((diceRoll == 2 || diceRoll == 12)&&(betOnNum==1))
{
cout << "Roll: "<<diceRoll<<" IF 1.";
system("PAUSE");
}
else if((diceRoll == 4 || diceRoll == 10)&&(betOnNum==2))
{
cout << "Roll: "<<diceRoll<<" IF 2.";
system("PAUSE");
}
else if((diceRoll == 6 || diceRoll == 8)&&(betOnNum==3))
{
cout << "Roll: "<<diceRoll<<" IF 3.";
system("PAUSE");
}
else
cout << "LOSER";
system("PAUSE");



}

Share this post


Link to post
Share on other sites
if((i==2 || 12) && (n==1))
12 in this context evaluates to true.

if((i==2 || true) && (n==1))
X || true is always true

if((true) && (n==1))
true && X is always X

if(n==1)
is what your actual statement is.


What you want:
if ( ((i==2) || (i==12)) && (n==1) )

Share this post


Link to post
Share on other sites
Your code looks surprisingly good for two days of work. There's some minor issues with breaking up functions, but nothing that won't be obvious with a little more work. Good job..keep at it!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!