CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jul 2005
    Location
    Krosno, Poland
    Posts
    51

    using null terminated strings with switch/case

    how can i use strings inside switch block in c/c++,
    what macro i must create to make folowing code work?
    Code:
    #define CMD_1 "cmd 1"
    #define CMD_2 "cmd 2"
    
    Function( char* command )
    {
    	switch(command){
    	case CMD_1:
    		...
    		break;
    	case CMD_2:
    		...
    		break;
    	}
    }

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: using null terminated strings with switch/case

    You can't. A switch can only be used on itegral types. You should use a bunch of if statements for your case.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: using null terminated strings with switch/case

    Well, if you want that, switch to C#. (the pun was intended )
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: using null terminated strings with switch/case

    When actions for each option are enclosed within separate functions rather than in single 'case block' maybe creating a map of strings to function pointers would be good idea. Basically it would be pretty the same, but it maybe would make your code clearer (if there is a lot of options, and not only 2).

    Unfortunately, as Marc said, 'switch' is useful only for integral types.

    Hob
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    Jul 2005
    Location
    Krosno, Poland
    Posts
    51

    Re: using null terminated strings with switch/case

    Quote Originally Posted by cilu
    Well, if you want that, switch to C#. (the pun was intended )
    i know what u mean , but id rather stay with regular c and use if-elseif set.

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: using null terminated strings with switch/case

    If it is C++ code, you can use a std::map<std::string, AnotherType>.
    Here are the types you can use for AnotherType:
    • A pointer to an abstract base class containing at least one ExecCommand virtual method, but maybe a more complex interface, depending on your projects.
    • A pointer to a function (which executes the command).
    • An integer.
      This integer can be used as an identifier in a switch statement.
      Use this solution, only if the switch clauses, really need the local variables and data of the function.


    But, you probably want to code that in C.
    In that case, you can try to emulate a std::map class (maybe with a less performant algorithm), or write your own hash table.
    If performance is not an issue, or if there are very few switch clauses, you can even use an array of structures (each structure contains a const char* and a pointer to function, or identifier).
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    Sep 2005
    Posts
    26

    Re: using null terminated strings with switch/case

    If you don't have very many commands to keep track of you could do something like this:

    Code:
    #define CMD_1 1
    #define CMD_2 2
    
    #define SWITCH(x) switch( (strcmp(x,"cmd 1") == 0) ? 1 :  \
                                               (strcmp(x ,"cmd 2") == 0) ? 2 :  \
                                             0)
    
    void Function( char* command )
    {
    	SWITCH(command){
    	case CMD_1:
    		break;
    	case CMD_2:
    		break;
    	}
    }
    Last edited by wschweit; September 28th, 2005 at 02:47 PM.

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: using null terminated strings with switch/case

    There is no such language as C/C++.

    You either want to write it in C or in C++.

    If programming in C++, I'd generally recommend SuperKoko's first method but it's more complex than it appears.

    In order to use the polymorphism, you'll need a map from string to pointers to different implementations of an abstract base class. (The virtual method doesn't have to called ExecCommand).

    How you'd set this map up and where it belongs will depend on your overall design. In particular, how the instances of these abstract classes are created, and how they are destroyed. If they have no data members then I'd recommend making global instances. Globals should generally be avoided though for any classes that have data members (thread-safety issues, for one. hidden side-effects, etc). You might want instances in the class that contains the map, or a special class that has the map and the instances.

    Finally, you might want to create them on the heap, in which case they'd have to be deleted at some point. If you use that approach, you'll want to use reference-counted pointers or COW-pointers.

  9. #9
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: using null terminated strings with switch/case

    wschweit: your code is wrong :
    == compares pointers and not the strings.
    Instead, you should use strcmp.
    And instead of using an ugly macro, it is preferable to use a function:
    Code:
    unsigned StringToCommandID(const char *cmd)
    {
    if (strcmp(cmd,"cmd 1")==0) return 1;
    else if (strcmp(cmd,"cmd 2")==0) return 2;
    else return 0;
    }
    And remember that this approach is only acceptable if there are very few cases.
    In other cases, you should at least put the associations between strings and integers, in a container; at least a C-style array of structures, if you program in C, or a std::map with the method NMTop40 described if you program in C++
    Last edited by SuperKoko; September 28th, 2005 at 02:09 PM. Reason: replaced return 1, with return 2
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  10. #10
    Join Date
    Sep 2005
    Posts
    26

    Wink Re: using null terminated strings with switch/case

    yes, strcmp is the correct way to compare, my bad. Too used to c++ std::strings.

    A function is the most maintainable, but the macro makes your code easier to read in this case.
    std::map is definately the best way to go though.

    Your code is wrong too! doesn't distinguish between 1 and 2.

  11. #11
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: using null terminated strings with switch/case

    Quote Originally Posted by wschweit
    Your code is wrong too! doesn't distinguish between 1 and 2.

    right, i edited it!
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  12. #12
    Join Date
    Sep 2005
    Posts
    26

    Re: using null terminated strings with switch/case

    Fixed mine too in case someone tried to use it. It worked when I tried it before I posted...

  13. #13
    Join Date
    Jul 2005
    Location
    Krosno, Poland
    Posts
    51

    Re: using null terminated strings with switch/case

    thanx for the ideas, maybe ill try to figure out something with map class

  14. #14
    Join Date
    Jun 2002
    Posts
    1,417

    Re: using null terminated strings with switch/case

    Quote Originally Posted by NMTop40
    There is no such language as C/C++.

    You either want to write it in C or in C++.
    That's what it means in American English But I can understand how it can be confusing to non-Americans or others not accustomed to reading gobbledegook.

  15. #15
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: using null terminated strings with switch/case

    You can also use enum for integer ID and populate map with pairs <string, id>. It's better for cases when you need just few of whole list (but more then 2-3 cases). When number of cases is big enough (about 5 or more), switch block would become messy and it would be better to use distinct functions/functors. And don't use those null terminated arrays, use strings instead.
    "Programs must be written for people to read, and only incidentally for machines to execute."

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured