Thread: Using a switch for multiple character, now!

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Smile Using a switch for multiple character, now!

    To Whomever can Help Me;

    You've shown me that you can use switch for single characters, but what about for a range of characters. For example, entering a char between C and W does the same thing for any char between those 2 chars. A, B, X, Y, and Z are there own separate switches. Any help would be greatly appreciated!

    Dave

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    switch(value)
    {
        case 'A' : DoStuff();break;
        case 'B' : DoOtherStuff();break;
        case 'X' : DoSomethingCompletelyDifferent();break;
        case 'Y' : DoEndOfMicrosoft();break;
        case 'C' :
        case 'D' :
        case 'E' :
        case 'F' :
        case 'G':
        case 'H':
         .
        .
        .
        case 'V':
        case 'W': DoNormFunc();break;
        default: DoBigScrewUp();
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    I would use if statements for that

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    A switch is more readable and therefore better maintainable.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    yes, i agree. i always use switch before if.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    This would probably be easier in an if because you could do if(input > 'someletter' && input < 'otherletter')

    instead of doing case 'a': case 'b': etc etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  4. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  5. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM