Where is the error?

R

Red Dragon

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 = (-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;

}
 
R

Richard Tobin

Red Dragon said:
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
 
J

Jaspreet

Red said:
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 = (-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]
 
D

DevarajA

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 = (-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.
 
D

Default User

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

pete

Jaspreet said:
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.
 
O

Old Wolf

Red said:
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 = (-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.
 
J

Jaspreet

Old said:
As well as what everyone else pointed out:


Use 'double' instead of 'float', it has more precision.


You should check scanf for errors, in case they type in words
instead of numbers, etc.


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.


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.
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.
 
R

Red Dragon

Old Wolf said:
As well as what everyone else pointed out:
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.
 
K

Kenneth Brody

<GoogleGroups img="smiley.png">Me too, also.</GoogleGroups>




















































Keith said:
<AOL>Me too.</AOL>

(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 <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
P

Peter Nilsson

Jaspreet said:
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. ;)
 
K

Keith Thompson

Jaspreet said:
Old said:
Red Dragon 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)".

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.

And it's ugly. And its mother dresses it funny.

(Consult the archives if you want to read endless arguments about
"E == 0" vs. "0 == E".)
 
J

Jaspreet

Keith said:
Jaspreet said:
Old said:
Red Dragon 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)".

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.

And it's ugly. And its mother dresses it funny.

(Consult the archives if you want to read endless arguments about
"E == 0" vs. "0 == E".)

--

I have already noticed the endless discussions that have taken place
but if the OP had written "if (0 == E)", he would have caught an error
by himself. Does that not ask for writing the if condition that way ?

Ok, lets leave it at that. No point in starting the sequel to those
endless arguments.
 
T

Tim Rentsch

Jaspreet said:
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? :)
 
S

Skarmander

Tim said:
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.

S.
 
J

Jaspreet

Tim said:
Jaspreet said:
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. :)
 
T

Tim Rentsch

Skarmander said:
Tim said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top