enum values not working in #define?

This is a discussion on enum values not working in #define? within the C++ Programming forums, part of the General Programming Boards category; So yet again I'm working through the teach yourself c++ in 21 days and I am stuck again. This is ...

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

    enum values not working in #define?

    So yet again I'm working through the teach yourself c++ in 21 days and I am stuck again.

    This is in regard to different debugging macro levels.

    Heres the example...

    Code:
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    enum LEVEL { NONE, LOW, MEDIUM, HIGH };
    const int FALSE = 0;
    const int TRUE = 1;
    typedef int BOOL;
    
    #define DEBUGLEVEL LOW
    
    #if DEBUGLEVEL < LOW
    #define ASSERT(x)
    #else
    #define ASSERT(x) \
        if (! (x)) \
        { \
            cout << "ERROR!! Assert " << #x << " failed\n"; \
            cout << " on line " << __LINE__ << "\n"; \
            cout << " in file " << __FILE__ << "\n"; \
        }
    #endif
    
    #if DEBUGLEVEL < MEDIUM
    #define EVAL(x)
    #else
    #define EVAL(x) \
        cout << #x << ":\t" << x << endl;
    
    #endif
    
    #if DEBUGLEVEL < HIGH
    #define PRINT(x)
    #else
    #define PRINT(x) \
         cout << x << endl;
    #endif
    So depending on what I set DEBUGLEVEL to be should decide what methods I want. As it stands the output always acts as if I'm using HIGH, but if I just use numbers such as...

    Code:
    #define DEBUGLEVEL 1
    
    #if DEBUGLEVEL < 1
    it works as expected.

    so whats up with the enum LEVEL stuff not working?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The preprocessor doesn't know anything about the language. For all it knows, HIGH is the same as SNORKEL -- it's just a bunch of letters.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    Its copied straight from the book, how do I fix it?

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    You have to define it
    Code:
    #define LOW 1
    Last edited by nimitzhunter; 01-14-2011 at 11:19 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    OK so why would the book try to use an enumeration if the preprocessor doesn't understand it?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MattJ812 View Post
    OK so why would the book try to use an enumeration if the preprocessor doesn't understand it?
    I'm not seeing this example in the book. Do you have a page number?

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    Using the PDF I'm on 543 of 772

    Code:
    any number of levels, a common system is to have four levels: HIGH, MEDIUM, LOW, and NONE.
    Listing 17.8 illustrates how this might be done, using the String and Animal classes from Listing
    17.6. The definitions of the class methods other than Invariants() have been left out to save
    space because they are unchanged from Listing 17.6.
    NOTE: To compile this code, copy lines 43-136 of Listing 17.6 between lines 64 and
    65 of this listing.
    Listing 17.8. Levels of debugging.
    0: enum LEVEL { NONE, LOW, MEDIUM, HIGH };
    1: const int FALSE = 0;
    2: const int TRUE = 1;
    3: typedef int BOOL;
    4:
    5: #define DEBUGLEVEL HIGH
    6:
    7: #include <iostream.h>
    8: #include <string.h>
    9:
    10:

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Hmm. Listing 17.8 in the copy I'm looking at is about cin.ignore(). Granted I can only look at the 5th ed. Perhaps they realized it was an atrocious idea and took it out. The #define part works just fine. But you won't be able to use < and > with it.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    OK so you're saying the enumeration works aslong as I don't test if the DEBUGLEVEL is less or greater than? Why is that?

    And you say you have the 5th edition? where do I get it? mine is 2nd i didn't download it very long ago.

  10. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    Quote Originally Posted by MattJ812 View Post
    OK so you're saying the enumeration works aslong as I don't test if the DEBUGLEVEL is less or greater than? Why is that?

    And you say you have the 5th edition? where do I get it? mine is 2nd i didn't download it very long ago.
    Because they are not valid types. So there are not the logical operations for them. You can go to the bookstore.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    Were they valid types when they wrote that?

    Im sorry I was under the assumption that this book was kept up online, it took long enough to figure out how to get a compiler to even start to learn from it.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My school has a subscription to Safari Online; many libraries do.

    #define just does text substitution. Sprinkling the word HIGH throughout your code is just fine, because your enum will turn that into a number. But the preprocessor does not know -- has no way of knowing or caring -- about that value, and as such any attempt to actually use the value is doomed to failure.

  13. #13
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    That makes you wonder why they wrote that code. Given that it's the second edition
    "All that we see or seem
    Is but a dream within a dream." - Poe

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    All editions will have errata.

  15. #15
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    I'm not in school I've been wanting to code games forever and learned a whole bunch about GML but I wanted to learn more about "proper" coding and c++ in 21 is what I found so here I am.

    Learn as I go I guess, hard to do when your learning materials are wrong heh.

    Thanks tho.

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

Similar Threads

  1. Changing flags from a DLL?
    By RobotGymnast in forum C++ Programming
    Replies: 17
    Last Post: 10-27-2008, 01:34 PM
  2. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  4. keyboard control
    By MathFan in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2002, 11:55 AM
  5. queued signals problem, C programming related
    By Unregistered in forum Linux Programming
    Replies: 3
    Last Post: 01-21-2002, 11:30 PM

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