[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Using a case statement with changing variables

Posted on 2007-04-03
Medium Priority
Last Modified: 2010-04-15
      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.

Question by:peterallsop
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 53

Expert Comment

ID: 18842268
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. */
LVL 53

Expert Comment

ID: 18842271
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. ?

Author Comment

ID: 18842329
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.

LVL 53

Expert Comment

ID: 18842377
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.

Author Comment

ID: 18842496
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.
LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 18842549
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 :)

Author Comment

ID: 18842570
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
LVL 53

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question