×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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  

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

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

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

(OP)
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....

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

Are you using managed code? If so what version?

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

cheers

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

(OP)
We are using Visual Studio 2008, and I believe we are using managed code.

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

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;
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close