Thread: Need help with quadratic formula functions

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    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!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    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;
            }

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int quad(double a, double b, double c, double a1, &double &a2)
    Eh?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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

    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. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Why the check would they want to do that?
    Because I am a total idiot and was not thinking! I blame liquor!

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    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?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    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. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    /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'
    this is saying that the file types.h already has declared an identifier called quad. Simply rename your function

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    Ah, that did it. Thanks very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic formula in c
    By cakestler in forum C Programming
    Replies: 2
    Last Post: 02-08-2009, 09:41 PM
  2. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  3. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  4. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  5. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM