Advertisement

enum types - outputting the label

Started by February 15, 2001 10:33 AM
10 comments, last by sutek 23 years, 11 months ago
mMonsterName is an enumerated type consisting of elements {human, orc, dragon} how do u output the label of an enumerated type''s elements instead of it''s enumeration value?.. ie, atm its printing "0 defeats 1", whereas i want "human defeats orc" printed out. I know i could do this using a case statement but that seems unnecessary for such a simple request ****************************** Peasant:>"Help Help Im being repressed!" King Arthur:>"Bloody Peasant!"
******************************Peasant:>"Help Help Im being repressed!"King Arthur:>"Bloody Peasant!"
first off, i think you are thinking of enumerations wrong. they are not strings, instead, they are like constants.
an enum is like this:

enum MyEnum { meValue1, meValue2, meValue3 };

but it could be done like this as well, and have the same effect.

typedef int MyEnum;
const MyEnum meValue1 = 0;
const MyEnum meValue2 = meValue1 + 1;
const MyEnum meValue3 = meValue2 + 1;

obviously, if these are exactly the same, then it would hold that you wouuld treate them the same. meValue1, meValue2, and meValue3 are all ints, not strings. So, when you print meValue1, then you get 0, not "meValue1". Does that make sense?
if you want to print "Monster defeats Orc", you are probably going to want to do something like this:

class Character
{
...
string myCharacterName;
...
};

And then do something like this:

cout << WinningCharacter.myCharacterName << " defeats " <<
LosingCharacter.myCharacterName << ". " << endl;

of course, that is terrible oop, but that''s a whole different post.

farmersckn
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
Advertisement
This is probably the easiest way to do it.

  enum Day{	Sunday = 0,	Monday,	Tuesday,	Wednesday,	Thursday,	Friday,	Saturday};char * lpDay[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};int main(int argc, char* argv[]){	Day today = Thursday;	printf(lpDay[today]);	return 0;}  
the more I read about C the less I like it. Enums are a great idea, but they should have gone all the way and made it so that when you print them they do show what they are, not just the int they are stored as internally.
quote: Original post by Anonymous Poster

the more I read about C the less I like it. Enums are a great idea, but they should have gone all the way and made it so that when you print them they do show what they are, not just the int they are stored as internally.


I hear what you''re saying, and yes it would be nice to have this. The problem though is that adding functionality like this would require C to become more like C++, and that won''t happen. The good news is you can do this in C++ with a little work.

- Shriek
oh don''t get me wrong, from what I''ve seen of C++ it is a horrible language. The syntax of both is all wrong. Ok get this, I want to declare an int, so I do this:

int i;

ok makes sense, it is of the form: type identifier; and that makes sense. Now what if I want to do an int pointer? Why is it this:

int *i;

That makes no sense, the variable that I am declaring is not an int, it is an int pointer, or an address to an integer. It would make more sense for the declaration to be:

∫ i;

likewise with arrays, it should be int[] i, not int i[]. Afterall, when I want to declare a char I can do this:

char firstInitial;

which makes sense, but if we take the same approach as above it becomes the strange:

char ''firstInitial'';

Likewise it is silly that they use the & for address, when it is also used for ANDing things, and they use * for dereference when it is also for multiplication. There is no good reason. I mean, the @ would be perfect for the dereference symbol.

Also the language should be updated every five years, with improving the language being a primary goal. Stuff like declaring functions ahead of time is pointless, java doesn''t require it, the compiler just checks around and figures it out for itself. I''m sure there are other things that could be improved but people have gotten used to them so they don''t care.
Advertisement
thanks ever so much jonstelly :D

and btw if u want to see an easy to read language, try ada - the code is so easy to read - i think thats why we were taught Ada at first year uni instead of a more practical language like c/c++ which is full of overloaded operations like & and *



******************************
Peasant:>"Help Help Im being repressed!"

King Arthur:>"Bloody Peasant!"
******************************Peasant:>"Help Help Im being repressed!"King Arthur:>"Bloody Peasant!"
quote: Original post by Anonymous Poster

the more I read about C the less I like it. Enums are a great idea, but they should have gone all the way and made it so that when you print them they do show what they are, not just the int they are stored as internally.


I think it''s set up like that because enums were designed just for internal data storage and not having their actual names represented.
--


WNDCLASSEX Reality;
...
...
Reality.lpfnWndProc=ComputerGames;
...
...
RegisterClassEx(&Reality);


Unable to register Reality...what''s wrong?
---------
Dan Upton
Lead Designer
WolfHeart Software
WNDCLASSEX Reality;......Reality.lpfnWndProc=ComputerGames;......RegisterClassEx(&Reality);Unable to register Reality...what's wrong?---------Dan Uptonhttp://0to1.orghttp://www20.brinkster.com/draqza
quote: Original post by Anonymous Poster

oh don''t get me wrong, from what I''ve seen of C++ it is a horrible language. The syntax of both is all wrong. Ok get this, I want to declare an int, so I do this:

int i;

ok makes sense, it is of the form: type identifier; and that makes sense. Now what if I want to do an int pointer? Why is it this:

int *i;


