CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2004
    Posts
    35

    Need help with quadratic formula function

    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!

  2. #2
    Join Date
    Sep 2005
    Location
    Cluj-Napoca, Romania
    Posts
    161

    Re: Need help with quadratic formula function

    Quote Originally Posted by mensch
    Code:
    //Functions
    int quad(double a, double b, double c, double a1, &double &a2)
    you have misplaced an "&".

  3. #3
    Join Date
    Dec 2004
    Posts
    35

    Re: Need help with quadratic formula function

    ah thanks. That actually wasnt the main problem, but i figured it out. I just had to rename my quad function. It was predefined.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured