Thread: Skipping if statement

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    10

    Skipping if statement

    My code seems to skip an if statement. The function code is here:
    Code:
    while(flag == 0)
        {
            if(strcmp(current_plane_fuel->name, temp_fuel->name)==0)
            {
                flag == 1;
            }
            temp_fuel = temp_fuel->next_fuel;
            
            if(temp_fuel == NULL && flag == 0)
            {
                flag = 1;
                check_result = 4;
            }                    
        }
    It basically checks 2 char arrays and if they are the same breaks the while loop.

    temp_fuel being a linked list of structs that hold a name.

    It's just checking if the fuel exists.

    If they are different it breaks the loop and sets the int "check_result" to 4.

    Even when I give it words that are the same it gives me the result of 4. And when I debug it, I check both values and they are the same. As I step through I see it skip over the whole if statement.

    It doesn't even place the debuggers current line on the "if(strcmp...etc" line.

    I can post the function in more detail if it helps, but I can't see why it won't even check the first if statement.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by FallenBlade View Post
    temp_fuel being a linked list of structs that hold a name.
    so temp_fuel may != NULL then.

    Even when I give it words that are the same it gives me the result of 4.
    If it skips the if statement, how or why would the result be 4? What should it be?

    I absolutely promise you this is because one of the variables is not what you think it is. There is no other possible explanation.

    Generally I use brackets around individual truth tests:
    Code:
    if((temp_fuel == NULL) && (flag == 0))
    But that should not matter here.
    Last edited by MK27; 12-13-2009 at 03:25 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    so temp_fuel may != NULL then.
    Sorry I should have worded that better.
    temp_fuel holds a name and a link to the next fuel

    In my loop I set temp_fuel to the next fuel. If there is no next fuel it will equal NULL.
    This works. And by stepping through it, I can see it does indeed go through all of my fuels until it hits NULL, where it sets check_result to 4 and then ends.

    The problem is the correct fuel is in the list. And when stepping through it, I can see that the 2 char arrays held in name are in fact equal. Yet it still skips the first if statement.

    My problem is the first if statement. The second one works fine.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Shouldn't the first codeblock be

    Code:
    flag = 1;
    Right now it's

    Code:
    flag == 1;
    As far as I can tell it does nothing if the condition in the if statement is true.
    Last edited by Subsonics; 12-13-2009 at 03:32 PM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300


    I have an old keyboard with red marker at the top:

    ==
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    Quote Originally Posted by Subsonics View Post
    Shouldn't the first codeblock be

    Code:
    flag = 1;
    Right now it's

    Code:
    flag == 1;
    As far as I can tell it does nothing if the condition in the if statement is true.
    Yes! Sorry. It has fixed it!

    I can't believe I've been staring at this code for around half an hour now and had friends look at it, and no one's noticed. I think I've gone code blind.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's often the little things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner question about the Return statement
    By lucidrave in forum C Programming
    Replies: 3
    Last Post: 08-07-2009, 05:19 PM
  2. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  3. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM