Link to home
Start Free TrialLog in
Avatar of peterallsop
peterallsop

asked on

Using a case statement with changing variables

switch(colour)
{
      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.

Regards
Pete
Avatar of Infinity08
Infinity08
Flag of Belgium image

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. ?
Avatar of peterallsop
peterallsop

ASKER

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
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.
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
then
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.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Pete
Feel free to post your code here once it's done, and I'll take a look at it ...