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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

enums and output

Status
Not open for further replies.

hunternjb

Programmer
Mar 21, 2001
7
0
0
US
I want to use an enum type and print out the constant rather than the integer value. Here is my enum type

enum Color { black=0, red, orange, yellow, green,
blue, indigo, violet, white};

Color color;

color = red;

cout << color; This would give me the value 1.

How do I get &quot;red&quot; printed to the screen instead of the 1?
 
This is C++, not Ada, so you do not have
String S = Color'Image(color);

You can do it by preparing a static array of strings that
corresponds to the string values of the enum type:
string S[] = {&quot;Black&quot;,&quot;Red&quot;,&quot;Orange&quot;...};
and printing:
cout << S[color];

This can, of course, be put into a class that hides the details, plus you can use an STL map to get better hiding
of the fact that enums are in fact just int's...
======
SeekerOfKnowledge
======
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top