Dangling Else error?

This is a discussion on Dangling Else error? within the C++ Programming forums, part of the General Programming Boards category; I'm supposed to be able to describe a 'dangling else' error for a quiz, yet I've never seen this in ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    26

    Dangling Else error?

    I'm supposed to be able to describe a 'dangling else' error for a quiz, yet I've never seen this in any of my books, and I'm transferring into this uni so I don't know what others have covered - can someone explain this to me? I assume it has something to do with how you use if and else (duh), but I'm supposed to be able to give an example on this quiz.

  2. #2
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    i don't know, but my guess would be an else without an if. but don't put that.
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  3. #3
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    The "Dangling Else":

    Code:
    if(x>y)
          if(x>8)
                 x++;
          else
                 x--;
    The problem here is that the 'else' could apply to either 'if'. So basically the dangling else occurs when using nested if.. else statements without {}'s as the following would have no confusion:

    Code:
    if(x>y)
    {
          if(x>8)
                 x++;
          else
                 x--;
    }
    Hope that make's sense.

    p.s. I didn't know this before now, just Googled +"Dangling else" +"C++"
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    example of a dangling else error:

    Code:
    if( x==1 )
       if ( y>2 )
          cout<< "foo" << endl;
    
    else
        cout<< "bar" << endl;
    also, http://www.google.com/search?hl=en&i...=Google+Search

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    lol, beat you to it axon
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Makes sense. I've never had a problem understanding an If statement though, and usually whenever I do an if / else I make sure I use {} brackets.

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    yeah....damnit, should've dropped the sandwitch and typed with both hands...hehe; but my google search is priceless.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Quote Originally Posted by axon
    yeah....damnit, should've dropped the sandwitch and typed with both hands...hehe; but my google search is priceless.
    I suck with google It always links me to pages asking me if i want to buy a domain. At least when I'm searching for C++ stuff.

  9. #9
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    that's ok, wasn't having a go. When I search for anything C++ related, I always add:

    +"C++"

    filters out a load of irrelevant crap.

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  10. #10
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    well, i got it wrong.
    when i search i either am too vague and get cruddy stuff. or too specific and get nothing.
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

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