If/else if or Switch and strings

I'm wondering what is the best way to handle this bit of code. The user is asked to input a string from a list of strings, and each string has some variables that it sets. I then want to do some calculations and outputs with those variables based on the string that was entered. All the strings fall into 4 "categories" and all the calculations will be the same for each string in that category. I'm not sure if I should use 4 if/elseif statements with multiple test conditions, a switch with multiple cases named with the string names with breaks to group the categories, or if there is a better way to this situation. I'm pretty new to C++, so if there is a much better way to do this I just need pointed in the right direction and I can do some more research/testing myself.

ex:

if (string == Blue || string == LightBlue || string == DarkBlue) {
calcs and ouput;
}
else if (string == Red || string == Orange) {
calcs and output;
}
else if (string == Black){
calcs and output;
}

OR

switch (string){
case Blue:
case LightBlue:
case DarkBlue:
calcs and output;
break;
case Red:
case Orange:
calcs and output;
break;
case Black:
calcs and output;
}
Last edited on
Definitely the first method, seeing as how switch statements cannot be applied to strings.
http://stackoverflow.com/questions/650162/why-switch-statement-cannot-be-applied-on-strings
Thanks for the info and link. That part of my program wasn't as bad/long as I thought it would be and seems to be testing fine, for now anyways.
Topic archived. No new replies allowed.