Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ - getting the integer enum value from a string

Status
Not open for further replies.

jadixon

Programmer
May 22, 2008
33
US
I have an enumerated type such as

enum Colors(Red, Blue, Green);

and a CString test_string = "Blue"

How do I get to the 2nd value in the enumerated type by the string?

What I am trying to get to is:

int retValue = Colors(test_string);

or something like that that will produce retValue = 1
 
Enumeration and text values (actually char arrays) are different. No enum <=> char array conversions in C and C++. You must present your own code "to convert" text to enum, for example (the simplest way):
Code:
bool textToColor(const CString& text, Colors& color)
{
  bool res = true;
  if (text == "Blue")
     color = Blue;
  else if (text == "Green")
     color = Green;
  ...
  else
     res = false;
  return res;
}
There are tons of other codes to do that (based on the same principle)...
 
I have over 100 values in the enumeration, so using ifs or case statements is not practical.

I will have to rethink my code to find a better way to get the desired result.

It's a shame C can't handle things like that....
 
Are you using managed code? If so what version?

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
We are using Visual Studio 2008, and I believe we are using managed code.
 
You could always do it the good ol' C way with macros. First the header file: colour.h. Note no ifndefs
Code:
COLOUR(red)
COLOUR(green)
COLOUR(blue)
Then the header file colourenum.h that all files use
Code:
#ifndef ColourEnum_h
#define ColourEnum_h
enum ColourEnum
{
#define COLOUR(x) x,
#include "colour.h"
#undef COLOUR
   ColourMax
};
#endif
In the code where you wish to print the colour
Code:
#include "colourenum.h"
...
const char* colourstr[] =
{
#define COLOUR(x) #x,
#include "colour.h"
#undef COLOUR
   ""
};
...
bool textToColor(const CString& in_text, ColourEnum& out_colour)
{
   bool res = false;
   out_colour = ColourMax;
   for (int ii = 0; ii < ColourMax; ++ii)
   {
      if (in_text == colourstr[ii])
      {
         out_colour = (ColourEnum)ii;
         res = true;
         break;
      }
   }
   return res;
}
Alternatively, add a map in the initialization and use STL
Code:
#include <map>
#include <string>
...
typedef std::map<std::string, ColourEnum> ColourMap;

ColourMap colours;
void ColourMapInit ()
{
   for (int ii = 0; ii < ColourMax; ++ii)
      colours[std::string(colourstr[ii])] = (ColourEnum)ii;
}
...
bool textToColor(const std::string& in_text, ColourEnum& out_colour)
{
   bool res = false;
   ColourMap::iterator iter = colours.find (in_text);
   if (iter != colours.end ())
   {
      out_colour = iter->second;
      res = true;
   }
   return res;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top