Convert enum to string in C++

Enum to String in C++

There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the efficient and easy ways to convert enum to string.

enum is a user-defined data type that contains integral constants. We use the keyword ‘enum’ to define it.
Example:

enum languages {Java, CPP, Python, Ruby};

Here the name of the enum is languages and {Java, CPP, Python, Ruby} are values of type enum. By default, Java is 0, CPP is 1 and so on. The default values can also be changed at the time of declaration like this:

enum languages
{   Java = 0,
    CPP=3,
    Python=8,
    Ruby=9
};

Let’s check the ways to convert enum to String in C++:

Using stringify macro method to convert enum to String in C++

It is a better way to convert an enum and it works in most of the cases. stringify() macro can be used to convert text in the code into a string. In this method, no variable dereferencing or macro substitutions are not required.
Note: This method can convert only the exact text which is mentioned inside the parentheses.
For example:

int main(){
cout << stringify (  Java2Blog  ) << endl; //Here all the leading and trailing whitespaces are ignored)
}

Code

#include
#define stringify( name ) # name
using namespace std;

enum languages
  {
  java = 0,
  python,
  cpp,
  Ruby
  };

//converting enum elements to string with 'stringify()'
const char* convert_enum[] = 
  {
  stringify( java ),
  stringify( python ),
  stringify( cpp ),
  stringify( Ruby )
  };

//function to print elements inside the enum
void printelement( languages element )
  {
  cout << convert_enum[ element ] << endl;
  }

int main()
  {
  cout << ”Elements in the enum are: “ << endl;
  printelement((languages)0);
  printelement((languages)1);
  printelement((languages)2);
  printelement((languages)3);
  return 0;
    }

Output

Elements in the enum are:
java
python
cpp
Ruby

Using const char* Array to convert enum to String in C++

It is the simplest way to convert an enum. In this method we will use the default values of enum to access the elements in the string array. This method can also be called the most naive method.
Code

#include 
#include 
using namespace std;

enum languages { Java, Python, Ruby, CPP } ;
static const char *enum_str[] =
        { "Java", "Python", "Ruby", "C++" };

int main(){
    cout << enum_str[Java] << endl;
    cout << enum_str[Python] << endl;
    cout << enum_str[Ruby] << endl;
    cout << enum_str[CPP] << endl;
    return 0;
}

Output

Java
Python
Ruby
C++

Using a custom-defined function to convert enum to String in C++

Code

#include 
#include 
using namespace std;

enum languages { Java, Python, Ruby, CPP } ;
static const char *enum_str[] =
        { "Java", "Python", "Ruby", "C++" };

//custom-defined function
string convert_enum (int value)
{
    string str(enum_str[value]);
    return str;
}
int main(){
    cout<< convert_enum(Java) << endl;
    cout<< convert_enum(Python) << endl;
    cout<< convert_enum(Ruby) << endl;
    cout<< convert_enum(CPP) << endl;
    return 0;
}

Output

Java
Python
Ruby
C++

Conclusion

We discussed different methods to convert enum to string. First method has the capability to handle big enums as well and other methods can be used for a simple conversion. Third party libraries can also be used to convert an enum to a string like this.
Happy Learning!!

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.