displaying enums

A

Aaron Toponce

Okay, maybe this is a dumb question, but here it goes: Consider the
following code:

enum day_of_week {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};

Now, we all know that enums positions are looked at as integers, how would
one go about displaying say "Mon" rather than "1"? For example consider
some more code:
#include <iostream>
#include "stdlib.h"
using namespace std;

enum day_of_week {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};

int main()
{
int iChoice;

cout << "Enter a number representing it's corresponding day of the week:
"; cin >> iChoice;
cout << "Your day is " << // what would I type here to access the
corresponding place in day_of_week?

system("PAUSE");
return 0;
}

Thanks for your help
 
J

John Carson

Aaron Toponce said:
Okay, maybe this is a dumb question, but here it goes: Consider the
following code:

enum day_of_week {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};

Now, we all know that enums positions are looked at as integers, how
would one go about displaying say "Mon" rather than "1"? For example
consider some more code:
#include <iostream>
#include "stdlib.h"
using namespace std;

enum day_of_week {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};

char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

int main()
{
int iChoice;

cout << "Enter a number representing it's corresponding day of the
week: "; cin >> iChoice;
cout << "Your day is " << // what would I type here to access the
corresponding place in day_of_week?

cout << "Your day is " << days[iChoice-1];

system("PAUSE");
return 0;
}

Thanks for your help

You are assuming agreement that Monday is the first day of the week, by the
way (some people would say that Sunday is).
 
S

Sam Holden

Okay, maybe this is a dumb question, but here it goes: Consider the
following code:

enum day_of_week {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};

Now, we all know that enums positions are looked at as integers, how would
one go about displaying say "Mon" rather than "1"? For example consider
some more code:

You wouldn't.

Enums don't provide strings. Use an array of strings or something instead.
 
C

Cedric LEMAIRE

Aaron Toponce said:
Okay, maybe this is a dumb question, but here it goes: Consider the
following code:

enum day_of_week {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};

Now, we all know that enums positions are looked at as integers, how would
one go about displaying say "Mon" rather than "1"? For example consider
some more code:
#include <iostream>
#include "stdlib.h"
using namespace std;

enum day_of_week {Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun};

int main()
{
int iChoice;

cout << "Enter a number representing it's corresponding day of the week:
"; cin >> iChoice;
cout << "Your day is " << // what would I type here to access the
corresponding place in day_of_week?

system("PAUSE");
return 0;
}
It exists a solution that you'll obtain searching with keywords
"C++ enum to string" into Google groups (sorry, I don't know how to have
the URL of the answer), a posting on june 16 2003.

It proposes you writing someting like:
//##markup##"enum DAY_OF_WEEK"
//##data##
//Mon
//Tue
//Wef
//Thu
//Fri
//Sat
//Sun
//##data##

and a code generator easy-to-use writes both the enum definition and the
function 'std::string DAY_OF_WEEKToString(DAY_OF_WEEK e)' for you in your
code.

Note that a small improvement should be brought to take into account the
assignment of a value to the enum.

-- Cedric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top