Why is this not working?

This is a discussion on Why is this not working? within the C++ Programming forums, part of the General Programming Boards category; i wrote this program..sweat all over it...but its not working..can any one help me figure my mistake? Code: #include <string> ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Unhappy Why is this not working?

    i wrote this program..sweat all over it...but its not working..can any one help me figure my mistake?

    Code:
    #include <string>
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int QuadRoots ( double a, double b, double c, double &r1, double &r2);
    
    int main() 
    {
        int result = 0;
        double a, b, c;
        double r1,r2;
    	
    	cout << "Quadratic coefficients: ";
        cin  >> a  >> b  >> c;
        
    	result = QuadRoots(a, b, c, r1, r2);
        if (result == 0)
        {
             cout << "Roots: " << "r1= " << " " << "r2= "
    			  << endl;
        }
        else if(result == 1)
        {
             cout << "One of the root is negative";
        }
        else if(result == 2)
        {
             cout << "One of the root is 0";
        }
        return 0;
    }
    
    int QuadRoots ( double a, double b, double c, double &r1, double &r2)
    {
        int result = 0;
        
        if(a == 0)
        {
             result = 2;
        }
        if((b*b - 4*a*c) < 0)
        {
             result = 1;
        }
        if ((a != 0) && ((b*b - 4*a*c) > 0)) 
        {
             double radical = sqrt(b*b - 4*a*c);
             &r1 = (-b + radical) / (2*a);
             &r2 = (-b - radical) / (2*a);
             result = 0;
        }
        return result;
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What do you mean it isn't working? Is it producing a compile error?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    &r1 = (-b + radical) / (2*a);
    &r2 = (-b - radical) / (2*a);
    you already passed the address when calling the function, so no need to set that equal to the address of the variable...take out the &'s and it should work fine.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    ok so when i take the & out..the value of r1 n r2 does not appear!

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
        if (result == 0)
        {
             cout << "Roots: " << "r1= " << " " << "r2= "
    			  << endl;
        }
    be nice if you actually outputed the variables

    edit: in case you dont know what i mean ....

    Code:
        if (result == 0)
        {
             cout << "Roots: " << "r1= " << r1 << " " << "r2= "
    			  << r2 << endl;
        }
    Last edited by Jamsan; 04-22-2003 at 11:23 AM.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Talking

    thanx..it compiles now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21