Re: Comparing string input to enum data type



"Bill Pursell" <bill.pursell@xxxxxxxxx> writes:
On Feb 27, 8:45 pm, "dtscho...@xxxxxxxxx" <dtscho...@xxxxxxxxx> wrote:
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)?

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



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)