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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Switch Statement

Switch Statement

Scheduled Pinned Locked Moved C / C++ / MFC
c++
7 Posts 5 Posters 1 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.
  • T Offline
    T Offline
    T RATHA KRISHNAN
    wrote on last edited by
    #1

    I wrote the following c++ code. #include float meters, centimeters; char choice; main () { cout<>meters; centimeters = (float) 100 * meters; cout<<"The length in centimeters is"; cout<>centimeters; meters = centimeters/100; cout<<"The Length in Meters is:"; cout<

    P C T C 4 Replies Last reply
    0
    • T T RATHA KRISHNAN

      I wrote the following c++ code. #include float meters, centimeters; char choice; main () { cout<>meters; centimeters = (float) 100 * meters; cout<<"The length in centimeters is"; cout<>centimeters; meters = centimeters/100; cout<<"The Length in Meters is:"; cout<

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      T.RATHA KRISHNAN wrote:

      I got this error: "case value 1 already used".

      It because of your logic. You are using boolean operation as case input. Which is repetitive. case should be different in switch statement.

      Prasad Notifier using ATL | Operator new[],delete[][^]

      1 Reply Last reply
      0
      • T T RATHA KRISHNAN

        I wrote the following c++ code. #include float meters, centimeters; char choice; main () { cout<>meters; centimeters = (float) 100 * meters; cout<<"The length in centimeters is"; cout<>centimeters; meters = centimeters/100; cout<<"The Length in Meters is:"; cout<

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        T.RATHA KRISHNAN wrote:

        case('m'||'M'):

        This is wrong. This will in fact evaluate to a boolean, not a character. What you need to do is this: case('m'): case('M'): // Code here break;


        Cédric Moonen Software developer
        Charting control [v1.1]

        1 Reply Last reply
        0
        • T T RATHA KRISHNAN

          I wrote the following c++ code. #include float meters, centimeters; char choice; main () { cout<>meters; centimeters = (float) 100 * meters; cout<<"The length in centimeters is"; cout<>centimeters; meters = centimeters/100; cout<<"The Length in Meters is:"; cout<

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          T.RATHA KRISHNAN wrote:

          I got this error: "case value 1 already used". What is the cause for this error?

          you only have to use constant in case field.. switch case just work like goto statement but with scope

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

          1 Reply Last reply
          0
          • T T RATHA KRISHNAN

            I wrote the following c++ code. #include float meters, centimeters; char choice; main () { cout<>meters; centimeters = (float) 100 * meters; cout<<"The length in centimeters is"; cout<>centimeters; meters = centimeters/100; cout<<"The Length in Meters is:"; cout<

            C Offline
            C Offline
            C Mahesh
            wrote on last edited by
            #5

            Use choice = (char)toupper( choice ) ; switch( choice ) { case 'M' : ---------- ---------- break ; case 'C' : ----------- ---------- break ; }

            T T 2 Replies Last reply
            0
            • C C Mahesh

              Use choice = (char)toupper( choice ) ; switch( choice ) { case 'M' : ---------- ---------- break ; case 'C' : ----------- ---------- break ; }

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              C Mahesh wrote:

              choice = (char)toupper( choice ) ;

              one of most simpler way to solve above problem!

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

              1 Reply Last reply
              0
              • C C Mahesh

                Use choice = (char)toupper( choice ) ; switch( choice ) { case 'M' : ---------- ---------- break ; case 'C' : ----------- ---------- break ; }

                T Offline
                T Offline
                T RATHA KRISHNAN
                wrote on last edited by
                #7

                Fantastic! It's working. Thank You Mahesh.

                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