446,171 Members | 930 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

# Multiple Expressions in Switch Statements

 P: 10 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: Expand|Select|Wrap|Line Numbers 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? Mar 10 '07 #1
13 Replies

 Expert 100+ P: 1,510 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 Expand|Select|Wrap|Line Numbers     if( num1==1 && num2 ==1 && nium3==1 ....)    {.... }     else      if( num1==2 && num2 == 2 && nium3==2 ....)    {.... } Mar 10 '07 #2

 P: 10 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: Expand|Select|Wrap|Line Numbers 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) Mar 10 '07 #3

 Expert 100+ P: 1,510 you store the five digits in an array and use a loop Expand|Select|Wrap|Line Numbers for(i=0;i<5;i++)   switch (digits[i])    {      case 1:...     case9: } Mar 10 '07 #4

 P: 10 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? you store the five digits in an array and use a loop Expand|Select|Wrap|Line Numbers for(i=0;i<5;i++)   switch (digits[i])    {      case 1:...     case9: } Mar 10 '07 #5

 Expert 2.5K+ P: 3,652 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: Expand|Select|Wrap|Line Numbers for (int i = 0; i < 5; i++) {    cin >> digit;    //Switch statement to evaluate digit } Mar 10 '07 #6

 P: 10 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!) Mar 10 '07 #7

 P: 10 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(?) 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: Expand|Select|Wrap|Line Numbers for (int i = 0; i < 5; i++) {    cin >> digit;    //Switch statement to evaluate digit } Mar 10 '07 #8

 Expert 100+ P: 1,510 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? Mar 11 '07 #9

 Expert 2.5K+ P: 3,652 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. Mar 11 '07 #10

 P: 10 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: Expand|Select|Wrap|Line Numbers 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...) 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. Mar 11 '07 #11

 Expert 2.5K+ P: 3,652 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+. Mar 11 '07 #12

 P: 10 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(?): Expand|Select|Wrap|Line Numbers string convert2bars (int, string& segment)  could you have the switch in a function and call it five times? Mar 11 '07 #13

 Expert 2.5K+ P: 3,652 I'm not sure why you're including the string& - I was thinking: Expand|Select|Wrap|Line Numbers 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 Expand|Select|Wrap|Line Numbers cout << convertToBars(digit1) << convertToBars(digit2) << /* etc */ << endl; Mar 11 '07 #14