Like Tree3Likes
  • 2 Post By Salem
  • 1 Post By iMalc

problem with switch statements.

This is a discussion on problem with switch statements. within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I'm working on making a little card game, and therefore i need to be able to deal cards. ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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.)
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    32,821
    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";
    }
    manasij7479 and iMalc like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,308
    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];
    sfryett likes this.
    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, 03:40 AM
  2. Switch Statements
    By guitarman32 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 08:02 AM
  3. Switch Statements.
    By RealityFusion in forum C++ Programming
    Replies: 16
    Last Post: 08-20-2003, 12:55 AM
  4. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 03:36 PM
  5. Switch statements
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 12:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21