# What is wrong with my quadratic formula code?

Page 1 of 1

## 7 Replies - 905 Views - Last Post: 27 September 2012 - 09:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293426&amp;s=e53e6444176c41557f588675b6ef3d93&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cjmitchell7

Reputation: 0
• Posts: 2
• Joined: 27-September 12

# What is wrong with my quadratic formula code?

Posted 27 September 2012 - 12:17 PM

I got these two errors when trying to compile my code

P:\EGR155\SQUARE~1.C(5) : error C2449: found '{' at file scope (missing function header?)
P:\EGR155\SQUARE~1.C(24) : error C2059: syntax error : '}'

[
```#include <stdio.h>
#include <math.h>

int main(void);
{
double a, b, c, root_1, root_2;

printf("Enter a's value:");
scanf("%f," &a);

printf("Enter b's value:");
scanf("%f," &B);

printf("Enter c's value:");
scanf("%f," &c);

formula = ((pow(b,2)-4*a*c)/2*a);

root 1 = (-b + sqrt(formula));

root 2 = (-b - sqrt(formula));

return 0;
}
```
]

This post has been edited by jimblumberg: 27 September 2012 - 12:31 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Is This A Good Question/Topic? 0

## Replies To: What is wrong with my quadratic formula code?

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11761
• Posts: 44,197
• Joined: 27-December 08

## Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 12:33 PM

You have a semi-colon here int main(void);. Remove it, because you do not need it.

Also, the quadratic formula is as follows:
```x = [-b +- sqrt(b^2 - 4ac)]/(2a)

```

In other words, you don't want the root of the 2a term.

### #3 jimblumberg

Reputation: 4894
• Posts: 15,282
• Joined: 25-December 09

## Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 12:33 PM

You have an extra semicolon in the following line:
```int main(void);
```

Also you should check your documentation for the scanf() function. You are using the incorrect format specifier for the double. The "%f" specifier is for a float, not a double.

Jim

### #4 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:21 PM

cjmitchell7, on 28 September 2012 - 12:47 AM, said:

```int main(void);
{
double a, b, c, root_1, root_2;
//initialize root1 and root2 here

```

Remove semicolon from main()
you could use float

Quote

```printf("Enter a's value:");
scanf("%f," &a);

printf("Enter b's value:");
scanf("%f," &B)/>;

printf("Enter c's value:");
scanf("%f," &c);
formula = ((pow(b,2)-4*a*c)/2*a);

```

change the 2nd scanf to
```scanf("%f",&B)/> /*remember c is a case sensitive language*/
```

remove formula variable and initialize root1 and root2 (remember no spaces between variables) as float where I have specified.

```
root1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);

root2 = (-b - sqrt(b^2 - 4*a*c))/(2*a);

```

regards,
Raghav

This post has been edited by raghav.naganathan: 27 September 2012 - 09:24 PM

### #5 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:27 PM

raghav.naganathan, on 28 September 2012 - 09:51 AM, said:

change the 2nd scanf to
```scanf("%f",&B)/> /*remember c is a case sensitive language*/
```

Sorry I meant 'b'
regards,
Raghav

### #6 jimblumberg

Reputation: 4894
• Posts: 15,282
• Joined: 25-December 09

## Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:38 PM

raghav.naganathan, on 27 September 2012 - 11:21 PM, said:

cjmitchell7, on 28 September 2012 - 12:47 AM, said:

```int main(void);
{
double a, b, c, root_1, root_2;
//initialize root1 and root2 here

```

Remove semicolon from main()
you could use float

I would recommend using doubles, and using the correct format specifier.

Quote

```printf("Enter a's value:");
scanf("%f," &a);

printf("Enter b's value:");
scanf("%f," &B);

printf("Enter c's value:");
scanf("%f," &c);
formula = ((pow(b,2)-4*a*c)/2*a);

```

change the 2nd scanf to
```scanf("%f",&B) /*remember c is a case sensitive language*/
```

This is a problem with this board's software. It thinks you are using an emoticon.

Quote

remove formula variable and initialize root1 and root2 (remember no spaces between variables) as float where I have specified.

```
root1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);

root2 = (-b - sqrt(b^2 - 4*a*c))/(2*a);

```

The caret (^) is the exclusive or symbol, not the symbol for exponentiation. There is no exponentiation symbol in C/C++ you need to either do the multiplication(b * b ) or use the pow() function. Also C/C++ is not sensitive to white space.
```// These are all equivalent.
root = 4*a*c;
root = 4* a*c;
root = 4 * a * c;

```

Jim

This post has been edited by jimblumberg: 27 September 2012 - 09:39 PM

### #7 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:44 PM

jimblumberg, on 28 September 2012 - 10:08 AM, said:

raghav.naganathan, on 27 September 2012 - 11:21 PM, said:

cjmitchell7, on 28 September 2012 - 12:47 AM, said:

```int main(void);
{
double a, b, c, root_1, root_2;
//initialize root1 and root2 here

```

Remove semicolon from main()
you could use float

I would recommend using doubles, and using the correct format specifier.

Quote

```printf("Enter a's value:");
scanf("%f," &a);

printf("Enter b's value:");
scanf("%f," &B)/>;

printf("Enter c's value:");
scanf("%f," &c);
formula = ((pow(b,2)-4*a*c)/2*a);

```

change the 2nd scanf to
```scanf("%f",&B)/> /*remember c is a case sensitive language*/
```

This is a problem with this board's software. It thinks you are using an emoticon.

Quote

remove formula variable and initialize root1 and root2 (remember no spaces between variables) as float where I have specified.

```
root1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);

root2 = (-b - sqrt(b^2 - 4*a*c))/(2*a);

```

The caret (^) is the exclusive or symbol, not the symbol for exponentiation. There is no exponentiation symbol in C/C++ you need to either do the multiplication(b * b ) or use the pow() function. Also C/C++ is not sensitive to white space.
```// These are all equivalent.
root = 4*a*c;
root = 4* a*c;
root = 4 * a * c;

```

Jim

Thank you very much sir, I sure am learning a lot from you

By the way, I meant no spaces for variables eg.
``` int root 1;// should be root1
```

regards
Raghav

### #8 jimblumberg

Reputation: 4894
• Posts: 15,282
• Joined: 25-December 09

## Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:54 PM

Quote

By the way, I meant no spaces for variables eg.

Agreed, but since the OP defined the variables with the underscore the underscore is needed everywhere the variable is referenced.
```double root_1;
.....

root_1 = someValue;

```

Jim