Thread: Merging Case Functions

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    40

    Merging Case Functions

    Instead of this:
    Code:
    case 'r':
    	{ Mufftastic code. }
    case 'R':
    	{ Same mufftastic code. }
    I need something that combines them, like this (even though this code doesn't work):
    Code:
    case 'r' || 'R':
    	{ Mufftastic code. }
    Can anyone offer any advice? Examples, even? Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Code:
    case 'r' :
    case 'R' :
    //insert code here
    break;
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is such a thing as "fallthrough":
    Code:
    case 'r':
      /* Nothing here, so we "fall through" to the next case */
    case 'R':
      /* Code. */
    Which is why break is so important in cases, normally.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or you could even do
    Code:
    switch ( toupper(ch) ) {
      case 'R':  // also case 'r' from the toupper() in the switch
    }
    Consider this, if you have a lot of case letters to choose from. A missing fall-through case would be a hard bug to spot in a sea of other cases.

    But if you also have
    Code:
    switch ( monty ) {
      case 'X':  // do something
      case 'x':  // and now for something completely different
    }
    then you will need to use the fall through style for any letters where case does not matter.
    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.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    40
    I never did think about doing such a thing. Thanks, everyone!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you want a little fun with switch case, to illustrate the issue...

    Code:
    #include <stdio.h>
    
    int main (void)
      {
        int i;
    
        for (i = 0; i < 5; i ++)
          { 
             switch(i)
               {
                  case 0 :
                     printf("The ");
                  case 1 : 
                     printf("fast ");
                  case 2:
                     printf("feline ");
                  case 3:
                     printf("flies ");
                   case 4:
                     printf("farthest");
                   case 5:
                     printf("\n");
                  }
               printf "\n");
             }
      return 0; 
    }
    Last edited by CommonTater; 05-27-2011 at 10:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  2. Functions in a switch case...
    By yaya in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2008, 03:58 AM
  3. merging
    By linuxdude in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2008, 01:26 AM
  4. switch/case & functions
    By Torsin in forum C++ Programming
    Replies: 8
    Last Post: 09-11-2003, 10:50 AM
  5. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM