Pages: [1] 2   Go Down
Author Topic: enumeration declaration  (Read 1403 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have this
Code:
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:
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.
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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";
Logged

Offline Offline
Edison Member
*
Karma: 33
Posts: 1446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

#define enum_string(en) #en

Used like this: enum_string(orange)
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 70
Posts: 2171
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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.
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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

Code:
enum food{orange,apple,watermellon} fruits;

how can i set fruits=orange
 with out typing this exactly but by using the char x[].

Code:
fruits=x;
fruits = *x;
does not work i undestand it. But how can i manage it?
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think this is about as close as you're gonna get:

Code:
enum food { orange, apple, watermelon };

food foodbar(const char * x){
if ( !strcmp(x, "orange")) return orange;
if ( !strcmp(x, "apple")) return apple;
if ( !strcmp(x, "watermelon")) return watermelon;
}

void setup(){
food fruits=foodbar("apple");
}
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 07, 2013, 06:23:25 pm by kyrpav » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
!strcmp(x, "orange")

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.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
but foodbar returns something so it makes a warning.
Then, eclipse is wrong. Specifically, what warning?

Quote
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.

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes thank you and sorry about my english i meant that it returns a value of type food. and i do not get why it give warning:

Description   Resource   Path   Location   Type
No return, in function returning non-void   main.cpp /testing/src   line 22   Code Analysis Problem
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:

enum food { orange, apple, watermelon, unknown };

food foodbar(const char * x){
if ( !strcmp(x, "orange")) return orange;
if ( !strcmp(x, "apple")) return apple;
if ( !strcmp(x, "watermelon")) return watermelon;
return unknown;
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

and this was because you did not have else at the last if? this is like a default situation in switch() i think.
Logged

Pages: [1] 2   Go Up
Jump to: