Solved

Using a case statement with changing variables

Posted on 2007-04-03
8
211 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:peterallsop
  • 5
  • 3
8 Comments
 
LVL 53

Expert Comment

by:Infinity08
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. */
    }
0
 
LVL 53

Expert Comment

by:Infinity08
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. ?
0
 

Author Comment

by:peterallsop
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
0
 
LVL 53

Expert Comment

by:Infinity08
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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:peterallsop
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
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.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 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 :)
0
 

Author Comment

by:peterallsop
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
Pete
0
 
LVL 53

Expert Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now