While statement

This is a discussion on While statement within the C Programming forums, part of the General Programming Boards category; What I want to do is to go into a while loop that uses fgets, and has the criteria that ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    While statement

    What I want to do is to go into a while loop that uses fgets, and has the criteria that the string read by fgets isn't NULL or the fullstop character "."

    What I have is

    Code:
    #define LINELENGTH 100
    #define EQUAL 0 
    while ((retval = fgets(buf, LINELENGTH, stdin)) != NULL
                        && strcmp(retval, ".") != EQUAL) {
                }
    Which isn't working. I would've though retval is given its value when fgets is called, so then it will be successfully implemented it into the strcmp, but it doesn't seem so.
    All it does at the moment is keep asking me for more strings (even if I enter the fullstop character) and once I press ctrl-D it seg faults.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    I think you wanted to strcmp(buf, ".") rather than retval.

    EDIT:
    Oh wait, that's dumb: retval is fine. Actually, it looks like your code is correct.

    By the way, I suggest that you just write 0 instead of EQUAL, since EQUAL makes no sense here.
    Last edited by laserlight; 10-12-2012 at 06:44 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by laserlight View Post
    I think you wanted to strcmp(buf, ".") rather than retval.

    EDIT:
    Oh wait, that's dumb: retval is fine. Actually, it looks like your code is correct.
    Actually no, you're right! I realized the problem now is the same problem I was having a little while back, fgets is including \n into buf so the strcmp isn't breaking the loop because of that.

    Quote Originally Posted by laserlight View Post
    By the way, I suggest that you just write 0 instead of EQUAL, since EQUAL makes no sense here.
    I'd agree with you, but it's the best I can do given our professor is very picky about magic numbers.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    My removeNewLine function doesn't work in this case

    Code:
    while ((retval = fgets(buf, LINELENGTH, stdin)) != NULL
                        && strcmp(removeNewLine(buf), ".") != EQUAL) {
    }
    
    void removeNewLine(char *command) {
        int length = strlen(command);
        
        if (length > 1 && command[length-1] == '\n') {
            command[length-1]='\0';
        }
    }
    Guess I'm gonna have to include that newline character in the strcmp.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    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, 06:52 AM
  4. Unless statement
    By zoupi in forum C Programming
    Replies: 2
    Last Post: 04-27-2009, 03:33 AM
  5. if statement
    By Joe100 in forum Game Programming
    Replies: 1
    Last Post: 12-07-2003, 05:57 AM

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