Thread: Switch case values

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147

    Switch case values

    is it possible to have different switch case values other than integers? such as characters or strings, if so how could this be done.


    thanks,

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Single characters, yes, since they're just integral constants, too.

    Other than that, no.
    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

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    thanks,

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Enumerations as well.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Another type of value is in windows programming. Not really sure what it is, but it may be somthing to do with destroying the open window. Or in english, close the window.
    Double Helix STL

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    There is a way you can use lookup tables to resolve a string (or any other type for that matter) to an int - have a look at this example using weekdays..
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <cctype>
    
    enum { DAY_MONDAY, DAY_TUESDAY, DAY_WEDNESDAY, DAY_THURSDAY, 
            DAY_FRIDAY, DAY_SATURDAY, DAY_SUNDAY, INVALID };
    
    const std::string days_of_week[] = {
            "monday", "tuesday", "wednesday", "thursday",
            "friday", "saturday", "sunday" };
    
    int get_day(std::string str)
    {
        typedef std::vector<std::string>::iterator iter_type;
        static std::vector<std::string> lookup_table(
            days_of_week, 
            days_of_week + (sizeof days_of_week/sizeof *days_of_week)
            );
        std::transform(str.begin(), str.end(), 
                       str.begin(), tolower);
    
        iter_type it ( std::find(lookup_table.begin(), 
                                 lookup_table.end(), 
                                 str)
                     );
        if( it != lookup_table.end() )
            return ( it - lookup_table.begin() );
        return INVALID;
    }
    
    int main()
    {
        std::string test_input("WEDNESDAY");
    
        switch( get_day(test_input) )
        {
        case DAY_MONDAY:
            std::cout << "The day is monday";
            break;
        case DAY_TUESDAY:
            std::cout << "The day is tuesday";
            break;
        case DAY_WEDNESDAY:
            std::cout << "The day is wednesday";
            break;
        case DAY_THURSDAY:
            std::cout << "The day is thursday";
            break;
        case DAY_FRIDAY:
            std::cout << "The day is friday";
            break;
        case DAY_SATURDAY:
            std::cout << "The day is saturday";
            break;
        case DAY_SUNDAY:
            std::cout << "The day is sunday";
            break;
        default:
            std::cout << "Invalid day";
        }
    }
    The get_day() function resolves the string to an integer, which happens to be the position of the day in the lookup table.

    The one drawback with this is that the enums must always match the lookup table, since the enums represent the position of each day in the lookup table.


    the get_day() function does several things..

    first it creates the days_of_week lookup table in the form of a std::vector<std::string>
    secondly it converts the input into all lowercase (because the lookup table is all lowercase)
    thirdly it searches the lookup table for a match to the string
    and finally, it calculates the string's position (relative to the beginning of the lookup table), and returns that.

    if the find() algorithm didn't get a match (it hit the end of the lookup table), then the get_day() function returns the value associated with INVALID.
    Last edited by Bench82; 12-27-2006 at 08:06 PM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Sure. I might start with something like this, though.
    Code:
    // remove INVALID from the enum
    
    size_t get_day(std::string str)
    {
       // no changes
       return static_cast<size_t>(-1);
    }
    
    void foo(const char *text)
    {
       std::string test_input(text);
       size_t index = get_day(test_input);
       if ( index < sizeof days_of_week / sizeof *days_of_week )
       {
          std::cout << "The day is " << days_of_week[get_day(test_input)];
       }
       else
       {
          std::cout << "Invalid day";
       }
       std::cout << '\n';
    }
    
    int main()
    {
       foo("WEDNESDAY");
       foo("BOBSDAY");
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318

    That reminds me...

    There was a recent article on thedailywtf.com where someone precalculated the hashes of the set of expected strings and then did a switch statement on the hash of the user-supplied string.
    Most unusual I would have to say, and you certainly wouldn't want to choose a poor hashing algorithm. It definitely qualifies as premature optimisation.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or maybe even use a std::map to translate from std::string to some enumeration of your choice.
    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.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    thanks Salem, youv given me a few ideas there. at the moment my menu selections are done with corrosponding numbers. however using your string technique and a bit of validiation it may look/feel more proffesional albeit slower to operate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. enumeration with switch case?
    By Shadow12345 in forum C++ Programming
    Replies: 17
    Last Post: 09-26-2002, 04:57 PM