Public Group

# Logical Problem in If statement

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

## 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 on other sites
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 on other sites
Quote:
 Original post by DrDevit'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 DrDevMy 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 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 Prototypesint  drawMenu(void);int  drawGame(int _funds,bool gameState);int  takeBet(int onRoll, int cashAmmount);int  checkRoll(int betOnNum, float amtBet);void drawRules(void);//Global variablesconst  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 screencout << "                                     -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 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 Prototypesint  drawMenu(void);int  drawGame(int _funds,bool gameState);int  takeBet(int onRoll, int cashAmmount);int  checkRoll(int betOnNum, float amtBet);void drawRules(void);//Global variablesconst  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 screencout << "                                     -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 on other sites
oh its
 i was using  =( ty
 
 0 
 Share this post Link to post Share on other sites 
 
 
 Antheus    2409 Antheus Member 2409 Posted July 1, 2008 if((i==2 || 12) && (n==1))12 in this context evaluates to true.if((i==2 || true) && (n==1))X || true is always trueif((true) && (n==1))true && X is always Xif(n==1)is what your actual statement is.What you want:if ( ((i==2) || (i==12)) && (n==1) ) 0 Share this post Link to post Share on other sites Sneftel    1788 Sneftel GDNet Emeritus 1788 Posted July 1, 2008 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! 0 Share this post Link to post Share on other sites 
 Sign in to follow this   Followers 0 
 Go To Topic Listing Forum 
 Advertisement 
 Advertisement Popular Tags 2D 3D Advice Algorithm Animation C# C++ Character Concept Design DX11 Education GameMaker Gameplay General Java Learning Marketing Mobile Music OpenGL PC Unity Unreal VR Popular Contributors Week Month Year All Time 1 Rutin 70 2 CyberFlash 36 3 Septopus 31 4 lawnjelly 17 5 supermikhail 16 Show More Advertisement Popular Now 21 Optimization How to Click and Unclick an object based on where the mouse is.. in GML By CyberFlashStarted 19 hours ago 10 number of matching chars in 2 strings By ICanCStarted Sunday at 11:20 AM 33 collision sprite By phil67rpgStarted Friday at 10:13 PM 20 Pub/Sub for game Servers... Are you? By SeptopusStarted Thursday at 06:38 PM 9 C++ Dynamic memory using "new" and malloc() By rytStarted Thursday at 01:59 PM Forum Statistics Total Topics 633422 Total Posts 3011800 Who's Online (See full list) There are no registered users currently online GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Forum This Topic More options... Find results that contain... All of my search term words Any of my search term words Find results in... Content titles and body Content titles only Home Groups For Beginners Forum Logical Problem in If statement 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Back Beginners Group Beginners Forum Beginners Resources Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community GameDev Challenges Affiliates Topical Workshops Gallery Groups Back For Beginners GameDev Challenges All Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Forums Back All Forums For Beginners Audio Back Music and Sound FX Games Career Development Business Back Games Career Development Production and Management Games Business and Law Game Design Back Game Design and Theory Writing for Games Programming Back Artificial Intelligence Engines and Middleware General and Gameplay Programming Graphics and GPU Programming Math and Physics Networking and Multiplayer Visual Arts Back 2D and 3D Art Critique and Feedback Community Back GameDev Challenges GDNet Lounge GDNet Comments, Suggestions, and Ideas Coding Horrors Your Announcements Hobby Project Classifieds Indie Showcase Affiliates Back NeHe Productions AngelCode Topical Workshops Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Subscriptions Chat Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search var ipsDebug = false; var CKEDITOR_BASEPATH = '//www.gamedev.net/applications/core/interface/ckeditor/ckeditor/'; var ipsSettings = { cookie_path: "/", cookie_prefix: "ips4_", cookie_ssl: true, upload_imgURL: "", message_imgURL: "", notification_imgURL: "", baseURL: "//www.gamedev.net/", jsURL: "//www.gamedev.net/applications/core/interface/js/js.php", csrfKey: "e710ae789510481445872ef2c8beb3a4", antiCache: "14de000c45", disableNotificationSounds: false, useCompiledFiles: true, links_external: true, memberID: 0, analyticsProvider: "ga", viewProfiles: true, mapProvider: 'google', mapApiKey: "AIzaSyAeT7tk3vnWWmbgVISkLpbhkQvekG19rHM", }; ips.setSetting( 'date_format', jQuery.parseJSON('"mm\/dd\/yy"') ); ips.setSetting( 'date_first_day', jQuery.parseJSON('0') ); ips.setSetting( 'remote_image_proxy', jQuery.parseJSON('1') ); ips.setSetting( 'ipb_url_filter_option', jQuery.parseJSON('"none"') ); ips.setSetting( 'url_filter_any_action', jQuery.parseJSON('"allow"') ); ips.setSetting( 'bypass_profanity', jQuery.parseJSON('0') ); ips.setSetting( 'emoji_style', jQuery.parseJSON('"native"') ); ips.setSetting( 'emoji_shortcodes', jQuery.parseJSON('"1"') ); ips.setSetting( 'emoji_ascii', jQuery.parseJSON('"1"') ); ips.setSetting( 'emoji_cache', jQuery.parseJSON('"1"') ); ips.setSetting( 'quickSearchDefault', jQuery.parseJSON('"all"') ); ips.setSetting( 'quickSearchMinimum', jQuery.parseJSON('3') ); ips.setSetting( 'quickSearchShowAdv', jQuery.parseJSON('true') ); ips.setSetting( 'quickSearchIn', jQuery.parseJSON('"title"') ); { "@context": "http://schema.org", "@type": "DiscussionForumPosting", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/", "discussionUrl": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/", "name": "Logical Problem in If statement", "headline": "Logical Problem in If statement", "text": "Hey people, this is my first post here and I hope its in the right forum.\n\nWell, 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.\n\nI was hoping since this is the beginners forum that some body could explain this problem to me, it\u0027s to do with if statements and the unary symbol things (ie \u0026amp;\u0026amp; || == \u0026lt;= etc)\n\nMy question is, why wont a statement such as \n\nif((i==2 || 12) \u0026amp;\u0026amp; (n==1)) work for me? can anybody help?\n\nUPDATE:\n\nJust wrote a really simple piece of code;\n#include \u0026lt;iostream\u0026gt;\n\nusing namespace std;\n\nint main()\n{\n int i = 0;\n int n = 0;\n\n cin \u0026gt;\u0026gt; i;\n cin \u0026gt;\u0026gt; n;\n\n if((i == 5 || 6) \u0026amp;\u0026amp; (n==2))\n {\n cout \u0026lt;\u0026lt; \"WORKS!\";\n system(\"PAUSE\");\n }\n else\n cout \u0026lt;\u0026lt; \"DOSENT WORK =(\";\n system(\"PAUSE\");\n return 0;\n}\n\nand it worked, is it just me or is it that the more complicated the code the less these multi-operator if statements work?\n\n[Edited by - DrDev on June 30, 2008 10:29:27 PM]\n", "dateCreated": "2008-06-30T21:54:53+0000", "datePublished": "2008-06-30T21:54:53+0000", "pageStart": 1, "pageEnd": 1, "image": "https://secure.gravatar.com/avatar/8b413305848a0a0ce545071b889987b4?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.305d27e7540f4b4ff24ba0ae54817526.png", "author": { "@type": "Person", "name": "DrDev", "image": "https://secure.gravatar.com/avatar/8b413305848a0a0ce545071b889987b4?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.305d27e7540f4b4ff24ba0ae54817526.png", "url": "https://www.gamedev.net/profile/140955-drdev/" }, "interactionStatistic": [ { "@type": "InteractionCounter", "interactionType": "http://schema.org/ViewAction", "userInteractionCount": 595 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/CommentAction", "userInteractionCount": 7 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/FollowAction", "userInteractionCount": 50 } ], "comment": [ { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/?do=findComment\u0026comment=499720", "author": { "@type": "Person", "name": "DrDev", "image": "https://secure.gravatar.com/avatar/8b413305848a0a0ce545071b889987b4?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.305d27e7540f4b4ff24ba0ae54817526.png", "url": "https://www.gamedev.net/profile/140955-drdev/" }, "dateCreated": "2008-06-30T21:54:53+0000", "text": "Hey people, this is my first post here and I hope its in the right forum.\n\nWell, 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.\n\nI was hoping since this is the beginners forum that some body could explain this problem to me, it\u0027s to do with if statements and the unary symbol things (ie \u0026amp;\u0026amp; || == \u0026lt;= etc)\n\nMy question is, why wont a statement such as \n\nif((i==2 || 12) \u0026amp;\u0026amp; (n==1)) work for me? can anybody help?\n\nUPDATE:\n\nJust wrote a really simple piece of code;\n#include \u0026lt;iostream\u0026gt;\n\nusing namespace std;\n\nint main()\n{\n int i = 0;\n int n = 0;\n\n cin \u0026gt;\u0026gt; i;\n cin \u0026gt;\u0026gt; n;\n\n if((i == 5 || 6) \u0026amp;\u0026amp; (n==2))\n {\n cout \u0026lt;\u0026lt; \"WORKS!\";\n system(\"PAUSE\");\n }\n else\n cout \u0026lt;\u0026lt; \"DOSENT WORK =(\";\n system(\"PAUSE\");\n return 0;\n}\n\nand it worked, is it just me or is it that the more complicated the code the less these multi-operator if statements work?\n\n[Edited by - DrDev on June 30, 2008 10:29:27 PM]\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/?do=findComment\u0026comment=4259724", "author": { "@type": "Person", "name": "Sneftel", "image": "https://secure.gravatar.com/avatar/6937dd2a5931d74fcc793d691e0ad1e2?d=https://www.gamedev.net/uploads/monthly_2017_08/S.png.1f6293a62d3e8d357c99b9c4c3bf1427.png", "url": "https://www.gamedev.net/profile/18969-sneftel/" }, "dateCreated": "2008-06-30T22:00:22+0000", "text": "First of all, those aren\u0027t \"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\u0027re thinking with if(i==2 || 12). You\u0027re parsing it as the English sentence fragment \"if i equals two or twelve\". But that\u0027s wrong. || should be translated as \"If either of these boolean conditions are true\", making the actual condition \"if the condition \u0027i equals two\u0027 is true or the condition \u0027twelve\u0027 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).", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/?do=findComment\u0026comment=4259725", "author": { "@type": "Person", "name": "fpsgamer", "image": "https://secure.gravatar.com/avatar/ff7eb246b5be46fd696ccb2b4b78d9cd?d=https://www.gamedev.net/uploads/monthly_2017_08/F.png.cac7c649c0063977c07d2ac383173a10.png", "url": "https://www.gamedev.net/profile/102564-fpsgamer/" }, "dateCreated": "2008-06-30T22:02:39+0000", "text": "The phrase you\u0027re 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 \u0027a\u0027.Those logical operators you\u0027re referring to are also binary operators because they accept two operands eg. a || b.The problem is that 12 always evaluates to \u0027true\u0027. You want to write this instead:if((i==2 || i == 12) \u0026amp;\u0026amp; (n==1))\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/?do=findComment\u0026comment=4259729", "author": { "@type": "Person", "name": "DrDev", "image": "https://secure.gravatar.com/avatar/8b413305848a0a0ce545071b889987b4?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.305d27e7540f4b4ff24ba0ae54817526.png", "url": "https://www.gamedev.net/profile/140955-drdev/" }, "dateCreated": "2008-06-30T22:16:08+0000", "text": "Thanks for the quick reply\u0027s! I kinda feel like an idiot with the whole calling them Unary operators, I just started learning two days ago and knew it wasn\u0027t right to call them that, but I couldn\u0027t remember what to call them.Thanks for clearing up my perception on logical operators and how you shouldn\u0027t 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\u0027t bother reading it because its uber messy, just feel like showing what I know so far.\n#include \u0026lt;iostream\u0026gt;#include \u0026lt;stdlib.h\u0026gt;#include \u0026lt;time.h\u0026gt;#include \u0026lt;string.h\u0026gt;using namespace std;//Function Prototypesint drawMenu(void);int drawGame(int _funds,bool gameState);int takeBet(int onRoll, int cashAmmount);int checkRoll(int betOnNum, float amtBet);void drawRules(void);//Global variablesconst 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 \u0026lt;\u0026lt; \" Craps v1.0\" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \"\\n 1. - Rules\" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \" 2. - Play \" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \" 3. - Exit \" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \"\\n Selection: \"; cin \u0026gt;\u0026gt; _select; switch(_select) { case 1: drawRules(); break; case 2: drawGame(_funds,\u0026amp;gameRunning); break; case 3: gameRunning = false; break; default: drawMenu(); } }void drawRules(void){char resume[80];system(\"CLS\"); //Clear screencout \u0026lt;\u0026lt; \" -Rules- \" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" You have $1000 to start gambling away.\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n You have 3 different possible bets:\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" You can bet on numbers 2 and 12, they \" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" will give you a 5 to 1 return if you win.\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n You can bet on numbers 4 and 10, they\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" will give you a 2.5 to 1 return if you win.\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n The last kind of bet you can do is on the\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" numbers 6 and 8, this gives you a win ratio of\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" 1.5 to 1 if you win.\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n The minimum ammount you can bet at one time is$10\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n The maximum ammount you can bet at one time is $150\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n If you lose all of your money, you lose and have to walk home =\\(\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" If you win more then$6000 you are a winner and get chased out of the casino\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n HAVE FUN!\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n Enter \\\u0027r\\\u0027 to return to main menu.\";cin \u0026gt;\u0026gt; resume;if(resume[0]==\u0027r\u0027) { 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 \u0026gt; 0) { system(\"CLS\"); cout \u0026lt;\u0026lt; \" Craps v1.0\" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \" Remaining $\"\u0026lt;\u0026lt;_funds\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; msg \u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \"\\n Dealer says:\\\"All bets please!\\\"\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" Dealer says:\\\"You sir, which number do you want to bet on?\\\" \"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" \\n I will bet on...\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" 1. numbers 2 \u0026amp; 12\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" 2. numbers 4 \u0026amp; 10\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" 3. numbers 6 \u0026amp; 8 \"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \"\\n[SELECT] \"; cin \u0026gt;\u0026gt; _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 \u0026lt;\u0026lt; \"\\n Dealer Says: Good choice,numbers \"\u0026lt;\u0026lt;_sel[0]\u0026lt;\u0026lt;\" \u0026amp; \"\u0026lt;\u0026lt;_sel[1]\u0026lt;\u0026lt;\" it is.\"\u0026lt;\u0026lt;endl; // Start bet ammount check loop to make sure the bet is within range. while(checkingAmt = true) { cout \u0026lt;\u0026lt; \" And how much would you like to bet?\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt;\"\\n I will bet$\"; cin \u0026gt;\u0026gt; _amt; //Begin first part of check, to see if _amt is less then $10 or more then$150. if( (_amt \u0026lt; 10) || (_amt \u0026gt;= 151) ) { //Display an error message. cout \u0026lt;\u0026lt; \"\\n !!! INVALID AMMOUNT, MINIMUM $10 MAXIMUM$150 !!!\\n\"\u0026lt;\u0026lt;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 \u0026gt; 10) \u0026amp;\u0026amp; (_amt \u0026lt;= 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)\u0026amp;\u0026amp;(betOnNum==1)) { cout \u0026lt;\u0026lt; \"Roll: \"\u0026lt;\u0026lt;diceRoll\u0026lt;\u0026lt;\" IF 1.\"; system(\"PAUSE\"); } else if((diceRoll == 4 || diceRoll == 10)\u0026amp;\u0026amp;(betOnNum==2)) { cout \u0026lt;\u0026lt; \"Roll: \"\u0026lt;\u0026lt;diceRoll\u0026lt;\u0026lt;\" IF 2.\"; system(\"PAUSE\"); } else if((diceRoll == 6 || diceRoll == 8)\u0026amp;\u0026amp;(betOnNum==3)) { cout \u0026lt;\u0026lt; \"Roll: \"\u0026lt;\u0026lt;diceRoll\u0026lt;\u0026lt;\" IF 3.\"; system(\"PAUSE\"); } else cout \u0026lt;\u0026lt; \"LOSER\"; system(\"PAUSE\"); }\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/?do=findComment\u0026comment=4259750", "author": { "@type": "Person", "name": "thre3dee", "image": "https://secure.gravatar.com/avatar/6e76d05e958cddb78c86ca934d6fcdea?d=https://www.gamedev.net/uploads/monthly_2017_08/T.png.451445ec94d542094ea2a7aa0b1f2905.png", "url": "https://www.gamedev.net/profile/106351-thre3dee/" }, "dateCreated": "2008-06-30T23:14:34+0000", "text": "Use [ source][ /source] tags (except without the spaces after the [.\n#include \u0026lt;iostream\u0026gt;#include \u0026lt;stdlib.h\u0026gt;#include \u0026lt;time.h\u0026gt;#include \u0026lt;string.h\u0026gt;using namespace std;//Function Prototypesint drawMenu(void);int drawGame(int _funds,bool gameState);int takeBet(int onRoll, int cashAmmount);int checkRoll(int betOnNum, float amtBet);void drawRules(void);//Global variablesconst 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 \u0026lt;\u0026lt; \" Craps v1.0\" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \"\\n 1. - Rules\" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \" 2. - Play \" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \" 3. - Exit \" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \"\\n Selection: \"; cin \u0026gt;\u0026gt; _select;switch(_select) {case 1: drawRules();break;case 2: drawGame(_funds,\u0026amp;gameRunning);break;case 3: gameRunning = false;break;default: drawMenu(); } }void drawRules(void){char resume[80];system(\"CLS\"); //Clear screencout \u0026lt;\u0026lt; \" -Rules- \" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" You have $1000 to start gambling away.\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n You have 3 different possible bets:\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" You can bet on numbers 2 and 12, they \" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" will give you a 5 to 1 return if you win.\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n You can bet on numbers 4 and 10, they\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" will give you a 2.5 to 1 return if you win.\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n The last kind of bet you can do is on the\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" numbers 6 and 8, this gives you a win ratio of\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" 1.5 to 1 if you win.\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n The minimum ammount you can bet at one time is$10\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n The maximum ammount you can bet at one time is $150\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n If you lose all of your money, you lose and have to walk home =\\(\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \" If you win more then$6000 you are a winner and get chased out of the casino\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n HAVE FUN!\" \u0026lt;\u0026lt; endl;cout \u0026lt;\u0026lt; \"\\n Enter \\\u0027r\\\u0027 to return to main menu.\";cin \u0026gt;\u0026gt; resume;if(resume[0]==\u0027r\u0027) { 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 \u0026gt; 0) { system(\"CLS\"); cout \u0026lt;\u0026lt; \" Craps v1.0\" \u0026lt;\u0026lt; endl; cout \u0026lt;\u0026lt; \" Remaining $\"\u0026lt;\u0026lt;_funds\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; msg \u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \"\\n Dealer says:\\\"All bets please!\\\"\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" Dealer says:\\\"You sir, which number do you want to bet on?\\\" \"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" \\n I will bet on...\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" 1. numbers 2 \u0026amp; 12\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" 2. numbers 4 \u0026amp; 10\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \" 3. numbers 6 \u0026amp; 8 \"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt; \"\\n[SELECT] \"; cin \u0026gt;\u0026gt; _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 \u0026lt;\u0026lt; \"\\n Dealer Says: Good choice,numbers \"\u0026lt;\u0026lt;_sel[0]\u0026lt;\u0026lt;\" \u0026amp; \"\u0026lt;\u0026lt;_sel[1]\u0026lt;\u0026lt;\" it is.\"\u0026lt;\u0026lt;endl;// Start bet ammount check loop to make sure the bet is within range.while(checkingAmt = true) { cout \u0026lt;\u0026lt; \" And how much would you like to bet?\"\u0026lt;\u0026lt;endl; cout \u0026lt;\u0026lt;\"\\n I will bet$\"; cin \u0026gt;\u0026gt; _amt;//Begin first part of check, to see if _amt is less then $10 or more then$150.if( (_amt \u0026lt; 10) || (_amt \u0026gt;= 151) ) {//Display an error message. cout \u0026lt;\u0026lt; \"\\n !!! INVALID AMMOUNT, MINIMUM $10 MAXIMUM$150 !!!\\n\"\u0026lt;\u0026lt;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 \u0026gt; 10) \u0026amp;\u0026amp; (_amt \u0026lt;= 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)\u0026amp;\u0026amp;(betOnNum==1)) { cout \u0026lt;\u0026lt; \"Roll: \"\u0026lt;\u0026lt;diceRoll\u0026lt;\u0026lt;\" IF 1.\"; system(\"PAUSE\"); }else if((diceRoll == 4 || diceRoll == 10)\u0026amp;\u0026amp;(betOnNum==2)) { cout \u0026lt;\u0026lt; \"Roll: \"\u0026lt;\u0026lt;diceRoll\u0026lt;\u0026lt;\" IF 2.\"; system(\"PAUSE\"); }else if((diceRoll == 6 || diceRoll == 8)\u0026amp;\u0026amp;(betOnNum==3)) { cout \u0026lt;\u0026lt; \"Roll: \"\u0026lt;\u0026lt;diceRoll\u0026lt;\u0026lt;\" IF 3.\"; system(\"PAUSE\"); }else cout \u0026lt;\u0026lt; \"LOSER\"; system(\"PAUSE\"); }\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/?do=findComment\u0026comment=4259752", "author": { "@type": "Person", "name": "DrDev", "image": "https://secure.gravatar.com/avatar/8b413305848a0a0ce545071b889987b4?d=https://www.gamedev.net/uploads/monthly_2017_08/D.png.305d27e7540f4b4ff24ba0ae54817526.png", "url": "https://www.gamedev.net/profile/140955-drdev/" }, "dateCreated": "2008-06-30T23:18:56+0000", "text": "oh its \n i was using =( ty\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/?do=findComment\u0026comment=4259986", "author": { "@type": "Person", "name": "Antheus", "image": "https://secure.gravatar.com/avatar/cf0a6ebb73e97e86ae20ae3972d87fd9?d=https://www.gamedev.net/uploads/monthly_2017_08/A.png.e39185feff92ce1b36db2bf96e3bae9e.png", "url": "https://www.gamedev.net/profile/98463-antheus/" }, "dateCreated": "2008-07-01T07:46:21+0000", "text": "if((i==2 || 12) \u0026amp;\u0026amp; (n==1))12 in this context evaluates to true.if((i==2 || true) \u0026amp;\u0026amp; (n==1))X || true is always trueif((true) \u0026amp;\u0026amp; (n==1))true \u0026amp;\u0026amp; X is always Xif(n==1)is what your actual statement is.What you want:if ( ((i==2) || (i==12)) \u0026amp;\u0026amp; (n==1) )\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/?do=findComment\u0026comment=4259988", "author": { "@type": "Person", "name": "Sneftel", "image": "https://secure.gravatar.com/avatar/6937dd2a5931d74fcc793d691e0ad1e2?d=https://www.gamedev.net/uploads/monthly_2017_08/S.png.1f6293a62d3e8d357c99b9c4c3bf1427.png", "url": "https://www.gamedev.net/profile/18969-sneftel/" }, "dateCreated": "2008-07-01T07:55:53+0000", "text": "Your code looks surprisingly good for two days of work. There\u0027s some minor issues with breaking up functions, but nothing that won\u0027t be obvious with a little more work. Good job..keep at it!", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/499720-logical--problem-in-if-statement/" } ] } { "@context": "http://www.schema.org", "@type": "WebSite", "name": "GameDev.net", "url": "https://www.gamedev.net/", "potentialAction": { "type": "SearchAction", "query-input": "required name=query", "target": "https://www.gamedev.net/search/?q={query}" }, "inLanguage": [ { "@type": "Language", "name": "English (USA)", "alternateName": "en-US" } ] } { "@context": "http://www.schema.org", "@type": "Organization", "name": "GameDev.net", "url": "https://www.gamedev.net/", "address": { "@type": "PostalAddress", "streetAddress": "", "addressLocality": null, "addressRegion": null, "postalCode": null, "addressCountry": null } } { "@context": "http://schema.org", "@type": "BreadcrumbList", "itemListElement": [ { "@type": "ListItem", "position": 1, "item": { "@id": "https://www.gamedev.net/groups/", "name": "Groups" } }, { "@type": "ListItem", "position": 2, "item": { "@id": "https://www.gamedev.net/groups/2-for-beginners/", "name": "For Beginners" } }, { "@type": "ListItem", "position": 3, "item": { "@id": "https://www.gamedev.net/forums/forum/71-for-beginners/", "name": "Forum" } } ] } { "@context": "http://schema.org", "@type": "ContactPage", "url": "https://www.gamedev.net/contact/" } Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept 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!