Multiple Expressions in Switch Statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jsta43catrocks
    New Member
    • Mar 2007
    • 10

    Multiple Expressions in Switch Statements

    In C++, my "switch" statement is okay when I ask it do evaluate ONE expression. (My number that I'm evaluating is one of ten single digits; I have ten cases for the ten digits.)

    BUT, I have five separate digits to evaluate.

    I can't believe that the only solution is to do:

    Code:
    switch (num1)
    {
    case 0:...
    case 9:
    }
    
    switch(num2)
    {
    case 0:...
    case 9:
    }
    etc.

    Assuming that my question makes sense, how do I evaluate multiple expressions in a switch statement?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    not sure about your logic requirements?
    are the statements associated with case 0:. case 1: etc the same in all the switch statements. Something like
    Code:
        if( num1==1 && num2 ==1 && nium3==1 ....)    {.... }
        else
         if( num1==2 && num2 == 2 && nium3==2 ....)    {.... }

    Comment

    • jsta43catrocks
      New Member
      • Mar 2007
      • 10

      #3
      I have a list of five different numbers (digits).

      My switch statement does something different whether the digit == 1 or whether the digit == 2, etc.

      I'm doing the switch statement five different times to five different digits.

      It's not liking me trying this:

      Code:
      switch (digit1, digit2, digit3, digit4, digit5)
      { 
      case 1:...
      case9:
      }
      but that's essentially what I'm trying to do.

      Writing out the entire thing five separate times doesn't seem right.

      Does this make sense? (the question, that is)

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        you store the five digits in an array and use a loop
        Code:
        for(i=0;i<5;i++)
          switch (digits[i])
           { 
            case 1:...
            case9:
        }

        Comment

        • jsta43catrocks
          New Member
          • Mar 2007
          • 10

          #5
          Sadly, we can't use arrays for this.

          It makes sense, but this is for a project for school, and we haven't gotten that far in the text yet. (and the teacher has yet to talk about them.)

          Are there any other options?




          Originally posted by horace1
          you store the five digits in an array and use a loop
          Code:
          for(i=0;i<5;i++)
            switch (digits[i])
             { 
              case 1:...
              case9:
          }

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Is there a way that you can use a loop to evaluate one digit at a time, or do you have to have them stored as you show (digit1, digit2, ..., digit5)?

            I'm thinking something like this:

            Code:
            for (int i = 0; i < 5; i++) {
               cin >> digit;
               //Switch statement to evaluate digit
            }

            Comment

            • jsta43catrocks
              New Member
              • Mar 2007
              • 10

              #7
              Originally posted by jsta43catrocks
              Sadly, we can't use arrays for this.

              It makes sense, but this is for a project for school, and we haven't gotten that far in the text yet. (and the teacher has yet to talk about them.)

              Are there any other options?
              I've tried converting it all to a large "if...if else" statement, but it's REALLY long because I've got 10 "else's" for each of the 5 digits, so it's 50+ lines long. (UGH!)

              Comment

              • jsta43catrocks
                New Member
                • Mar 2007
                • 10

                #8
                Each digit is already named (digit1, digit2, etc.), because they're already being converted from being "chars" into "ints."

                (They're being read from a file, but "12345" is read as an entire number, so I'm reading them each as characters first -- one at a time -- and then converting them to integers, so then I can work with them.)

                In my effort to boil the question down to the simplest elements, some details get lost.

                My "if...if else" statements WORK (see other post), but it looks so cumbersome and awkward. I just thought the switch statement would make it easier(?)




                Originally posted by Ganon11
                Is there a way that you can use a loop to evaluate one digit at a time, or do you have to have them stored as you show (digit1, digit2, ..., digit5)?

                I'm thinking something like this:

                Code:
                for (int i = 0; i < 5; i++) {
                   cin >> digit;
                   //Switch statement to evaluate digit
                }

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  Originally posted by jsta43catrocks
                  I've tried converting it all to a large "if...if else" statement, but it's REALLY long because I've got 10 "else's" for each of the 5 digits, so it's 50+ lines long. (UGH!)
                  could you have the switch in a function and call it five times?

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    What exactly are you going to be doing depending on these digits? Depending on what it is, there might be a way to simplify this process.

                    Comment

                    • jsta43catrocks
                      New Member
                      • Mar 2007
                      • 10

                      #11
                      Chances are, this will sound more complicated than it is, but here goes.

                      We're doing mock mail bar codes.

                      Each digit represents a series of whole bars ("|") and "half bars" (","), so that if my digit is "1," then the output for that digit would be ",,,||". Each digit has some combination of two "|'s" and three (commas), for a total of five "marks" per digit.

                      A 5-digit zip code then has 30 "marks."

                      My "if...if else" statements are, essentially:

                      Code:
                      if num == 1
                      cout << ",,,||"
                      else if num == 2
                      cout << ",,|,|"
                      
                      etc.
                      Can I put the ("marks combos") in reference parameters to streamline it some?
                      (Since the function is printing, I only have value parameters right now...)

                      Originally posted by Ganon11
                      What exactly are you going to be doing depending on these digits? Depending on what it is, there might be a way to simplify this process.

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        You could probably write a short function. As a parameter, give it an integer (the digit), and return the |, combination as a string. This way, you can get each digit and then call the function to find the string representation. You will still need the switch statement/if...else statements, but at least it will only be 10 lines rather than 50+.

                        Comment

                        • jsta43catrocks
                          New Member
                          • Mar 2007
                          • 10

                          #13
                          Let me try to work that out. I like that idea.

                          I'm assuming you mean to call each number in the body, and have the switch in the function(?) I was calling each number in the function (which had the switch).

                          Would my definition look something like(?):

                          Code:
                          string convert2bars (int, string& segment)
                          Originally posted by horace1
                          could you have the switch in a function and call it five times?

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            I'm not sure why you're including the string& - I was thinking:

                            Code:
                            string convertToBars(int digit) {
                               switch (digit) {
                                  case 1: return ",,,||";
                                  case 2: return ",,|,|";
                                  // etc.
                               }
                            }
                            Then, in your main (or where you get digit1, digit2, etc), say

                            Code:
                            cout << convertToBars(digit1) << convertToBars(digit2) << /* etc */ << endl;

                            Comment

                            Working...