Thread: While statement

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mentallic
    I'd agree with you, but it's the best I can do given our professor is very picky about magic numbers.
    In this case 0 is not a magic number that should be replaced with a named constant because it is well known that return value of strcmp is to be compared with respect to 0. Instead, EQUAL is a "magic constant". For example, strcmp(x, y) > 0 is a test for whether x is > y: the ">" indicates the intended comparison. strcmp(x, y) > EQUAL is a test for whether... huh? As such, if I were your professor, I would mark you down for using EQUAL. And no, ZERO does no better as it does not provide any additional information, and it is extremely implausible (and misleading if it were so) that ZERO might be defined to something else. In this case, 0 is correct.

    Quote Originally Posted by Mentallic
    My removeNewLine function doesn't work in this case
    Then change it to return a char*, or break up your expressions into different statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by laserlight View Post
    In this case 0 is not a magic number that should be replaced with a named constant because it is well known that return value of strcmp is to be compared with respect to 0. Instead, EQUAL is a "magic constant". For example, strcmp(x, y) > 0 is a test for whether x is > y: the ">" indicates the intended comparison. strcmp(x, y) > EQUAL is a test for whether... huh? As such, if I were your professor, I would mark you down for using EQUAL. And no, ZERO does no better as it does not provide any additional information, and it is extremely implausible (and misleading if it were so) that ZERO might be defined to something else. In this case, 0 is correct.
    I've caught him on numerous occassions writing out his code in a very unorthodox manner (at least with respect to our style guide), and he has thrown #define's all over the place. When I saw him
    #define x "x"
    #define y "y"
    because we were going to do a few strcmp's in the program, I think it was at this point I felt like I needed to define basically everything. But I haven't seen him explicitly #define the return value of strcmp and you do make a good point, so I'll change it.


    Quote Originally Posted by laserlight View Post
    Then change it to return a char*, or break up your expressions into different statements.
    I'd have thought of that if I didn't already run into the mess between pointers and arrays in a previous thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statement being ignored
    By Nathaneil123 in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2010, 07:48 PM
  2. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  3. why do I get this statement?
    By Jasper in forum C Programming
    Replies: 5
    Last Post: 09-16-2009, 05:52 AM
  4. Unless statement
    By zoupi in forum C Programming
    Replies: 2
    Last Post: 04-27-2009, 02:33 AM
  5. if statement
    By Joe100 in forum Game Programming
    Replies: 1
    Last Post: 12-07-2003, 05:57 AM