Re: Comparing string input to enum data type



Bill Pursell wrote On 02/27/07 16:16,:
On Feb 27, 8:45 pm, "dtscho...@xxxxxxxxx" <dtscho...@xxxxxxxxx> wrote:

Greetings.

I am working on an assignment and can't seem to get the right concept
for something I'm attempting to do with enum data types.

I have defined the following in my code:

enum color {red, blue, green, yellow, black, purple, pink};

What I am doing now is reading from a text file a string (char *). I
wish to compare the value read into the array and determine which
color it is from the enum data type. How can I compare a string like
"red" to see if it exists in the values that are part of the
enumerated data type 'color'? I know I need to write some kind of
compare function, but I'm not getting anywhere figuring out how that
should work.



If you've red the string into a, and you want to set c:

if( 0 == strncmp("red", a, sizeof "red") )
c = red;
else if ...

Why not just strcmp("red", a) ?

--
Eric.Sosman@xxxxxxx
.



Relevant Pages

  • Re: Comparing string input to enum data type
    ... for something I'm attempting to do with enum data types. ... What I am doing now is reading from a text file a string. ... color it is from the enum data type. ... How can I compare a string like ...
    (comp.lang.c)
  • Re: Comparing string input to enum data type
    ... for something I'm attempting to do with enum data types. ... What I am doing now is reading from a text file a string. ... How can I compare a string like ... enum color {red, blue, brown, black, purple}; ...
    (comp.lang.c)
  • Re: Comparing string input to enum data type
    ... for something I'm attempting to do with enum data types. ... What I am doing now is reading from a text file a string. ... color it is from the enum data type. ... How can I compare a string like ...
    (comp.lang.c)
  • Re: Comparing string input to enum data type
    ... for something I'm attempting to do with enum data types. ... What I am doing now is reading from a text file a string. ... color it is from the enum data type. ... How can I compare a string like ...
    (comp.lang.c)
  • Comparing string input to enum data type
    ... for something I'm attempting to do with enum data types. ... What I am doing now is reading from a text file a string. ... color it is from the enum data type. ... How can I compare a string like ...
    (comp.lang.c)