Thread: problem with switch statements.

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    15

    Question problem with switch statements.

    Hello everyone.
    I'm working on making a little card game, and therefore i need to be able to deal cards.
    so im working on function to give you a card.
    I tried this piece of code:

    Code:
       switch (card) { //heart, diamond, clubs or spade   
         case (card<= 13):
            cardEmblem = "heart";
            break;
        case (card <= 26):
            cardEmblem = "diamond";
            break;
        case (card <= 39):
            cardEmblem = "clubs";
            break;
        case (card <= 52):
            cardEmblem = "spade";
        }
    and it didn't work. so i read in Alex' book that the general syntax for switch statements are that where i write (card <= 13) only one value can be.
    so my question, do i have to do this with else if statements?
    Or is it possible with the switch?

    Thanks in advance

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    No, this isn't possible with switch.
    switch's cases can have only compile time constants , not conditions, variables or anything like that.
    If you want conditions, use an if--else-if--else ladder.

    (This isn't the first time someone asked a similar question about switch upon reading that book, I wonder if there is some deficiency in the explanation.)

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    15
    he defined it correctly, but by reading it you assume this is possible. when you read it back, you see he clearly explains it.
    also, i didn't read all of it in the book, because i thought i understood it already xD
    Oh well, lets get the if statements coming then!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Have you considered something like
    Code:
    case ( card / 13 ) {
      case (0):
         cardEmblem = "heart";
         break;
     case (1):
         cardEmblem = "diamond";
         break;
     case (2):
         cardEmblem = "clubs";
         break;
     case (3):
         cardEmblem = "spade";
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    15
    Quote Originally Posted by Salem View Post
    Have you considered something like
    Code:
    case ( card / 13 ) {
      case (0):
         cardEmblem = "heart";
         break;
     case (1):
         cardEmblem = "diamond";
         break;
     case (2):
         cardEmblem = "clubs";
         break;
     case (3):
         cardEmblem = "spade";
    }
    You sir, are smart :P
    Thank you

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Beaten to the punch, so I'll enhance that idea to show the lookup-table way:
    Code:
    static const char *suits[] = {"hearts", "diamonds", "clubs", "spades"};
    assert(card < 52);
    cardEmblem = suits[card / 13];
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Statements
    By lazyturtle in forum C++ Programming
    Replies: 12
    Last Post: 05-02-2007, 02:40 AM
  2. Switch Statements
    By guitarman32 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 07:02 AM
  3. Switch Statements.
    By RealityFusion in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2003, 11:55 PM
  4. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM
  5. Switch statements
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 11:28 AM