Re: Comparing string input to enum data type
- From: "dtschoepe@xxxxxxxxx" <dtschoepe@xxxxxxxxx>
- Date: 28 Feb 2007 12:14:18 -0800
On Feb 28, 10:21 am, rober...@xxxxxxxxxxxxxxxxxx (Walter Roberson)
wrote:
#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.
I goofed there on the 'color' reference, it should also be
'theColor'.
I'll add a default assignment at the bottom, not a bad idea.
This is just a small assignment where we are creating a structure that
represents a person and one of the characteristics is a favorite
color. I then build a linked list of Persons and perform various
operations on that list.
I used #define so I would have less places to edit in case I added a
color, since I use it in at least 2 places.
David
.
- Follow-Ups:
- Re: Comparing string input to enum data type
- From: Keith Thompson
- 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
- Re: Comparing string input to enum data type
- From: Walter Roberson
- Comparing string input to enum data type
- Prev by Date: Re: syntactically comprehensive test program
- Next by Date: Re: Source Code for C Unleashed
- Previous by thread: Re: Comparing string input to enum data type
- Next by thread: Re: Comparing string input to enum data type
- Index(es):