Thread: error: parse error before "else"

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    34

    error: parse error before "else"

    Code:
    *cols = 2;
                    
                    else if (!table[*rows][*cols]) {
                         
                        table[*rows][*cols] = 1;
                        
                        return TRUE;
                    }
    ~flood

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    More code please. Just because it says "parse error before else" doesn't mean the error is directly before the else.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    I have hundreads of lines before that piece of code. It compiles well if I just comment *cols = 2;
    ~flood

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    before an else if statement, you need either an if statement, or another else if statement. Currently, you appear to have:
    Code:
    *cols = 2;
    before your else if block, but it is not inside of an if or else if block.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have hundreads of lines before that piece of code.
    That's nice, why not just limit yourself to the first five lines before that assignment if you're afraid you might get carried away and post it all.

    >It compiles well if I just comment *cols = 2;
    You don't quite understand. I don't care. I want to see what you have before that line because you've given a partial snippet. I could paste that snippet into a test program and get it to work without any modification, that's why I want to see something that is proof of an error.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    Quote Originally Posted by bithub
    before an else if statement, you need either an if statement, or another else if statement. Currently, you appear to have:
    Code:
    *cols = 2;
    before your else if block, but it is not inside of an if or else if block.
    Yea you are right. Why does it work like that?


    That's nice, why not just limit yourself to the first five lines before that assignment if you're afraid you might get carried away and post it all.
    sorry about that.
    ~flood

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why does it work like that?
    Your other option is to scrap everything and start over.

    Curious as to what your first option was? Well, so is your compiler, so anytime you have an else clause, you need an if clause as well.

    >but it is not inside of an if or else if block
    A hint for future replies: No matter how sure you are of your assumption, it's still an assumption. It could be that the snippet just had bad formatting, which is why I was pushing for a more complete example. I don't know about you, but I'd rather be 100% sure of the problem than to be 99% sure of the problem and have it turn out to be that odd 1%.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    Given his error and his code, my only assumption was that the code he posted was the actual code he was using in his project. If I had to start second guessing every bit of code posted here (Is that actually what your code looks like, or did you mess up the copy/paste), then I would quickly become frustrated.

    If I keep assuming the error they post is the actual error they are getting, and the code they post is the actual code they are using, then I can sleep soundly at night knowing that the odd 1% is on them, and not me

    >>I could paste that snippet into a test program and get it to work without any modification
    eh, how?

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    Your other option is to scrap everything and start over.

    Curious as to what your first option was? Well, so is your compiler, so anytime you have an else clause, you need an if clause as well.
    I did have an if statement before the else if it's just that *cols = 2; was between the two.
    ~flood

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    ><snip> then I can sleep soundly at night knowing that the odd 1% is on them, and not me
    Whatever floats your boat. I'm far more anal.

    >eh, how?
    This is a direct paste with no changes into a test program. It compiles cleanly, and in a real program, the if statements are plausable because we had no idea what the logic for the program was.
    Code:
    #define TRUE 1
    
    int main ( void )
    {
      int table[3][3] = {0};
      int r = 0, c = 0, *rows = &r, *cols = &c;
    
      if (table[*rows][*cols])
      *cols = 2;
                    
                    else if (!table[*rows][*cols]) {
                         
                        table[*rows][*cols] = 1;
                        
                        return TRUE;
                    }
    }
    >it's just that *cols = 2; was between the two.
    That breaks the if chain, so you were effectively trying to start a new if chain with an else if.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    >> Whatever floats your boat. I'm far more anal.


    >> This is a direct paste with no changes into a test program.
    Yeah, I guess I was thinking too much in context of the error he gave.

  12. #12
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Are there any other statements between the if and else statements besides *cols = 2; ?

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    Are there any other statements between the if and else statements besides *cols = 2; ?
    nope
    ~flood

  14. #14
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Hey, I've got an idea. How about posting more code? I'm sure this is entirely original and no one's mentioned it, so I thought I'd take care of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with "else"
    By Brain_Child in forum C Programming
    Replies: 4
    Last Post: 02-28-2009, 07:34 AM
  2. "expected primary-expression before "else" ???
    By Helix Stark in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 11:04 PM