7 Replies - 597 Views - Last Post: 27 September 2012 - 09:54 PM Rate Topic: -----

#1 cjmitchell7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:21 PM

View Postcjmitchell7, 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

Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:27 PM

View Postraghav.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
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:38 PM

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

View Postcjmitchell7, 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

Was This Post Helpful? 1
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: What is wrong with my quadratic formula code?

Posted 27 September 2012 - 09:44 PM

View Postjimblumberg, on 28 September 2012 - 10:08 AM, said:

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

View Postcjmitchell7, 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
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • 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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1