Thread: 'Parse error before X'...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    'Parse error before X'...

    Hi, I'm pretty new to C programming and I'm using C-free 3.5 compiler to try a few lines of code. I usually get the error "Parse error before 'else'(for example)". What does it mean?. I get that error on lines 10 and 13 of the program below. What I was trying to do was make a program that prompts the user for 2 numbers, if the user types the same number, he has to type two numbers again. If the numbers are different, the program tells the user which number is bigger.
    Perhaps I should have used "while", but I wasn't sure how to...

    Code:
    #include <stdio.h>
    void main()
    {
        int a,b,max;
        
        scanf("%d %d", &a, &b);
        if (a == b)
        printf ("The value of the numbers is the same\n");
        scanf("%d %d", &a, &b);
        else if(a<b)
            max = b;
        printf("The bigger number is: %d\n", max);
        else
        max = a;
        printf("The bigger number is: %d\n", max);
    }
    Thanks
    Alastor

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Code:
    #include <stdio.h>
    void main()
    {
        int a,b,max;
        
        scanf("%d %d", &a, &b);
    
        if (a == b) {
            printf ("The value of the numbers is the same\n");
            scanf("%d %d", &a, &b);  // hmm...
        } else if(a<b) {
            max = b;
            printf("The bigger number is: %d\n", max);
        } else {
            max = a;
            printf("The bigger number is: %d\n", max);
        }
    }
    If you're going to need >1 line in an if statement, you need to enclose it in brackets.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Thanks a lot, but why do you need the {} brackets for that but not for

    Code:
    #include <stdio.h>
    void main()
    {
        int a,b,max;
    
        scanf("%d %d", &a, &b);
        if (a>b)
         max=a;
    else
    max=b;
    printf("The bigger number is %d\n", max)
    }
    Thanks
    Alastor

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You need { } brackets if you want more than one statement to be affected by an if.
    If you say:
    Code:
    if(a < 2)
      a++;
    b++;
    Then a is only incremented if a is less than two, while b is always incremented. However, if you say:
    Code:
    if(a < 2)
    {
      a++;
      b++;
    }
    Then a and b will only be incremented if a is less than two.

    The same applies in many other places, such as for and while loops.
    When I write code, if I use an if statement without the {}s, I leave the statement it affects on the same line as the if (ex: "if(b < 3) a++;") or, if I put it on the next line, I indent it to make it more apparent that it's part of the if(), and the next lines aren't. (Such as in the first example in this post.)
    Last edited by Cactus_Hugger; 10-01-2005 at 02:57 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM