how to print enum field literals

This is a discussion on how to print enum field literals within the C++ Programming forums, part of the General Programming Boards category; hey, I have a enum with many items, using cout, I only get the integers print out. what I want ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    how to print enum field literals

    hey, I have a enum with many items, using cout, I only get the integers print out. what I want is the meaningful words in the enum. what is the idiom for this task?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    You can do that in Java because enums are class objects there, but enums in C/C++ are just souped up const ints, so if you print it, all you're going to get is a number. You'll have to write your own functions to print the names.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    use strcpy

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by mrsirpoopsalot View Post
    use strcpy
    this is C++ we are talking about. Wake up, it has std::string type that can be manipulted without old C-routines
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Printing enums as text requires that you translate the enum value into a string - and the compiler can not do that for you, which is pretty much the same as what cunnus88 said.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    so this wont work?

    Code:
    enum Category { 
    unknown = -1, meat, poultry, 
    seafood, dairy, vegetable, fruit, 
    grain, sweet, nCategory
    };
    Code:
    void convertCategory(Category category, char categoryDescription[])
    {
     
    switch (category)
    {
    case meat: strcpy(categoryDescription,  "meat");
    break;
    case poultry: strcpy(categoryDescription,  "poultry");
    break;

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You could do that or we could make a C++ approach to the problem.
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    enum enWeapon
    {
    	eWeaponSword = 0,
    	eWeaponKnife = 1,
    	eWeaponStick = 2
    };
    
    std::map<enWeapon, std::string> weaponMap;
    
    void InitializeMap()
    {
    	weaponMap[eWeaponSword] = "Sword";
    	weaponMap[eWeaponKnife] = "Knife";
    	weaponMap[eWeaponStick] = "Stick";
    }
    
    int main()
    {
    	InitializeMap();
    
    	std::cout<<weaponMap[eWeaponStick]<<std::endl;
    	std::cout<<weaponMap[eWeaponSword]<<std::endl;
    	std::cin.get();
    
    	return 0;
    }
    Woop?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by mrsirpoopsalot View Post
    so this wont work?
    It will work, but it is a C solution while this is C++.
    Therefore, you should use std::string instead of the old char.
    Or better yet, a map.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    I'd create a function and have the map be static variable, and make it initialize on the first function call. For more complex situations, where the map is needed by multiple functions, I'd create a class to do all things associated with that enum, including the enum itself.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 04:03 PM
  2. how do you print a typedef enum?
    By -EquinoX- in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 08:50 PM
  3. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  4. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM
  5. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21