• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Using a case statement with changing variables

      case 0:      if( colour = black )      { return 0;      } break;
      case 1: if( colour = brown )      { return 1;      } break;
      case 2: if( colour = red )        { return 2;      } break;
      case 3:      if ( colour = orange )      { return 3;      } break;
      case 4:      if ( colour = yellow )      { return 4;      } break;
      case 5: if ( colour = green )      { return 5;      } break;
      case 6: if ( colour = blue )      { return 6;      } break;
      case 7:      if ( colour = violet )      { return 7;      } break;
      case 8:      if ( colour = gray )      { return 8;      } break;
      case 9: if ( colour = white )      { return 9;      } break;

I want to be able to use this switch / case statement for different inputs.
For example B1 = yellow, B2 = green, B3 = gray call the case statement on input to change the input from character to the correct integer

I would prefer to have an explanation of how to do it rather than code if possible.

I await anyones reply.

  • 5
  • 3
1 Solution
I'm not sure what you want to do, but this might suit your needs : just define an enum for the different colours :

    typedef enum { black, brown /*, etc.*/ } colours;

Then you can replace your switch simply with :

    return colour;

No need for a switch !!!

You can of course use a switch like this :

    switch (colour) {
      case black : /* do something black */ break;
      case brown : /* do something brown */ break;
      /* etc. */
If the above was not what you meant, can you explain a bit more what you intended ? ie. what is colour, and how did you get it, etc. ?
peterallsopAuthor Commented:
Sorry should have explained more.
Lets say the user enters 3 colours into the program B1 = yellow, B2 = green, B3 = gray
now for the purpose of the program I need to be able to convert them into numbers using one function instead of lots of if statements (smaller neater code). For B1 the user enters yellow the program then calls the function and changes the value of B1from yellow to 4.

black =      0
brown = 1
red = 2
orange = 3
yellow      = 4
green = 5
blue = 6
violet = 7
gray = 8
white = 9

colour was just a name I used for the example.

Sorry I should have been more specific
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Well, that's exactly what an enum does. It gives names to integer values. ie. black is really the value 0, but you can use black instead.

But, if I understand you correctly, the user will enter a string "black", and you want to get its value 0 ... correct ?

If so, a switch is not really an option here. You'd have to use a whole series of if-elseif statements. (a switch can only handle integer values, not strings).

It's a lot easier to let the user enter an integer value though. You can print a menu with the possible values, and then let the user choose one.

I'm still not sure what you want to do exactly. Can you give a specific example of what would happen ... ie. explain what the user of your program does, and how the code should react to that.
peterallsopAuthor Commented:
The use of the program is a resistor calculator.

So the user enters the 3 band colours (I'm currently ignoring the 3rd for now)
User enters band 1
Band 2

the input of them need to be combined  (multiply the fist band by 10 and then add the second band to the first) the third band is then multiplied by the result of the first two bands.

"But, if I understand you correctly, the user will enter a string "black", and you want to get its value 0 ... correct ?" Yes you are correct.

I can do a whole load of else-if statements but I would have to do them each band.

Would typedef enum work even though there's 1 to 9 and there's also a multiplier which is 0, 10, 100 and so on of the same colours.
http://www.sizes.com/materls/resistorcolor.htm <--- Band Codes
For this purpose using 4 band resistor and ignoring the tolerance

I can easily use if statements but that means alot of code and its messy. Also I'm trying to go along the lines of reusable code.
1) if the user enters a string (ie. types "black"), then there's no 'easy' way to get the integer value. You'll need if-elseif statements with strcmp() to find which colour the user entered, and then assign the correct multiplier value.

2) if you show a menu with the different colours, and their mnemonic value (0, 1, 2, etc.), then the user can just enter that mnemonic value, and you can simply use a switch to assign the correct multiplier value.

It all depends on how you want the user to enter the colours ...
The second option is a lot easier to code (and you can even make use of an enum to make the code more readable), and would be my preferred solution.
The first option allows the user to simply type in the colour without having to find the correct mnemonic value in the menu. So, it's slightly more straightforward for the user, but it will require a lot of ugly (as you called it), and bug-prone code.

Your choice :)
peterallsopAuthor Commented:
Thanks very much for the help, I think I will code both and then see which on is better although I already know that the second one will be better.

Thanks again
Feel free to post your code here once it's done, and I'll take a look at it ...

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now