1. ## Help with this quadratic

Hello all,
I hope I am not bombarding you with my problem but I need to know what was wrong with the code below:

Code:
```/* program to calculate the root of a quadratic equation*/
#include <stdio.h>
#include <math.h>
main()
{
float a, b, c, d, r=0.0, x1, x2;
printf ("\nEnter value for a\n");
scanf ("%f", &a);
printf ("\nEnter value for b\n");
scanf ("%f", &b);
printf ("\nEnter value for c\n");
scanf ("%f", &c);
d=sqrt((b*b)-4*a*c);
if (d>r)
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
printf("\nThe real roots are %f, %f\n", x1, x2);
else if (d<r){
printf ("\nThere are no real root because the equation turns to infinity\n");
};
else
x1=(-b/2*a);
x2=(-b/2*a);
printf("\nThe real roots are %f, %f\n", x1, x2);
}```
Below is the error:
c:\program files\miracle c\quadrati.c: line 18: Parse Error, expecting `'}''
'else if (d<r){ printf ("\nThere are no real root because the equation turns to infinity\n")'
aborting compile

2. Like it says, missing some brackets in your code:
Code:
```   if (d>r) {
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
printf("\nThe real roots are %f, %f\n", x1, x2);
}
else if (d<r){
printf ("\nThere are no real root because the equation turns to infinity\n");
}
else {
x1=(-b/2*a);
x2=(-b/2*a);
printf("\nThe real roots are %f, %f\n", x1, x2);
}```
Yeah, like Happy said, you should get rid of the semicolon as well... didn't catch that one before. [/edit]

3. Your if-else statements should be in blocks like this :

Code:
```if(condition)
{
// do stuff
}
else
{
// do other stuff
}```
That's one thing. And the other thing is that :

Code:
```else if (d<r){
printf ("\nThere are no real root because the equation turns to infinity\n");
};```
You probably don't want that semi-colon there.

Further, you should get rid of Miracle C. It's as old as the dickens, there are better free compilers and IDE's out there.

4. Further, you should get rid of Miracle C. It's as old as the dickens, there are better free compilers and IDE's out there.
I use Borland C 5.5 and got the same error as well.

5. Code:
```/* program to calculate the root of a quadratic equation*/
#include <stdio.h>
#include <math.h>
main()
{
float a, b, c, d, r=0.0, x1, x2;
printf ("\nEnter value for a\n");
scanf ("%f", &a);
printf ("\nEnter value for b\n");
scanf ("%f", &b);
printf ("\nEnter value for c\n");
scanf ("%f", &c);
d=sqrt((b*b)-4*a*c);
if (d>r){
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
printf("\nThe real roots are %f, %f\n", x1, x2);
}
else if (d<r){
printf ("\nThere are no real root because the equation turns to infinity\n");
}
else {
x1=(-b/2*a);
x2=(-b/2*a);
printf("\nThe real roots are %f, %f\n", x1, x2);
}```

//Try that.

6. You need to check the sign of the discriminant before attempting to get the square root of it, as you may be trying to take the sqrt of a negative value. Eg.
Code:
```d = b * b - 4 * a * c;
if(!d) printf("one real root: %f", -b / (2 * a));
if(d > 0)
{
d = sqrt(d);

printf("two real roots: %f, %f", (-b + d) / (2 * a), (-b - d) / (2 * a));
}
else printf("no real roots");```

7. Originally Posted by Happy_Reaper
Your if-else statements should be in blocks like this :

Code:
```if(condition)
{
// do stuff
}
else
{
// do other stuff
}```
That's one thing. And the other thing is that :

Code:
```else if (d<r){
printf ("\nThere are no real root because the equation turns to infinity\n");
};```
You probably don't want that semi-colon there.

Further, you should get rid of Miracle C. It's as old as the dickens, there are better free compilers and IDE's out there.
Honestly, i dont know what I could have done without the able and intelligent people in this forum, I did it and it works great. Thanks so much.

8. Originally Posted by abs.emailverify
I use Borland C 5.5 and got the same error as well.
I never said the compiler was the problem, just that you should not use Miracle C (as it could potentially cause you problems).

9. and dont forget to include return in your main function. Main should always return 0. Include this at the end

Code:
`return 0;`
ssharish2005

10. > and dont forget to include return in your main function. Main should always return 0. Include this at the end

Supposed that main returns an integer value I guess ... what does just "main() {" return ? (?.?)

11. Supposed that main returns an integer value I guess ... what does just "main() {" return ? (?.?)
Main should always return an int, and as such should be declared as int main().

12. > Main should always return an int, and as such should be declared as int main().

13. Even in that case, I think most compilers default to int when a function's return value isn't specified. So main still returns an int. But you're going into compiler specific land...Not a good place to be.

14. Originally Posted by TriKri
> and dont forget to include return in your main function. Main should always return 0. Include this at the end

Supposed that main returns an integer value I guess ... what does just "main() {" return ? (?.?)

it is by default int main(). You specify it or not it is int.

ssharish2005

15. > it is by default int main(). You specify it or not it is int.

Ok, thanks for the info. Though I'm not planing to ever use just "main() {".