Thread: switch case

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Angry switch case

    I tryed to figure out this bug for 2 hours, couldn't get it. I'm just trying to make sure I know how to do this (its not homework) and this is the only thing I could think of making. I am using DJGPP and every time I try to run it, all of the case lines get an error. Whats wronge? It worked in BASIC!!!

    Code:
        switch ( age ) {
             case >= 0 && < 5:
                  cout <<"You are a toddler!\n";
                  break;
             case >=5 && < 13:
                  cout <<"You are a kid!\n";
                  break;
             case >= 13 && < 19:
                  cout <<"You are a teenager!\n";
                  break;
             case >= 20 && < 30:
                  cout <<"You are a young adult!\n";
                  break;
             case >= 30 && < 55:
                  cout <<"You are an adult!\n";
                  break;
             case >= 55:
                  cout <<"You are a valued old dude!\n";
                  break;
             }//end switch
    Please help! Thanks!!!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You cannot make case statements like that with ranges. You must explicitly make a case statement for each possible value (not recommended), use if/else statements, or use an advanced solution like a map.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Thanks! but wow... thats pretty stupid. at least i know i know how to use switch case...

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Additionally, each time you use a comparison operator, it must have an operand on each side (there is no such thing as implicit operands). That is, the following: 'if (x < 2 && > 1)' meaning, 'if x less than 2 and x is greater than 1' will not fly.

    There is no real reason to do this in this case, but you could also put people into age 'bins', and use a switch:
    Code:
    enum age_category { ac_toddler, ac_kid, ac_teenager, ac_adult, ac_older_than_dirt };
    
    age_category cat;
    if(age < 5)
       cat = ac_toddler;
    // etc...
    else
       cat = ac_older_than_dirt;
    
    switch(cat)
    {
       case ac_toddler:
          std::cout << "A toddler!" << std::endl;
          break;
    // etc...
    }
    The only reason to do this would be if you needed that information (which category a person is in) too often to make recomputing it tedious (... if the code would show up many places).
    Last edited by Zach L.; 08-24-2005 at 07:20 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM