printing enums

This is a discussion on printing enums within the C++ Programming forums, part of the General Programming Boards category; i was working through the tutorial http://www.cprogramming.com/tutorial/enum.html this tutorial is great, but I have a question on it. it doesn't ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    printing enums

    i was working through the tutorial
    http://www.cprogramming.com/tutorial/enum.html
    this tutorial is great, but I have a question on it.
    it doesn't address the printing of enums.
    it says that you have to provide special handling to have it print anything other then the integer value that is stored. Does this mean i have to test for the integer value and then print the enum value? is there no other way to have the value print with out testing?

    ie.
    Code:
    BinaryBit_t BitValue = off_bit
     
    if (BitValue == 0)
    	cout<<"off_bit";
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Yes. If you print an enum, you will just see it's corresponding numeric value. If you want to print something of value for an enum, you must check what the enum is and print something manually. You could, of course, overload the insertion operator for your enum type so it will always print a meaningful value on an output stream. For example:
    Code:
    #include <iostream>
    using namespace std;
    
    enum value_type { VT_NONE, VT_ON, VT_OFF };
    
    ostream& operator<<(ostream &out, const value_type &vt) {
      switch (vt) {
        case VT_NONE: return out << "none";
        case VT_ON:   return out << "on";
        case VT_OFF:  return out << "off";
      }
      return out;
    }
    
    int main() {
      value_type vt = VT_ON;
      cout << vt << endl;
      
      return 0;
    }
    Quote Originally Posted by output
    on

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    What does the apparent acronym FWIW represent?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    OTI. TFTI.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If you use the names frequently, then storing the values in a std::map may be of use to you.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    With an enum that is consecutive a simple switch would probably be best, and an array would be more appropriate than a map.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An array would have to assume that all enums follow consecutively. Remember, with enums this is perfectly fine:
    Code:
    enum foo { bar = -29, baz = 34, bif, pow, kersmash, holyenumerationsbatman = 1000 };



    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You are correct.

    >> With an enum that is consecutive a simple switch would probably be best, and an array would be more appropriate than a map.

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Not necessarily:
    Code:
    enum foo { bar = 1, bat, etc }; // I don't have Quzah's talent for enum names...
    is nice and consecutive.

    *edit*
    Besides, chances are most of the code making use of the enums will not depend directly on their integral values. So, if something came along and you did need to change the values of the enums at a later date (for whatever reason), you'd break the array (and if the code is old/long/obfuscated enough, you might not find it). You wouldn't break the map.

    Of course, I wouldn't really use this method unless stringified versions of the enum values' names came up a alot. (And, at any rate, I tend not to use enums very much.)

    Cheers
    Last edited by Zach L.; 08-08-2005 at 06:23 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    A switch would still be preferable to a map. It would not break, and would avoid logarithmic time lookups and extra storage in favor of constant time lookup. To make an array work with consecutive enum values that don't start at 0 would be as simple as adding an offset.

    Only if the strings were variable would I consider using a map, and then an unordered_map (hash_map) would be preferable if you had one available.

  13. #13
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    another question i have is can a predefined enum value be an equation?

    i.e.
    Code:
    enum BinaryBit_t {off_bit=a+b,on_bit=a-b};
    my compiler does not like this, but i was wondering if there is another way to do this?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I suppose that depends on what a and b are.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    *this
    Join Date
    Mar 2005
    Posts
    498
    I'm totaly saying this out of nowhere, but are enums constant? Can't they not be changed, that would explain why your compiler won't let you do that.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  4. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 07:10 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 03:46 AM

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