Thread: What's wrong with this IF-statement?

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    What's wrong with this IF-statement?

    Code:
    if(cat.GetInfo() = 0)
    {
    	return 0;
    }
    Is it because cat.GetInto is in a class? I've never read about this before...

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    It's supposed to be:
    cat.GetInfo() == 0.
    == is comparison operator
    = is assignment operator

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Damn!

    That's right! Math got to be again. Stupid me (again!). Thanks.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    13
    Hehe, I abhor those types of errors, so simple yet alter the program totally.

  5. #5
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Too bad the compiler doesn't have an option to automatically fix them.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    Actually, if you use gcc with the -Wall option, it will issue warnings for this:

    Code:
    int main()
    {
      int i = 0;
    
      if(i = 50)
        ;
    
      return 0;
    }

    $ g++ -Wall test.cpp
    test.cpp: In function `int main()':
    test.cpp:6: warning: suggest parentheses around assignment used as truth value

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Sometimes you'll find people reverse the operands so that the compiler catches it, for instance in this case:
    Code:
    if(0 == cat.GetInfo())
    {
        return 0;
    }
    compiles fine, whereas:

    Code:
    if(0 = cat.GetInfo())
    {
        return 0;
    }
    should throw up a compilation error. That said, the argument goes, if you can remember to reverse the operands, you can remember to check for correct use of the equality operator

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 6
    Last Post: 05-10-2008, 02:11 AM
  2. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 12:26 AM
  3. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  4. What's wrong with my success statement??
    By cool_dude07 in forum C Programming
    Replies: 7
    Last Post: 07-21-2007, 11:22 PM
  5. Something wrong with this if statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 05:19 PM