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 <stdio.h>
#include <math.h>
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 = (bsqrt(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 = (bsqrt(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;
}  
Share this Question
P: n/a

In article <43**********@news.tm.net.my>,
Red Dragon <ts*****@streamyx.com> wrote: if ( E > 0)
x1 = (b+sqrt(E))/(2*a); x2 = (bsqrt(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  
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 <stdio.h> #include <math.h>
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 = (bsqrt(E))/(2*a);
More than 1 statement and you need a curly brace. Thats why I actually
try and have a brace irrespective of the number of statemnts I am
going to put in my ifelse contruct.
[snip rest of the program]  
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 <stdio.h> #include <math.h>
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 = (bsqrt(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 = (bsqrt(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  
P: n/a

Red Dragon wrote: <!DOCTYPE HTML PUBLIC "//W3C//DTD HTML 4.0 Transitional//EN">
Your question has been answered elsewhere. Please change your OE
settings to stop posting in HTML.
Brian  
P: n/a

Jaspreet wrote: More than 1 statement and you need a curly brace. Thats why I actually try and have a brace irrespective of the number of statemnts I am going to put in my ifelse contruct.
Me too.

pete  
P: n/a

pete <pf*****@mindspring.com> writes: Jaspreet wrote:
More than 1 statement and you need a curly brace. Thats why I actually try and have a brace irrespective of the number of statemnts I am going to put in my ifelse contruct.
Me too.
<AOL>Me too.</AOL>

Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.  
P: n/a

Red Dragon wrote: I am C programming student.
As well as what everyone else pointed out:
#include <stdio.h> #include <math.h>
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 = (bsqrt(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 = (bsqrt(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. }  
P: n/a

Old Wolf wrote: Red Dragon wrote: I am C programming student. As well as what everyone else pointed out:
#include <stdio.h> #include <math.h>
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 = (bsqrt(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 = (bsqrt(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. }
 
P: n/a

"Old Wolf" <ol*****@inspire.net.nz> 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 <stdio.h> #include <math.h>
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.  
P: n/a

<GoogleGroups img="smiley.png">Me too, also.</GoogleGroups>
Keith Thompson wrote: pete <pf*****@mindspring.com> writes: Jaspreet wrote:
More than 1 statement and you need a curly brace. Thats why I actually try and have a brace irrespective of the number of statemnts I am going to put in my ifelse contruct.
Me too.
<AOL>Me too.</AOL>
(Sorry about that topposting, 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 <std_disclaimer.h> 
++++
Don't email me at: <mailto:Th*************@gmail.com>  
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  
P: n/a

Tim Rentsch wrote: "Jaspreet" <js***********@gmail.com> 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. :)  
P: n/a

Skarmander <in*****@dontmailme.com> writes: Tim Rentsch wrote: "Jaspreet" <js***********@gmail.com> 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? :)
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. :)   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1449
 replies: 13
 date asked: Nov 15 '05
