Using a case statement with changing variables

Posted on 2007-04-03
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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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. <--- 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 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 :)

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to Post data to a PHP webpage from an embedded C application using TCP/IP? 5 234
Super Scope, DHCP 5 91
How to learn Linux? 10 66
nested if statement in excel help 4 37
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 how to use strings and some functions related to them in the C programming language.

809 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