it can be
int* i; //integer pointer
also. If you ever work for a software company, the coding standard is usually that you use int* instead of int because it is clearer

quote:
That makes no sense, the variable that I am declaring is not an int, it is an int pointer, or an address to an integer. It would make more sense for the declaration to be:

∫ i;

& is the reference operator already. So it would not make sense.

int b;
int& i = b; //i is now an alias for b

quote:
likewise with arrays, it should be int[] i, not int i[]. Afterall, when I want to declare a char I can do this:


int[] i; is java syntax.

int i[]; is C/C++.

actually the syntax is right, int i[], is not array of integer,
it is a pointer, pointing to an array of data.

so int i[size] is equivalent to int* i = new int[size];
so the syntax is logical

quote:
char firstInitial;

which makes sense, but if we take the same approach as above it becomes the strange:

char ''firstInitial'';


What are you talking about. ''a'' mean character a; so
char firstInitial = ''a''; means that firstInitial is equal to character a;

quote:
Likewise it is silly that they use the & for address, when it is also used for ANDing things, and they use * for dereference when it is also for multiplication. There is no good reason. I mean, the @ would be perfect for the dereference symbol.


you are right different symbols for different actions makes things clear, but remember that C was made to work on every machine. When C came out, not all machine were supporting the whole 256 ASCII charset. So @ was not a choice!! C++ was made to backward compatible with C, so they had the use the same symbols.

It is actually not hard at all to get use to.

quote:
Also the language should be updated every five years, with improving the language being a primary goal. Stuff like declaring functions ahead of time is pointless, java doesn''t require it, the compiler just checks around and figures it out for itself. I''m sure there are other things that could be improved but people have gotten used to them so they don''t care.


This one is not as simple. Again, C++ get this(you are right, it''s a bad thing) from C.

Java do a 2 pass compilation. But C/C++ creates compilation Unit which are totally independant. So you need the whole ugly system of header files and forward declarations. But, there is nothing that stops a compiler writer to write a 2 pass compilation algorithm and make C++ compile like Java. But it wouldn''t be portable, so I never seen it done.

Though there is one advantage I can see with that approach is that you can split the functions of the declarations from the implementation, but we good documentation this doesn''t matter.

This is something that I would like to see changing.

They actually update the standard for C++ every 5 years. C is not being developed anymore. The first C++ standard was in 1998. The next one is planned in the 2005(5 years )



quote: Original post by Anonymous Poster

oh don't get me wrong, from what I've seen of C++ it is a horrible language. The syntax of both is all wrong. Ok get this, I want to declare an int, so I do this:

int i;

ok makes sense, it is of the form: type identifier; and that makes sense. Now what if I want to do an int pointer? Why is it this:

int *i;


it can be
int* i; //integer pointer
also. If you ever work for a software company, the coding standard is usually that you use int* instead of int because it is clearer

quote:
That makes no sense, the variable that I am declaring is not an int, it is an int pointer, or an address to an integer. It would make more sense for the declaration to be:

∫ i;

& is the reference operator already. So it would not make sense.

int b;
int& i = b; //i is now an alias for b

quote:
likewise with arrays, it should be int[] i, not int i[]. Afterall, when I want to declare a char I can do this:


int[] i; is java syntax.

int i[]; is C/C++.

actually the syntax is right, int i[], is not array of integer,
it is a pointer, pointing to an array of data.

so int i[size] is equivalent to int* i = new int[size];
so the syntax is logical

quote:
char firstInitial;

which makes sense, but if we take the same approach as above it becomes the strange:

char 'firstInitial';


What are you talking about. 'a' mean character a; so
char firstInitial = 'a'; means that firstInitial is equal to character a;

quote:
Likewise it is silly that they use the & for address, when it is also used for ANDing things, and they use * for dereference when it is also for multiplication. There is no good reason. I mean, the @ would be perfect for the dereference symbol.


you are right different symbols for different actions makes things clear, but remember that C was made to work on every machine. When C came out, not all machine were supporting the whole 256 ASCII charset. So @ was not a choice!! C++ was made to backward compatible with C, so they had the use the same symbols.

It is actually not hard at all to get use to.

quote:
Also the language should be updated every five years, with improving the language being a primary goal. Stuff like declaring functions ahead of time is pointless, java doesn't require it, the compiler just checks around and figures it out for itself. I'm sure there are other things that could be improved but people have gotten used to them so they don't care.


This one is not as simple. Again, C++ get this(you are right, it's a bad thing) from C.

Java do a 2 pass compilation. But C/C++ creates compilation Unit which are totally independant. So you need the whole ugly system of header files and forward declarations. But, there is nothing that stops a compiler writer to write a 2 pass compilation algorithm and make C++ compile like Java. But it wouldn't be portable, so I never seen it done.

Though there is one advantage I can see with that approach is that you can split the functions of the declarations from the implementation, but we good documentation this doesn't matter.

This is something that I would like to see changing.

They actually update the standard for C++ every 5 years. C is not being developed anymore. The first C++ standard was in 1998. The next one is planned in the 2003(5 years )

Sorry for the anonymous redundant post




Edited by - Gorg on February 16, 2001 9:55:04 AM

This topic is closed to new replies.

Advertisement