Thread: attaching more values to a case

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    18

    attaching more values to a case

    I know how to write a switch case, type in a char "a" and run case "a"

    But is it possible to somehow attach more than one value for what triggers the case?
    for instance, instead of case 'a': case a,b,c and make all three chars trigger the case?
    Last edited by mangekyou; 04-02-2019 at 04:19 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, just allow follow-through by putting the relevant code followed by the break after the multiple cases.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    Quote Originally Posted by laserlight View Post
    Yes, just allow follow-through by putting the relevant code followed by the break after the multiple cases.
    Got it, thanks. I thought there was a more efficient way than typing case after case .

    for instance

    instead of

    case 'a':
    case 'b':
    case 'b':

    print

    more like

    case 'a' 'b' 'c'

    print

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    There is an extension on GCC:

    Code:
    switch (x) {
    // 'a', 'b', or 'c'...
    case 'a' ... 'c': dosomething(); break;
    }
    But this is not standard...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. attaching a char to each object
    By kdun in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2013, 02:46 PM
  2. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  3. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM
  4. attaching a vcr or other tv device to a computer
    By eats only heads in forum Tech Board
    Replies: 12
    Last Post: 12-08-2002, 01:57 PM
  5. attaching multiple files
    By smd in forum C++ Programming
    Replies: 1
    Last Post: 07-17-2002, 07:25 AM

Tags for this Thread