# Need help with quadratic formula functions

• 10-15-2005
orikon
Need help with quadratic formula functions
I can't figure out whats wrong with my program. It's supposed to calculate the quadratic formula using functions, but I think I'm messing up my function call. I get a lot of compiler errors (I'm using gnu g++)

Here's the code:

Code:

```#include<iostream.h> #include<stdlib.h> #include<math.h> //Prototypes int quad(double a, double b, double c,, double &a1, double &a2); //Main Routine int main()         {         double num1, num2, num3;         double ans1, ans2;         int type;         cout<<"Welcome to the .."<<endl;         cin>>num1>>num2>>num3;         type = quad(num1, num2, num3, ans1, ans2);         if (type==0)                 {                 cout<<ans1<<endl;                 cout<<ans2<<endl;                 }         } //Functions int quad(double a, double b, double c, double a1, &double &a2)         {         double temp;         if (a==0) return -1;         temp = b*b-4*a*c;         if (temp<0) return -2;         a1=(-b+sqrt(temp))/(2*a);         a2=(-b-sqrt(temp))/(2*a);         return 0;         }```
and here are the errors

Code:

```quad.cpp:15: error: type specifier omitted for parameter quad.cpp:15: error: `int quad(double, double, double, double&, double&)'   redeclared as different kind of symbol /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous   declaration of `typedef struct quad_t quad' /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous   non-function declaration `typedef struct quad_t quad' quad.cpp:15: error: conflicts with function declaration `int quad(double,   double, double, double&, double&)' quad.cpp: In function `int main()': quad.cpp:26: error: no matching function for call to `_quad::_quad(double&,   double&, double&, double&, double&)' /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:565: error: candidates   are: _quad::_quad() /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:565: error:                   _quad::_quad(const _quad&) quad.cpp: At global scope: quad.cpp:37: error: parse error before `double' quad.cpp: In function `int quad(...)': quad.cpp:38: error: `int quad(...)' redeclared as different kind of symbol /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous   declaration of `typedef struct quad_t quad' /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous   non-function declaration `typedef struct quad_t quad' quad.cpp:38: error: conflicts with function declaration `int quad(...)' quad.cpp:41: error: `a' undeclared (first use this function) quad.cpp:41: error: (Each undeclared identifier is reported only once for each   function it appears in.) quad.cpp:42: error: `b' undeclared (first use this function) quad.cpp:42: error: `c' undeclared (first use this function) quad.cpp:44: error: `a1' undeclared (first use this function) quad.cpp:45: error: `a2' undeclared (first use this function)```
Thanks!
• 10-15-2005
ComputerPhreak
Typo's:

Quote:

Originally Posted by orikon
Code:

```#include<iostream.h> #include<stdlib.h> #include<math.h> //Prototypes int quad(double a, double b, double c,,double &a1 , double &a2); //Main Routine int main()         {         double num1, num2, num3;         double ans1, ans2;         int type;         cout<<"Welcome to the .."<<endl;         cin>>num1>>num2>>num3;         type = quad(num1, num2, num3, ans1, ans2);         if (type==0)                 {                 cout<<ans1<<endl;                 cout<<ans2<<endl;                 }         } //Functions int quad(double a, double b, double c, double a1, &double &a2)         {         double temp;         if (a==0) return -1;         temp = b*b-4*a*c;         if (temp<0) return -2;         a1=(-b+sqrt(temp))/(2*a);         a2=(-b-sqrt(temp))/(2*a);         return 0;         }```

• 10-15-2005
Enahs
First.

Code:

```#include<iostream.h> #include<stdlib.h> #include<math.h>```
No no. Such old library declaration

Code:

```#include <iostream> #include <cmath> #include <cstdlib>```

Now to your function prototype. Why do you have two commas back to back?
Also, it is a type int. That means it expects to return a type int (it should be type double).

Then when you call your function, you pass it “ans1” and “ans2”. But you have done nothing with ans1 and ans2 but declare them. So they will just hold something random in memory that was previously there.

Also, you do not do anything at all with a1 and a2 in your function. The variables inside the ( ) are things you want to pass to it, to use in the function…not things you want to return.

