1. ## 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
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
/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous
double, double, double&, double&)'
double&, double&, double&, double&)'
/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:565: error: candidates
/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:565: error:
quad.cpp:37: error: parse error before `double'
/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous
/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous
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!

2. Typo's:

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;
}```

3. 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;```

4. Code:
`int quad(double a, double b, double c, double a1, &double &a2)`
Eh?

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

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.

6. Why the check would they want to do that?
Because I am a total idiot and was not thinking! I blame liquor!

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

9. 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;

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
/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:566: error: previous
double, double, double&, double&)'
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)
double&, double&)'
/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:565: error: candidates
/usr/local/lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/include/sys/types.h:565: error:
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