By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 930 Online
Bytes IT Community
+ 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
  1. switch (num1)
  2. {
  3. case 0:...
  4. case 9:
  5. }
  6.  
  7. switch(num2)
  8. {
  9. case 0:...
  10. case 9:
  11. }
etc.

Assuming that my question makes sense, how do I evaluate multiple expressions in a switch statement?
Mar 10 '07 #1
Share this Question
Share on Google+
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
  1.     if( num1==1 && num2 ==1 && nium3==1 ....)    {.... }
  2.     else
  3.      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
  1. switch (digit1, digit2, digit3, digit4, digit5)
  2. case 1:...
  3. case9:
  4. }
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
  1. for(i=0;i<5;i++)
  2.   switch (digits[i])
  3.    { 
  4.     case 1:...
  5.     case9:
  6. }
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
  1. for(i=0;i<5;i++)
  2.   switch (digits[i])
  3.    { 
  4.     case 1:...
  5.     case9:
  6. }
Mar 10 '07 #5

Ganon11
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
  1. for (int i = 0; i < 5; i++) {
  2.    cin >> digit;
  3.    //Switch statement to evaluate digit
  4. }
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
  1. for (int i = 0; i < 5; i++) {
  2.    cin >> digit;
  3.    //Switch statement to evaluate digit
  4. }
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

Ganon11
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
  1. if num == 1
  2. cout << ",,,||"
  3. else if num == 2
  4. cout << ",,|,|"
  5.  
  6. 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

Ganon11
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
  1. string convert2bars (int, string& segment) 
could you have the switch in a function and call it five times?
Mar 11 '07 #13

Ganon11
Expert 2.5K+
P: 3,652
I'm not sure why you're including the string& - I was thinking:

Expand|Select|Wrap|Line Numbers
  1. string convertToBars(int digit) {
  2.    switch (digit) {
  3.       case 1: return ",,,||";
  4.       case 2: return ",,|,|";
  5.       // etc.
  6.    }
  7. }
Then, in your main (or where you get digit1, digit2, etc), say

Expand|Select|Wrap|Line Numbers
  1. cout << convertToBars(digit1) << convertToBars(digit2) << /* etc */ << endl;
Mar 11 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.