I am trying to convert an Enum type called:

enum Category{unknown = -1, meat, poultry, seasfood, diary, vegetable, fruit, grain,sweet};

And a struct Food that contains array Category category.

The user is given numbers to enter for the Category types, ie: 0 = meat, etc.

My program can take that in and output the number for the category. But I need to convert that number so that category output reads the name of the category instead of the number of the category.

I tried doing a static_cast<Category>(temp) to convert the input, but my compiler gave a convertion error. I wrote a function to convertEnum using switches:

void convertEnum(Food foodItem[],Category category)
{
int i = 0;
switch (category)
{
case 0: strcpy(foodItem[i].category, "meat");
break;
case 1: strcpy(foodItem[i].category, "poultry");
break;
case 2: strcpy(foodItem[i].category, "seafood");
break;
case 3: strcpy(foodItem[i].category, "dairy");
break;
case 4: strcpy(foodItem[i].category, "vegetable");
break;
case 5: strcpy(foodItem[i].category, "fruit");
break;
case 6: strcpy(foodItem[i].category, "grain");
break;
case 7: strcpy(foodItem[i].category, "sweet");
}

And I get error codes for unable to convert....

Any help would be appreciated. If my function to convertEnum is correct, would the best place to call the function be before the input of category or during the output?

My project was "done" until I had to convert the category number. :(

As far as I know, there isn't a way of getting the enum's name directly from the number is represents.

The best way to do this is to create a string array containing the names of the foods. Then reference it with the number, like this:

foodItem.name[i]

Or you could use an enum instead of a number:

foodItem.name[meat]

Hope this helps

OMG! where did all those color codes in your post come from?? Did you enter all that crap manually? Or did your editor do that?

I just tried hitting the "quote" button on his post to see the BB code he used - and it's like this:

[code=c]
[color=#1111]whatever code here;[color]
etc.[/code]

Just so you know, to color code manually you must use [code] tags. If you use [code=c], color tags will be ignored, because that tag automatically colors code for you.

Edited 3 Years Ago by Dani: Formatting fixed

The best way to do this is to create a string array containing the names of the foods. Then reference it with the number, like this:

foodItem.name[i]

Or you could use an enum instead of a number:

foodItem.name[meat]

Hope this helps

I think this is definitely the method to input the category number and acc. copy the category name into the string. I dont think you even need a string array.
for eg:

int category;
printf("Enter category : ");
scanf("%d",&category);
void convertEnum(Food foodItem[],category)
{
int i = 0;
switch (category)
{
case 0: strcpy(foodItem[i].name, "meat");
break;
case 1: strcpy(foodItem[i].name, "poultry");
break;

....
}

Close, but:

void convertEnum(Food foodItem[], int category)

and:

int i = 0;
switch (category)
{
case 0: strcpy(foodItem.name, "meat");

Why use foodItem, if int == 0 and never changes?

" Life is like a box of chocolates, you never know what you are going to get " - Forrest Gump

That's not true, his mother always told him that. So it should be: "- Forrest Gumps mother":)

Edited 6 Years Ago by Nick Evan: n/a

Close, but:

void convertEnum(Food foodItem[], int category)

and:

Why use foodItem, if int == 0 and never changes?

thnx for pickin out a couple of glitches.


That's not true, his mother always told him that. So it should be: "- Forrest Gumps mother":)


regards Niek

I was more intent on mentioning the movie the quote is frm :)

I think this is definitely the method to input the category number and acc. copy the category name into the string. I dont think you even need a string array.

Tell me, which do you think looks better, yours or mine?

char *food_names[] = {"meat", "poultry"};
    enum {meat=0, poultry};
    
    strcpy(foodItem.name, food_names[meat]);
void convertEnum(Food foodItem[],category)
{
int i = 0;
switch (category)
{
case 0: strcpy(foodItem[i].name, "meat");
break;
case 1: strcpy(foodItem[i].name, "poultry");
break;

The latter requires a entire function just to do the conversion; mine simply references with either an index or with an enum. You can put this right in the main() function because it's so short. I'm not an expert in optimization, but I'd say directly referencing the memory instead of a switch() statement is faster.

Thanks for the tips everyone. I wrote a function called convertEnum using the strcpy switch block (as above) and it worked.

I don't know why my post ended up so funky looking. I think it's because I edited my preview post before reposting. I'll have to watch that next time.

-Sheila

This article has been dead for over six months. Start a new discussion instead.