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
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.
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.
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.
Enums are for ranges, what you have is a discrete set of values.
You are better off having a collection of strings in progmem that you can compare against, your key or index ( what would be the enum value ) is simply the index of the corresponding progmem string.
The error you had here:
fruits=setfood;
is because 'setfood' is a pointer, a 16-bit value that has nothing to do with the data it points to. The other problem here is on initialisation the enum can happily be set to values outside the range you specified. This is because the valid enum range is more than what is specified in the brackets.
i understand that there is a way to do my job with strings and nested ifs.
regardless what i want to do can you give me a solution to the problem i will make more specific.
i have a string
char x[]="orange";
i do not know whati s written in the string but i know that it is enumerated in the values of an enumerated variable.i am sure of it.
so the variable is
enum food{orange,apple,watermellon} fruits;
how can i set fruits=orange
with out typing this exactly but by using the char x[].
fruits=x;
fruits = *x;
does not work i undestand it. But how can i manage it?
ok thank you i have one more question. eclipse understands that function foodbar belongs to datatype food and it is a non-void function , and foodbar returns something but eclipse does not understand it and it gives warning.
It works in that example, but IMO it's bad practice to treat the return value from strcmp() as a boolean since it isn't one. I'd much rather see an explicit comparison with zero. (Apart from that quibble, I think you've described the simplest and most straight forward implementation.)
eclipse understands that function foodbar belongs to datatype food
Then it (or you) understand it all wrong. The function is declared to return a value of type food, which is an enum.
but foodbar returns something so it makes a warning.
Then, eclipse is wrong. Specifically, what warning?
also you could say that the foodbar functions returns a food variable
Again, you'd be wrong. It returns a value of type food, not a variable of type food. What you do with that value is up to you. You can store it in a variable of type food, or print it, or pass it to a function that expects a food.
kyrpav:
ok thank you i have one more question. eclipse understands that function foodbar belongs to datatype food and it is a non-void function , and foodbar returns something but eclipse does not understand it and it gives warning.
Eclipse is complaining because I should have written something more like:
No return, in function returning non-void main.cpp /testing/src line 22 Code Analysis Problem
means that not all paths properly return a value. You need to add another value, None, to the enum and return that at the end of the function, if no other return path is followed.
It works in that example, but IMO it's bad practice to treat the return value from strcmp() as a boolean since it isn't one. I'd much rather see an explicit comparison with zero. (Apart from that quibble, I think you've described the simplest and most straight forward implementation.)
Yeah, and all if statement expressions should be followed by a compound {} statement