Re: Comparing string input to enum data type



In article <1172686660.286081.176300@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
dtschoepe@xxxxxxxxx <dtschoepe@xxxxxxxxx> wrote:
Here's what I ended up doing....

#define COLORS red, blue, green, yellow, black, purple, pink

It isn't clear why you chose to #define those. Any change in them
needs to be reflected in the routine, so decoupling the symbols from
the routine does not buy you anything obvious.

enum color readColor(char * theFavColor)
{
enum colors { COLORS };
enum colors theColor;
if ( strcmp(color, "red") == 0 )
theColor = red;
else if ( strcmp(theFavColor, "blue") == 0)
theColor = blue;
else if ( strcmp(theFavColor, "green") == 0)
theColor = green;
else if ( strcmp(theFavColor, "yellow") == 0)
theColor = yellow;
else if ( strcmp(theFavColor, "black") == 0)
theColor = black;
else if ( strcmp(theFavColor, "purple") == 0)
theColor = purple;
else if ( strcmp(theFavColor, "pink") == 0)
theColor = pink;

return theColor;
}

The variable named color is not set before you do the first
comparison (against "red"). The variable theColor is not set
if none of the choices match.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
.



Relevant Pages