Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Use of string in switch case

Use of string in switch case

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 4 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    itkid
    wrote on last edited by
    #1

    Hi, I want to use the string in switch case statement. Can you please help me. Thanks

    B I 2 Replies Last reply
    0
    • I itkid

      Hi, I want to use the string in switch case statement. Can you please help me. Thanks

      B Offline
      B Offline
      berndg
      wrote on last edited by
      #2

      I can only guess and believe you might want to do something like this: switch (myString) { case "Blue": ... break; case "Red": ... break; ... } Trouble is, the C or C++ languages don't do that. C# does. Thus, one solution is to use C#. If you want to use C/C++, you must use nested if/else clauses to emulate the same effect, something like so: if (_tcsicmp(myString, _T("blue")) == 0) { ... } else if (_tcsicmp(myString, _T("red")) == 0) { ... ... } Doesn't look as nice, but, compared to the above fictivious switch statement, gives you control over the comparison algorithm (e.g. use _tcsicmp for case-insensitive comparison, or _tcscmp for case-sensitive comparison), etc.

      1 Reply Last reply
      0
      • I itkid

        Hi, I want to use the string in switch case statement. Can you please help me. Thanks

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        You cannot use a string in switch cases - the cases have to be constant numberic values. If the strings are not known at compile time, then you have to use if...else if...else statements. If they are known at compile time, then you can precompute hashes of the case strings, and switch on a hash of the string you want to switch on. Um, that wasn't very clear, was it?

        void DoSomethingDependingOnString (LPCTSTR szSwitchable)
        {
        switch (ComputeHash(szSwitchable))
        {
        case 0x4wfe: // ComputeHash ("I am a string!")
        DoFirstThing ();
        break;

        case 0xfg13:  // ComputeHash ("I am another string!")
            DoSecondThing ();
            break;
        
        default:
            DoDefaultThing ();
            break;
        }
        

        }

        I hope that was little clearer. Iain.

        B 1 Reply Last reply
        0
        • I Iain Clarke Warrior Programmer

          You cannot use a string in switch cases - the cases have to be constant numberic values. If the strings are not known at compile time, then you have to use if...else if...else statements. If they are known at compile time, then you can precompute hashes of the case strings, and switch on a hash of the string you want to switch on. Um, that wasn't very clear, was it?

          void DoSomethingDependingOnString (LPCTSTR szSwitchable)
          {
          switch (ComputeHash(szSwitchable))
          {
          case 0x4wfe: // ComputeHash ("I am a string!")
          DoFirstThing ();
          break;

          case 0xfg13:  // ComputeHash ("I am another string!")
              DoSecondThing ();
              break;
          
          default:
              DoDefaultThing ();
              break;
          }
          

          }

          I hope that was little clearer. Iain.

          B Offline
          B Offline
          berndg
          wrote on last edited by
          #4

          I would recommend not to use the hash approach, for at least these: i) it is hard to maintain - always think a couple of years and several generations of fellow programmers ahead. Who's going to remember how to create the keys, why and when? ii) it is doomed to fail - hash keys are not unique, so there is a chance that two different strings produce the same key.

          M I 2 Replies Last reply
          0
          • B berndg

            I would recommend not to use the hash approach, for at least these: i) it is hard to maintain - always think a couple of years and several generations of fellow programmers ahead. Who's going to remember how to create the keys, why and when? ii) it is doomed to fail - hash keys are not unique, so there is a chance that two different strings produce the same key.

            M Offline
            M Offline
            markkuk
            wrote on last edited by
            #5

            berndg wrote: ii) it is doomed to fail - hash keys are not unique, so there is a chance that two different strings produce the same key. If you have a finite set of possible strings known at the program development time, you can create a "perfect" hash function[^] that doesn't cause collisions.

            1 Reply Last reply
            0
            • B berndg

              I would recommend not to use the hash approach, for at least these: i) it is hard to maintain - always think a couple of years and several generations of fellow programmers ahead. Who's going to remember how to create the keys, why and when? ii) it is doomed to fail - hash keys are not unique, so there is a chance that two different strings produce the same key.

              I Offline
              I Offline
              itkid
              wrote on last edited by
              #6

              Hi Guy, Thank for showing interest in my proble. Please refer for below link for good solution. http://www.codeguru.com/Cpp/Cpp/cpp_mfc/article.php/c4067/[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups