• Advertisement

Archived

This topic is now archived and is closed to further replies.

enum types - outputting the label

This topic is 6215 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!"

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Guest 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest 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;

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:

&int 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.

Share this post


Link to post
Share on other sites
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!"

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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:

&int 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 )



Share this post


Link to post
Share on other sites
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:

&int 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

Share this post


Link to post
Share on other sites
quote:
Original post by Gorg

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




Actually, that would make things less clear. When you use ''int *i'' you know that the * goes with the variable name. Using ''int* i'' sounds like all proceeding variables will be of type int*, which isn''t true. IE, ''int* i, j;'' creates a pointer to an integer (i) and a straight integer (j) even though the syntax would lead you to believe otherwise.

BTW, the software companies I''ve worked for have never used the ''int*'' standard. Of course, I''ve only worked for a few so...


- Houdini

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Houdini

[quote]Original post by Gorg

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




Actually, that would make things less clear. When you use ''int *i'' you know that the * goes with the variable name. Using ''int* i'' sounds like all proceeding variables will be of type int*, which isn''t true. IE, ''int* i, j;'' creates a pointer to an integer (i) and a straight integer (j) even though the syntax would lead you to believe otherwise.

BTW, the software companies I''ve worked for have never used the ''int*'' standard. Of course, I''ve only worked for a few so...


- Houdini

Yes, but int* i; tells you that the type is int. someone could have written

int *fajbllejl;aj;

reading rapidly, there is a chance that he misses the * and do not know it is a pointer.

But this is useless fight. It doesn''t really matter which notation you use, as long as everybody in your teams follows the same one.

Share this post


Link to post
Share on other sites

  • Advertisement