Re: Comparing string input to enum data type
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: Wed, 28 Feb 2007 18:21:47 +0000 (UTC)
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
.
- Follow-Ups:
- Re: Comparing string input to enum data type
- From: dtschoepe@xxxxxxxxx
- Re: Comparing string input to enum data type
- References:
- Comparing string input to enum data type
- From: dtschoepe@xxxxxxxxx
- Re: Comparing string input to enum data type
- From: Yevgen Muntyan
- Re: Comparing string input to enum data type
- From: CBFalconer
- Re: Comparing string input to enum data type
- From: dtschoepe@xxxxxxxxx
- Comparing string input to enum data type
- Prev by Date: Re: Comparing string input to enum data type
- Next by Date: Re: Comparing string input to enum data type
- Previous by thread: Re: Comparing string input to enum data type
- Next by thread: Re: Comparing string input to enum data type
- Index(es):
Relevant Pages
|