Re: Comparing string input to enum data type
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 27 Feb 2007 14:17:59 -0800
"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"
.
- Follow-Ups:
- Re: Comparing string input to enum data type
- From: Bill Pursell
- 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: Bill Pursell
- Comparing string input to enum data type
- Prev by Date: Re: Redirecting stderr
- Next by Date: Re: how to read/write a characters stream which is either of one byte/2 byte
- 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
|