1
  • This is a C language program
  • The following program is about calculate the the solutions for the linear system: a1x+b1y=c1,a2x+b2y+c2.
  • I checked the syntax of else-if,but the compiler still tells me “Else without a previous if",I got no idea what's happening.

I wonder whether you could give some instructions.

Thanks a lot!

#include<stdio.h>
#include<math.h>
//this program is used to calculate the solutions for the linear system: a1x+b1y=c1,a2x+b2y+c2//
int main()
{
    float a1,a2,b1,b2,c1,c2,x,y;
    printf("Enter values for a1,a2,b1,b2,c1,c2:");
    scanf("%f %f %f %f %f %f",&a1,&a2,&b1,&b2,&c1,&c2);

    if ((a1*b2-a2*b1) <= 0.01 || (a1*b2-a2*b1) == 0);//if the denominator is too small//
    {
    printf("The denominator is 0.\n");
    }
    else
    {
        x = (b2*c1-b1*c2)/(a1*b2-a2*b1);
        y = (a1*c2-a2*c1)/(a1*b2-a2*b1);
    }
    printf("x = %f",x);printf("y = %f\n",y);
    return 0;
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
anmo
  • 83
  • 9
  • 2
    There is a `;` after your `if` which screws it up. – DeiDei Sep 28 '18 at 05:40
  • @DeiDei As I can not find a dupe for this question and it might be relevant for future visitors (even if it is a simple error). Would you mind writing an answer for this? – Kami Kaze Sep 28 '18 at 06:11
  • Thanks to everyone.I got to know that when ever I met a problem,I am supposed to deal with it myself.That's a really important process.Thanks for your instructions and your tolerence to my ignorance.I will never made such stupid mistakes again.Thanks again for everyone!@Antti Haapala – anmo Sep 28 '18 at 06:33
  • As a hint for future questions: You get better response if you use precise descriptions of your problem. If you have a problem with `if else`, there is no need to add anything about a linear system. Also you don't use `else if`, but only use normal `if` with an `else`. The better your title, the better the help because you attract the right people to look into the question. Same goes for the code. For a minimal example you could remove all the `printf` and `scanf` stuff and the calculations as it is not related to the problem. – Gerhardh Sep 28 '18 at 06:50
  • @Gerhardh Thank you!I got it.I will implement these principles from now on. – anmo Sep 28 '18 at 07:03

2 Answers2

1

Thanks to @DeiDei
It is the ";"after the sentence(if) which create the problem. If there is an ";"after "if",that means the end of the sentence "if",so the sentence of "else" will not be regarded as part of the sentence"if".

anmo
  • 83
  • 9
1
if ((a1*b2-a2*b1) <= 0.01 || (a1*b2-a2*b1) == 0);//if the denominator is too small//

Because of that semicolon after the if statement, that semicolon is now the body of the if statement. Yes, a spare semicolon is actually legal code - it just does nothing. Most compilers will optimize it away instead of translating it to actual machine code that does nothing (which also exists).

So basically, that snipped says "if (a1*b2-a2*b1) <= 0.01) or ((a1*b2-a2*b1) == 0)", then do nothing. And else... also do nothing. Your compiler will probably optimize away this line completely.

{
printf("The denominator is 0.\n");
}

As it is not part of the if statement, this now just always prints. The braces around it are legal even without an if.

So now the following else block here

else
{
    x = (b2*c1-b1*c2)/(a1*b2-a2*b1);
    y = (a1*c2-a2*c1)/(a1*b2-a2*b1);
}

...is completely alone, because the if is over and done at this point. That's why you get the error. The solution to your problem is to remove the semicolon at the end of the line where your if statement starts.

It is the ";"after the sentence(if) which create the problem. If there is an ";"after "if",that means the end of the sentence "if",so the sentence of "else" will not be regarded as part of the sentence"if".

Exactly. The reason why the error message is so confusing is because the actual mistake of adding that semicolon didn't instantly make that statement invalid, but resulted in a different, valid construct that kept being valid all the way through the block with the printf and only stopped making sense at the else.

Blaze
  • 16,736
  • 2
  • 25
  • 44