Convert a string to an enum value?

Hello everyone,

asssuming i have the following enumeration:

 
  enum fruits {APPLE, PEAR, BANANA};


how can i convert a string to an enum value in order to do this?
1
2
3
4
5
6
7
8
9
10
  mystr=getString();
switch (mystr) {

    case APPLE:
        cout << "This is an apple" << endl;
        break;
    case PEAR:
        break;
     ...
}


thank you in advance
you need a function to convert the string to a Fruit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

enum Fruit {APPLE, PEAR, BANANA};

Fruit convert(const std::string& str)
{
    if(str == "APPLE") return APPLE;
    else if(str == "PEAR") return PEAR;
    else if(str == "BANANA") return BANANA;
}

int main()
{
    std::string str;
    std::cin >> str;
    
    Fruit fr = convert(str);
    
    switch (fr) {

        case APPLE:
            cout << "This is an apple" << endl;
            break;
        case PEAR:
            break;
    }

    return 0;
}
	
Last edited on
Thank you.

Going to try.
Enum↔string conversions are a bit of an annoyance in C and C++ because they violate the One Definition Rule.

You can, however, use a simple macro trick to fix it (among a whole slew of other useful things).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

// Here's the macro trick ----------------------------------------------------

// First, define your list
#define FRUITS(F) \
  F(Apple),      \
  F(Pear),       \
  F(Banana),     \
  F(Strawberry)

// Create the Enums
#define F(e) e
enum Fruit { FRUITS(F), NumFruits };
#undef F

// Create the strings
#define F(s) #s
std::string FruitNames[] = { FRUITS(F) };
#undef F

// Here are a couple of conversion functions ----------------------------------
// just like Gamer2015 suggested

std::string totitle( std::string s )
{
  for (char& c : s)   c = std::tolower( c );
  for (char& c : s) { c = std::toupper( c ); break; }
  return s;
}

Fruit name_to_fruit( std::string s )
{
  return Fruit( std::find( FruitNames, FruitNames + NumFruits, totitle( s ) ) - FruitNames );
}

std::string fruit_to_name( Fruit fruit )
{
  return (fruit < NumFruits) ? FruitNames[ fruit ] : "";
}

// Let's have some fun --------------------------------------------------------

int main()
{
  std::string users_fruit;
  std::cout << "What is the name of your favorite fruit? ";
  std::getline( std::cin, users_fruit );
  
  switch (name_to_fruit( users_fruit ))
  {
    case Apple:
      std::cout << "Sorry, I don't much care for an Apple.\n";
      break;
      
    case Strawberry: 
      std::cout << "Me too! I love Strawberries!\n";
      std::cout << "(Especially with cereals like Golden Grahams.)\n";
      break;
      
    case NumFruits:
      std::cout << "I've never heard of a \"" << users_fruit << "\".\n";
      break;
      
    default:
      std::cout << "Yeah, " << fruit_to_name(name_to_fruit(users_fruit)) << "s are okay.\n";
      break;
  }
}

You might want to add some other validations to the user's input as well, such as trimming it, etc.

(I used Title Case. You can stick with ALL CAPS if you wish, just adjust line 37 to use a function that adjusts the string s appropriately.)

Enjoy!
thank you for the usefull tips
Topic archived. No new replies allowed.