Skip to content
C++ Better Explained
Go back
C++ Enum to String: Convert Enums to Text (3 Ways)
Edit page

C++ Enum to String: Convert Enums to Text

C++ has no built-in way to turn an enum into a string, so you write a short conversion function — usually a switch that returns the matching text. Enums are stored as integers, so printing one directly gives a number, not the name. Here are three clean ways to get the text.


Why You Need This

Printing an enum shows its underlying integer, which is rarely what you want:

#include <iostream>

enum class Color { Red, Green, Blue };

int main() {
    Color c = Color::Green;
    std::cout << static_cast<int>(c) << "\n";  // prints 1, not "Green"
    return 0;
}

The compiler keeps only the numeric value (Red=0, Green=1, Blue=2); the names exist just for your source code. To display "Green", you map the value back to text.


Method 1: A switch Statement

The most readable approach for a handful of values is a switch that returns a string:

#include <iostream>
#include <string>

enum class Color { Red, Green, Blue };

std::string toString(Color c) {
    switch (c) {
        case Color::Red:   return "Red";
        case Color::Green: return "Green";
        case Color::Blue:  return "Blue";
        default:           return "Unknown";
    }
}

int main() {
    std::cout << toString(Color::Green) << "\n";  // Green
    return 0;
}

Each case returns the matching text. The default case is good insurance — if someone adds a new enum value later and forgets to update the function, you get "Unknown" instead of undefined behavior.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Method 2: An Array Lookup

If your enum values are sequential starting at 0 (the default), you can index an array of names directly:

#include <iostream>
#include <string>

enum Color { Red, Green, Blue, COLOR_COUNT };

const std::string colorNames[] = { "Red", "Green", "Blue" };

int main() {
    Color c = Green;
    std::cout << colorNames[c] << "\n";  // Green
    return 0;
}

Because a plain enum converts to its integer automatically, colorNames[c] works. This is compact, but it’s fragile: if the names array and the enum ever drift out of order, you’ll print the wrong text. Keep them right next to each other.


Method 3: A std::map

For larger or non-sequential enums, a std::map is the most maintainable choice:

#include <iostream>
#include <string>
#include <map>

enum class Status { Active, Paused, Stopped };

int main() {
    std::map<Status, std::string> names = {
        {Status::Active,  "Active"},
        {Status::Paused,  "Paused"},
        {Status::Stopped, "Stopped"}
    };

    std::cout << names[Status::Paused] << "\n";  // Paused
    return 0;
}

The map makes the value-to-name pairing explicit and easy to extend — just add a line. It’s slightly heavier than a switch, but for configuration-style enums that readability is worth it.


Which Method to Use

SituationBest method
A few valuesswitch
Sequential values, performance mattersarray lookup
Many or non-sequential valuesstd::map

For most beginner code, start with the switch — it’s clear, safe, and needs no extra container.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ Check if File Exists: 3 Reliable Ways (with Examples)
Next Post
C++ Measure Execution Time with std::chrono (Beginner Guide)