I would sugest using a void function, and just call it in main by
Code:

`quad(a,b,c); //Passing it the user inputted vales of a,b,c`
And in the function, instead of
Code:

```        a1=(-b+sqrt(temp))/(2*a);         a2=(-b-sqrt(temp))/(2*a); //I would do cout << ( (-b+sqrt(temp))/(2*a) ) << endl; cout << ( (-b-sqrt(temp))/(2*a) ) << endl;```
• 10-15-2005
dwks
Code:

`int quad(double a, double b, double c, double a1, &double &a2)`
Eh?
• 10-15-2005
Thantos
Quote:

Originally Posted by Enahs
And in the function, instead of

Code:

```        a1=(-b+sqrt(temp))/(2*a);         a2=(-b-sqrt(temp))/(2*a); //I would do cout << ( (-b+sqrt(temp))/(2*a) ) << endl; cout << ( (-b-sqrt(temp))/(2*a) ) << endl;```

Why the check would they want to do that?
1) It violates the idea of having a function do just one thing (in this case doing the calculations)
2) Wouldn't allow for use of the answers anywhere else in the program
3) limits your output to only the stdout

Quote:

Also, you do not do anything at all with a1 and a2 in your function. The variables inside the ( ) are things you want to pass to it, to use in the function…not things you want to return.
Aside from the syntax errors his idea is correct. He wants to have the function return answers, which there are two of. Since you can only return 1 value it makes perfect sense to use a reference to pass back the results. Also he is using the return value to indicate errors, also a perfectly good idea.
• 10-15-2005
Enahs
Quote:

Why the check would they want to do that?
Because I am a total idiot and was not thinking! I blame liquor!
• 10-15-2005
orikon
lol, I appreciate the help guys. I fixed those typos, but I'm still getting most of the same errors. I did try Enahs's idea, but that didn't really work the way I wanted. I still can't figure out what's wrong, Any other suggestions?
• 10-15-2005
Salem
• 10-15-2005
orikon
ok, pretty much the same thig as what i had before except without the typos:
Code:

```#include<iostream> #include<cstdlib> #include<cmath> //Prototypes int quad(double a, double b, double c, double &a1, double &a2); //Main Routine int main()         {         double num1, num2, num3;         double ans1, ans2;         int type;         cout<<"Welcome to the .."<<endl;         cin>>num1>>num2>>num3;         type = quad(num1, num2, num3);         if (type==0)                 {                 cout<<ans1<<endl;                 cout<<ans2<<endl;                 }         else cout<<"no"<<endl;         } //Functions int quad(double a, double b, double c, double &a1, double &a2)         {         double temp;         if (a==0) return -1;         temp = b*b-4*a*c;         if (temp<0) return -2;         a1=(-b+sqrt(temp))/(2*a);         a2=(-b-sqrt(temp))/(2*a);         return 0;         }```
And here are the errors.
Code:

``` quad.cpp:15: error: `int quad(double, double, double, double&, double&)'   redeclared as different kind of symbol /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous   declaration of `typedef struct quad_t quad' /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous   non-function declaration `typedef struct quad_t quad' quad.cpp:15: error: conflicts with function declaration `int quad(double,   double, double, double&, double&)' quad.cpp: In function `int main()': quad.cpp:24: error: `cout' undeclared (first use this function) quad.cpp:24: error: (Each undeclared identifier is reported only once for each   function it appears in.) quad.cpp:24: error: `endl' undeclared (first use this function) quad.cpp:25: error: `cin' undeclared (first use this function) quad.cpp:26: error: no matching function for call to `_quad::_quad(double&,   double&, double&)' /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:565: error: candidates   are: _quad::_quad() /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:565: error:                   _quad::_quad(const _quad&) quad.cpp: In function `int quad(double, double, double, double&, double&)': quad.cpp:38: error: `int quad(double, double, double, double&, double&)'   redeclared as different kind of symbol /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous   declaration of `typedef struct quad_t quad' /usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous   non-function declaration `typedef struct quad_t quad' quad.cpp:38: error: conflicts with function declaration `int quad(double,   double, double, double&, double&)'```
• 10-15-2005
Thantos
Quote:

/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous