473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Where is the error?

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;

}

Nov 15 '05 #1
13 1813
In article <43**********@news.tm.net.my>,
Red Dragon <ts*****@streamyx.com> 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
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 = (-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
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.

--
Devaraja (Xdevaraja87^gmail^c0mX)
Linux Registerd User #338167
http://counter.li.org
Nov 15 '05 #4
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
Nov 15 '05 #5
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
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 ir-respective of the number of statemnts I am
going to put in my if-else 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.
Nov 15 '05 #7
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 = (-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
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 = (-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

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

Nov 15 '05 #10
<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 ir-respective of the number of statemnts I am
going to put in my if-else contruct.


Me too.


<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:Th*************@gmail.com>

Nov 15 '05 #11
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

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. :)

Nov 15 '05 #13
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. :)
Nov 15 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
18
by: jabailo | last post by:
I wrote a program that loops through a file of records. It parses each line in the file and sends them to a web service that inserts them into an AS400DB2 database using Asynch calls. This is...
7
by: Yongsub Eric Shin | last post by:
Hi. I'm just a beginner in ASP.Net. I started writing codes and I keep on getting this Runtime Error page, where it says "Description: An application error occurred on the server. The current...
7
by: Britney | last post by:
Original code: this.oleDbSelectCommand1.CommandText = "SELECT TOP 100 user_id, password, nick_name, sex, age, has_picture, city, state, " + "country FROM dbo.users WHERE (has_picture = ?) AND (sex...
3
by: Yannick | last post by:
Hi, I try to execute request on a ms-access database but I have a problem with date. the "myDate" field's format is "date/time" my request is: SELECT myCode, myStuff, myDATE FROM myTable ...
12
by: Yannick | last post by:
Hi, I've got a problem accessing a ms-access db with a sql statement like this: SELECT * FROM laTable WHERE laDate = #05/21/2004# ; with asp.net (vb code) laTable contains a "laDate"...
9
by: Scott Ribe | last post by:
OK, I'm using a pool of worker threads that each have a connection (from my own software, no PHP or anything like that involved), and a connection is in a state where all queries fail. Looking back...
7
by: Swinky | last post by:
Mr. Browne's copy code on his web site has saved me. I have been struggling to copy a record with several related sub-form tables. I found code on his web site that copies a sub-form table,...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
10
by: arial | last post by:
Hi, I am getting this error message: Incorrect syntax near the keyword 'where'. Description: An unhandled exception occurred during the execution of the current web request. Please review...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.