473,657 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

displaying enums

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
Jul 19 '05 #1
3 8956
"Aaron Toponce" <to*******@yaho o.com> wrote in message
news:EdtKa.1944 5$R73.3886@sccr nsc04
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)
Jul 19 '05 #2
On Thu, 26 Jun 2003 02:52:21 GMT, Aaron Toponce <to*******@yaho o.com> 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

Jul 19 '05 #3
"Aaron Toponce" <to*******@yaho o.com> wrote in message news:<EdtKa.194 45$R73.3886@scc rnsc04>...
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_WEEKToSt ring(DAY_OF_WEE K 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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
9544
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
2
2072
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the Enums 'Car' and 'House Public Class Fo Public Enum Ca For Chevrole Toyot
27
2724
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template <typename Enum> class smart_enum { public: typedef Enum enum_type;
2
1790
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I have them in structs for now, I thought I'd ask. Would IntPtr Enums push the bounds of a stupid question? Regarding a post a bit further down by Tom; ' What would you like to change in C# and .NET?'
4
5588
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and when looking at the db we can't tell what the value in a field represents. The other problem is that our enums are currently all stored in a single class, which means that because of no visibility constraints the enums are often used out of context...
2
1989
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't seem to find an appropriate Reflection method to access Enums within a class Thanks! --- Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/
2
2877
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default, g++ seems to create enums of 4 bytes in length, unless I use the -fshort-enums option. I don't much want to use this all or nothing approach in case it breaks library code etc, and there's no guarantee that other compilers have a comparable...
11
12484
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course, I can cast a MyEnum instance down to the int / short whatever (since int implements IEquatable<int>), but I don't like doing that, as it feels a bit messy, and I am then propegating the things that know what the base represenation is...
4
2992
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this "properly" yet "ugly" is to test the state for each state. I know that enumes are not ordered but since they are "stored" as numbers they have an inherent ordering which can be used. I don't really care about the ordering though but just the ability to...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.