Click to See Complete Forum and Search --> : Need help with quadratic formula function


mensch
October 15th, 2005, 01:40 PM
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:


#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


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!

a_c
October 15th, 2005, 01:53 PM
//Functions
int quad(double a, double b, double c, double a1, &double &a2)


you have misplaced an "&".

mensch
October 16th, 2005, 12:12 AM
ah thanks. That actually wasnt the main problem, but i figured it out. I just had to rename my quad function. It was predefined.