Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > displaying enums

Reply
Thread Tools

displaying enums

 
 
Aaron Toponce
Guest
Posts: n/a
 
      06-26-2003
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
--
"Black holes are where God divided by zero." -Steven Wright


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      06-26-2003
"Aaron Toponce" <(E-Mail Removed)> wrote in message
news:EdtKa.19445$R73.3886@sccrnsc04
> 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
> --
> "Black holes are where God divided by zero." -Steven Wright


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

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      06-26-2003
On Thu, 26 Jun 2003 02:52:21 GMT, Aaron Toponce <(E-Mail Removed)> wrote:
> 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.

--
Sam Holden

 
Reply With Quote
 
Cedric LEMAIRE
Guest
Posts: n/a
 
      06-27-2003
"Aaron Toponce" <(E-Mail Removed)> wrote in message news:<EdtKa.19445$R73.3886@sccrnsc04>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enums without identifier, enums and typedef =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= C Programming 10 01-20-2007 01:20 AM
Storing Enums in web.config Matt ASP .Net 2 06-07-2005 01:45 PM
Basic question about enums Hans De Schrijver ASP .Net 1 06-05-2004 11:47 AM
enums martin ASP .Net 2 05-28-2004 04:48 PM
Loop through enums Syd ASP .Net 1 08-28-2003 09:49 PM



Advertisments