Thread: enum values not working in #define?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,336
    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
    463
    You have to define it
    Code:
    #define LOW 1
    Last edited by nimitzhunter; 01-15-2011 at 12:19 AM.
    "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,336
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  8. #8
    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.

  9. #9
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    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

  10. #10
    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.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  12. #12
    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.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MattJ812 View Post
    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.
    You're going to find that everywhere...

    Self-publishing has so thoroughly diluted the wisdom pool that you're actually more likely to get bad information than good, at least the first time around... The real trick is to tell them apart.

    A few years back, learning C I must have gone through about 5 books and tutorials looking for one that A) understood I was a beginner and B) wasn't chock full of mistakes. I eventually muddled through and got reasonably good at it. If you read the other threads here you'll see I just went through it again with C++ ... I was blocked on the whole OOP thing; no matter what I did it simply would not sink in. The tutorials and books (including C++ in 21 Days) did little more than deepen the confusion. Asking questions (other than here) more often made it worse than better...

    Just a few days ago I pushed the point here and got a flood of extremely helpful discussion and a reference to "Thinking In C++". Between the two it's finally sinking in.... Or, pehaps I should say it "seems to be" sinking in, I've yet to put it to the test.

    I think you'll find your best plan is to get a decent tutorial on plain C and work through it to give yourself the fundimentals, do a couple of simple programs, then climb into the "Thinking in C++" books... Although they are two separate languages, there is enough similarity that knowing C helps with C++ ...

    Oddly the C in 21 days tutorial is pretty good...
    Teach Yourself C in 21 Days -- Table of Contents

    Thinking in C++ is here...
    Bruce Eckel's MindView, Inc: Thinking in C++ 2nd Edition by Bruce Eckel

  14. #14
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    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

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    All editions will have errata.

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, 10: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-22-2002, 12:30 AM