Go Down

Topic: enumeration declaration (Read 1 time) previous topic - next topic

kyrpav

i have this
Code: [Select]
enum food{orange,apple,pineapple,watermelon} fruits;

void choosefood(char setfood[]){

fruits=setfood;
switch (fruits):
.....................
}

And this does not work because

cannot convert 'char*' to food enum

basically what i want to do is call a function and give a string. Inside  the function i want to set the string to the value of the enum food fruits.
in general 
Code: [Select]
food fruits=orange;
works but i want to be able to set a string without typing the word exactly something like a reference. I could retrieve the string from another function and give it to this so i wont have manual interference.

tobyb121

Can you just explain this a bit better, do you want a function that you call like: chooseFood("orange") which sets the value fruits to orange (from your enum). If so the only way I think you are going to be able to that is to have a series of if statements with comparing the string values, either with strcmp or your own fuction.

KeithRB

Even though the enums are assigned sequentially, you could do something like this:

char * fruitstrings[6];

/* This part must be in a function! */
fruitstrings[orange] = "orange";
fruitstrings[apple] = "apple";

KeithRB

You might also look into the "stringizing" operator # and create a macro like:

#define enum_string(en) #en

Used like this: enum_string(orange)

kyrpav

well i think you got it. I want to call a function that will give me a string. after that i want to set the string equal to the value of an enumerated variable. I am sure that the string is one of the values.

So one function will give a string that could be "orange ,watermelon, apple" something of theses but i do not know which of them. everyday it would be different.

And i want to have a second function which will do thing. the first thing i want it to do is to set the the string as the value of the enumerated variable like you said

if the string is :

string mystring = "apple";

then

it will do

food fruits= apple;

but i do not know which  the string will be every day so i need this to be updated.

Go Up