Thread: Some Trouble - Switching with strings.

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    Some Trouble - Switching with strings.

    Hi folks,

    I am trying to create a C++ program that will operate a switch statement. I have since found out that I cannot switch with strings as i need to. Each case in the switch will be designed to set a string variable.

    Is there any way to achieve what I am suggesting. I have heard about maps, but i think that they are too complex for the simple task that i am trying to program.

    Regards and thanks in advance,
    mintsmike

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Maybe try doing string comparisons, like...

    Code:
    string stuff1 = "meh";
    int sw_case = 0;
    
    if(stuff1 == "meh") sw_case = 0;
    else if(stuff1 == "blah") sw_case = 1;
    
    switch(sw_case){
         case 0:
         break;
    
         case 1:
         break;
    }
    Just a quick example of what I'm thinking, I typed it up in the reply window so its obviously not perfect.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A map could be more appropriate and not too complicated.

    A major difference is that unlike a switch or an if chain, the map's data won't need to be hard-coded.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    147
    In the old days I've used something like this:

    Code:
    p = codestring.find( entry ) / 5;
    
    switch( p )
       {
         ...
       }
    This assumes that codestring is a series of 5 character or less codes, like:

    Code:
    codestring = "one  two  three four";
    Obviously this pseudo code doesn't account for a non-match. For short lists (say 20 or less) it's not much slower than a map.

    It is a naive approach. There is the potential for an odd match. If a key happens to be a subword of another key, for example, it can fail without additional effort.
    Last edited by JVene; 05-02-2009 at 08:17 AM.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Can you direct me to some resource that teaches how to use maps in this way.

    Regards

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I don't see why you'd want to do it... It doesn't really make sense to me. Are you just trying to see if the guy enters something? Either way, this might work.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main( void ) {
      std::string searchStr;
      std::vector<std::string> inputOptions;
      std::vector<std::string>::iterator::difference_type pos;
      inputOptions.push_back( "one" );
      inputOptions.push_back( "two" );
      // etc
    
      std::cout<< "What do you want to switch: ";
      std::cin >> searchStr;
    
      pos = std::find(inputOptions.begin(),inputOptions.end(),searchStr) - inputOptions.begin();
      switch ( pos ) {
        case 0:
          std::cout<< "First position\n";
          break;
    
        case 1:
          std::cout<< "Second position\n";
          break;
    
        default:
          std::cout<< "Not found\n";
          break;
      }
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Strings under the STL?
    By laserlight in forum C++ Programming
    Replies: 9
    Last Post: 07-19-2007, 02:55 AM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. trouble with strings and characters
    By mhenderson in forum C Programming
    Replies: 7
    Last Post: 08-02-2006, 01:29 PM
  4. strings in C++
    By elad in forum C++ Programming
    Replies: 11
    Last Post: 05-20-2006, 02:27 AM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM