454,942 Members | 1,174 Online
Need help? Post your question and get tips & solutions from a community of 454,942 IT Pros & Developers. It's quick & easy.

# Where is the error?

 P: n/a I am C programming student. My program here has an error message " illegal else without matching if " which I cannot figure out what is not matching. Can somebody please help. Where is the mistake? Thanks Khoon /* Roots of a Quadratic Equation. 12.10.05 */ #include #include int main (void) { int a; int b; int c; float x1; float x2; float E; float R; float I; printf ("Please key in the value of constant a,b and c for finding the roots of quadratic"); printf ("equation ax%c+bx+c=0 :",253); scanf ("%d%d%d", &a,&b,&c); E =(b*b - 4*a*c); if ( E > 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); printf ("Your quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x2); else if (E = 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); printf ("Your quadratic equation has two same roots : x1=x2=%1.6f",x1); else R = (-b/2*a); I = (sqrt(E))/(2*a); printf ("Your quadratic equation has two distinct imaginary roots :\n"); printf (" x1=%1.6f+%1.6fi , x2=%1.6f-%1.6fi\n",R,I,R,I); return 0; } Nov 15 '05 #1
13 Replies

 P: n/a In article <43**********@news.tm.net.my>, Red Dragon wrote: if ( E > 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); You seem to be relying on identation to specify the structure. (This was long considered a joke until Python actually did it.) You need braces around the then and else parts. -- Richard Nov 15 '05 #2

 P: n/a Red Dragon wrote: I am C programming student. My program here has an error message " illegal else without matching if " which I cannot figure out what is not matching. Can somebody please help. Where is the mistake? Thanks Khoon /* Roots of a Quadratic Equation. 12.10.05 */ #include #include int main (void) { int a; int b; int c; float x1; float x2; float E; float R; float I; printf ("Please key in the value of constant a,b and c for finding the roots of quadratic"); printf ("equation ax%c+bx+c=0 :",253); scanf ("%d%d%d", &a,&b,&c); E =(b*b - 4*a*c); if ( E > 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); More than 1 statement and you need a curly brace. Thats why I actually try and have a brace ir-respective of the number of statemnts I am going to put in my if-else contruct. [snip rest of the program] Nov 15 '05 #3

 P: n/a Red Dragon ha scritto: I am C programming student. My program here has an error message " illegal else without matching if " which I cannot figure out what is not matching. Can somebody please help. Where is the mistake? Thanks Khoon /* Roots of a Quadratic Equation. 12.10.05 */ #include #include int main (void) { int a; int b; int c; float x1; float x2; float E; float R; float I; printf ("Please key in the value of constant a,b and c for finding the roots of quadratic"); printf ("equation ax%c+bx+c=0 :",253); scanf ("%d%d%d", &a,&b,&c); E =(b*b - 4*a*c); if ( E > 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); printf ("Your quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x2); else if (E = 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); printf ("Your quadratic equation has two same roots : x1=x2=%1.6f",x1); else R = (-b/2*a); I = (sqrt(E))/(2*a); printf ("Your quadratic equation has two distinct imaginary roots :\n"); printf (" x1=%1.6f+%1.6fi , x2=%1.6f-%1.6fi\n",R,I,R,I); return 0; } You need braces when using if..else, do..while, while, for, declaring a structure or a function, and wherever you need to logically enclose some code. if(x==5) { /*do something*/ } while(x!=10) { /*do something*/ } do { /*do something*/ }while(x!=20); for(x=0;x<50;x++) { /*do something*/ } And so on.. Anyway a good book should explain this in the first pages. -- Devaraja (Xdevaraja87^gmail^c0mX) Linux Registerd User #338167 http://counter.li.org Nov 15 '05 #4

 P: n/a Red Dragon wrote: Your question has been answered elsewhere. Please change your OE settings to stop posting in HTML. Brian Nov 15 '05 #5

 P: n/a Jaspreet wrote: More than 1 statement and you need a curly brace. Thats why I actually try and have a brace ir-respective of the number of statemnts I am going to put in my if-else contruct. Me too. -- pete Nov 15 '05 #6

 P: n/a pete writes: Jaspreet wrote: More than 1 statement and you need a curly brace. Thats why I actually try and have a brace ir-respective of the number of statemnts I am going to put in my if-else contruct. Me too. Me too. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Nov 15 '05 #7

 P: n/a Red Dragon wrote: I am C programming student. As well as what everyone else pointed out: #include #include int main (void) { int a; int b; int c; float x1; float x2; float E; float R; float I; Use 'double' instead of 'float', it has more precision. printf ("Please key in the value of constant a,b and c for finding the roots of quadratic"); printf ("equation ax%c+bx+c=0 :",253); scanf ("%d%d%d", &a,&b,&c); You should check scanf for errors, in case they type in words instead of numbers, etc. E =(b*b - 4*a*c); if ( E > 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); printf ("Your quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x2); You should print a "\n" at the end of your printf. Otherwise the output might nto appear correctly. Same goes for the rest of the printfs. else if (E = 0) This assigns 0 to E. If you want to check if E equals 0, you need to write: if (E == 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); printf ("Your quadratic equation has two same roots : x1=x2=%1.6f",x1); else R = (-b/2*a); I = (sqrt(E))/(2*a); printf ("Your quadratic equation has two distinct imaginary roots :\n"); printf (" x1=%1.6f+%1.6fi , x2=%1.6f-%1.6fi\n",R,I,R,I); return 0; The return should be outside the if...else block, of course. } Nov 15 '05 #8

 P: n/a Old Wolf wrote: Red Dragon wrote: I am C programming student. As well as what everyone else pointed out: #include #include int main (void) { int a; int b; int c; float x1; float x2; float E; float R; float I; Use 'double' instead of 'float', it has more precision. printf ("Please key in the value of constant a,b and c for finding the roots of quadratic"); printf ("equation ax%c+bx+c=0 :",253); scanf ("%d%d%d", &a,&b,&c); You should check scanf for errors, in case they type in words instead of numbers, etc. E =(b*b - 4*a*c); if ( E > 0) x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); printf ("Your quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x2); You should print a "\n" at the end of your printf. Otherwise the output might nto appear correctly. Same goes for the rest of the printfs. else if (E = 0) This assigns 0 to E. If you want to check if E equals 0, you need to write: if (E == 0) Hence it is always preferable to write the if as: if (constant == variable) that is "if (0 == E)". However, this trick would take care of the such conditions only when there is a variable either on the left or on the right side. x1 = (-b+sqrt(E))/(2*a); x2 = (-b-sqrt(E))/(2*a); printf ("Your quadratic equation has two same roots : x1=x2=%1.6f",x1); else R = (-b/2*a); I = (sqrt(E))/(2*a); printf ("Your quadratic equation has two distinct imaginary roots :\n"); printf (" x1=%1.6f+%1.6fi , x2=%1.6f-%1.6fi\n",R,I,R,I); return 0; The return should be outside the if...else block, of course. return is already out of the if..else block. I assume you meant once the OP puts the curly braces, "return 0" needs to be out of else. } Nov 15 '05 #9

 P: n/a "Old Wolf" wrote in message news:11**********************@z14g2000cwz.googlegr oups.com... Red Dragon wrote: I am C programming student. As well as what everyone else pointed out: #include #include Thank you all very very very much. I have put in the braces and the problem disappeared.. Thank you guys for the enlightenment and the tips. Regards, Khoon. Nov 15 '05 #10

 P: n/a Me too, also. Keith Thompson wrote: pete writes: Jaspreet wrote: More than 1 statement and you need a curly brace. Thats why I actually try and have a brace ir-respective of the number of statemnts I am going to put in my if-else contruct. Me too. Me too. (Sorry about that top-posting, but I had no other way to make it look like the typical unquoted Google replies we get.) -- +-------------------------+--------------------+-----------------------------+ | Kenneth J. Brody | www.hvcomputer.com | | | kenbrody/at\spamcop.net | www.fptech.com | #include | +-------------------------+--------------------+-----------------------------+ Don't e-mail me at: Nov 15 '05 #11

 P: n/a Jaspreet wrote: Old Wolf wrote: else if (E = 0) This assigns 0 to E. If you want to check if E equals 0, you need to write: if (E == 0) Hence it is always preferable to write the if as: ^^^^^^ if (constant == variable) that is "if (0 == E)". In the same sense that it is _always_ preferable to hop on your left foot becuase it avoids damage to the right leg. Walking may be risky, but at least it's natural. ;) -- Peter Nov 15 '05 #12

 P: n/a Tim Rentsch wrote: "Jaspreet" writes: Keith Thompson wrote: [snip] (Consult the archives if you want to read endless arguments about "E == 0" vs. "0 == E".) [snip] Ok, lets leave it at that. No point in starting the sequel to those endless arguments. If the arguments are endless, how can they have a sequel? Doesn't that violate the Infinity Lemma or something? :) The last we put an end to those arguments was just the end of Season 1. Lets not start a new season of those endless arguments. I hope this time I am not in violation of any theorm. I plead not guilty by manner of insanity. :) Nov 15 '05 #13

 P: n/a Skarmander writes: Tim Rentsch wrote: "Jaspreet" writes:Keith Thompson wrote: [snip](Consult the archives if you want to read endless arguments about"E == 0" vs. "0 == E".) [snip]Ok, lets leave it at that. No point in starting the sequel to thoseendless arguments. If the arguments are endless, how can they have a sequel? Doesn't that violate the Infinity Lemma or something? :) One word: ordinals. Ah, the tragedy of a comic playing to an audience with only an infinitesimal sense of humor. Pray begin ordinating, and please let us know when you get to omega. :) Nov 15 '05 #14

### This discussion thread is closed

Replies have been disabled for this discussion.