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. switch (string)

switch (string)

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphics
8 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    Manfr3d
    wrote on last edited by
    #1

    Hello guys, how can I use the switch statement with a string, a vector or char[]? Or is there an alternative to this? Thanks and best wishes.

    D CPalliniC M M 4 Replies Last reply
    0
    • M Manfr3d

      Hello guys, how can I use the switch statement with a string, a vector or char[]? Or is there an alternative to this? Thanks and best wishes.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Austrian_Programmer wrote:

      how can I use the switch statement with a string, a vector or char[]?

      string str = "abc123";
      switch(str[0])
      {
      case 'a':
      break;
      }

      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      CPalliniC 1 Reply Last reply
      0
      • D David Crow

        Austrian_Programmer wrote:

        how can I use the switch statement with a string, a vector or char[]?

        string str = "abc123";
        switch(str[0])
        {
        case 'a':
        break;
        }

        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • M Manfr3d

          Hello guys, how can I use the switch statement with a string, a vector or char[]? Or is there an alternative to this? Thanks and best wishes.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Austrian_Programmer wrote:

          Or is there an alternative to this?

          The gold old if. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          In testa che avete, signor di Ceprano?

          M 1 Reply Last reply
          0
          • CPalliniC CPallini

            Austrian_Programmer wrote:

            Or is there an alternative to this?

            The gold old if. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

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

            There are a lot of different cases, so an if statement won't be such a good choice. Futhermore i want to switch the whole string not just the first letter. In the worst case I've to use a map, combining the strings with integers.

            CPalliniC 1 Reply Last reply
            0
            • M Manfr3d

              Hello guys, how can I use the switch statement with a string, a vector or char[]? Or is there an alternative to this? Thanks and best wishes.

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              switch only works with integral types.

              --Mike-- Visual C++ MVP :cool: LINKS~! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

              1 Reply Last reply
              0
              • M Manfr3d

                There are a lot of different cases, so an if statement won't be such a good choice. Futhermore i want to switch the whole string not just the first letter. In the worst case I've to use a map, combining the strings with integers.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                You probably need a hash table.

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • M Manfr3d

                  Hello guys, how can I use the switch statement with a string, a vector or char[]? Or is there an alternative to this? Thanks and best wishes.

                  M Offline
                  M Offline
                  Manfr3d
                  wrote on last edited by
                  #8

                  I found a solution to my problem using maps and enums. If anyone is interested in the complete way to do it can be found at 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