Thread: using switch with strings

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Question using switch with strings

    is there a way to use the switch statement with strings...like....

    Code:
    std::string blah;
    
    switch (blah)
    
    {
    case "happy": std::cout << "happy" << endl;
    break;
    }
    sorry if my code is wrong...i havent use switch statements much and i am at school with no compiler to check my syntax...
    help is appreciated....


    thanks
    nextus, the samurai warrior

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    no, switch must have constant numbers, string compares are not possible like that. You have to do string compares in an if block.

    if(!strcmp(str,"blah"))
    ...
    ..else if (!strcmp(str,"you"))
    ...


    etc.

    There are other more complex ways but this'll do it for you.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    An addendum to Fill's response, switch statements take 'integral' types. Since char's are also 'integral' data types, you can use those as well:
    Code:
    switch (x)
    {
    case 'a':      whatever;
                   break;
    case 1:        something else;
                   break;
    case char(13): more code; // non-portable
                   break;
    :
    :
    default:       break;
    }
    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Shadow12345
    Guest
    just to kind of add to what skipper said, chars can be represented by decimal value and mean the same thing. If you are interested in knowing what their values are check out:
    www.asciitable.com
    I hope you find that interesting

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Very cool! Thanks.

    Frankly, I should have been more clear. ASCII is not portable, therefore, sticking with the C++ standard characters is something that you'll want to adhere to.

    (Did "bookmark" Shadow12345's link, though. )

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. C++ Strings under the STL?
    By laserlight in forum C++ Programming
    Replies: 9
    Last Post: 07-19-2007, 02:55 AM
  4. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  5. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM