Can I use string or chars in a switch?

This is a discussion on Can I use string or chars in a switch? within the C++ Programming forums, part of the General Programming Boards category; I cant figure out how to use string or chars in a switch any pointers...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    Can I use string or chars in a switch?

    I cant figure out how to use string or chars in a switch any pointers

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Strings cannot be used in a switch statement. chars can. Its just a limitation of the c++.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Can i get a example of how to use a char cause ive tried

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Code:
    char c = 'b';
    switch(c) {
         case 'a': ...
         case 'b': ...
         case 'c': ...
    }

  5. #5
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    Don't forget that you will need to break out of your switch case.

    Code:
    char a = 'b';
    switch(a) {
      case 'a':
        //do something
        break;
      case 'b':
        // do something
        break;
      default:
        // do something
    }

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Code:
    ...
    includes breaks .

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This only works for single characters, though. Not for C-style strings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loops switch and chars
    By mad_muppet in forum C Programming
    Replies: 7
    Last Post: 07-26-2008, 02:06 PM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM

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