displaying problem

This is a discussion on displaying problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I have the following problem ( triangle of sides a,b,c) Write a void function that uses five parameters: 3 ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    39

    displaying problem

    Hi,
    I have the following problem ( triangle of sides a,b,c)

    Write a void function that uses five parameters: 3 values are the length of the edges of the triangle, and 2 are reference parameter that compute the area and perimeter.
    Make the function robust.
    Note that not all combination of a,b,c produce a triangle.
    the function should produce correct results for legal data and reasonable results for illegal combination.


    This is what I wrote below.
    The problem I have is that when I enter bad combination for the edges of the triangles, instead of just displaying "bad combination, it continues and compute the area and perimeter (even though the data are illegal.

    Code:
    #include<iostream>
    #include<cmath>
    
    using namespace std;
    
    int main() //driver program
    {
    double area,perimeter;
    int finish,a,b,c;
    void compute_area(double&,double&,double,double,double);
    a=8;
    b=8;
    c=30;
    compute_area(area,perimeter,a,b,c);
    
    cout << "The area of a triangle with sides of length " << a
            << " , " << b << " and " << c << " is " << area <<
            "and the  perimeter is "<<perimeter<< endl;
    
    
    cin>>finish;// to keep output window open
    
    return 0;
    }//end main
    
    
    // function
    
    void compute_area (double& area, double& perimeter, double a, double b, double c) {
       double s;
        
        if( (a<(b+c))||(b<(a+c))||(c<(b+a)) )
        { cout<<"bad combination!"<<endl;
          return;
        }
       else
       {
       s = (a + b + c)/ 2.0;// semiperimeter
       area = sqrt (s*(s-a)*(s-b)*(s-c)); // area triangle
       perimeter=a+b+c;
       }
       return;
    }
    Thank you.
    B

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    You should put this -

    void compute_area(double&,double&,double,double,double) ;

    before int main().

  3. #3
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    You shouldn't print the result if there's an error. Maybe return a bool and only print if it's true?
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    // the way you had it was okay, but this is probably better
    bool compute_area(double&, double&, double, double, double);
    
    int main()
    {
        double area,perimeter;
    
        // always initialize if you can
        int a = 8;
        int b = 8;
        int c = 30;
    
        if (compute_area(area,perimeter,a,b,c))
        {
            cout << "The area of a triangle with sides of length " << a
                << " , " << b << " and " << c << " is " << area <<
                "and the  perimeter is "<<perimeter<< endl;
        }
    
        cin.get(); // better way to keep the window open
    
        return 0;
    }
    
    bool compute_area (double& area, double& perimeter, double a, double b, double c)
    {
        double s;
    
        if( (a<(b+c))||(b<(a+c))||(c<(b+a)) )
        {
            cout<<"bad combination!"<<endl;
            return false;
        }
        else
        {
            s = (a + b + c)/ 2.0;// semiperimeter
            area = sqrt (s*(s-a)*(s-b)*(s-c)); // area triangle
            perimeter=a+b+c;
        }
    
        return true;
    }

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote Originally Posted by Hikaru
    You shouldn't print the result if there's an error. Maybe return a bool and only print if it's true?
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    // the way you had it was okay, but this is probably better
    bool compute_area(double&, double&, double, double, double);
    
    int main()
    {
        double area,perimeter;
    
        // always initialize if you can
        int a = 8;
        int b = 8;
        int c = 30;
    
        if (compute_area(area,perimeter,a,b,c))
        {
            cout << "The area of a triangle with sides of length " << a
                << " , " << b << " and " << c << " is " << area <<
                "and the  perimeter is "<<perimeter<< endl;
        }
    
        cin.get(); // better way to keep the window open
    
        return 0;
    }
    
    bool compute_area (double& area, double& perimeter, double a, double b, double c)
    {
        double s;
    
        if( (a<(b+c))||(b<(a+c))||(c<(b+a)) )
        {
            cout<<"bad combination!"<<endl;
            return false;
        }
        else
        {
            s = (a + b + c)/ 2.0;// semiperimeter
            area = sqrt (s*(s-a)*(s-b)*(s-c)); // area triangle
            perimeter=a+b+c;
        }
    
        return true;
    }
    Yes I thought about that but the function must be a VOID type !


    B
    Last edited by braddy; 09-09-2006 at 06:04 PM.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> Yes I thought about that but the function must be a VOID type !

    That's a bad restriction! It doesn't "Make the function robust", does it?
    Set the area and the perimiter values to -1 when it fails.

    Code:
    compute_area( area, perimeter, a, b, c );
    
    if ( (area == -1) || (perimiter == -1) ) // Technically && but whatever
    {
    	std::cout<< "ERROR!";
    }
    else
    {
    	std::cout<< "OK!!";
    }
    But, IMO a bool would be better
    Last edited by twomers; 09-09-2006 at 06:11 PM.

  6. #6
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Yes I thought about that but the function must be a VOID type !
    Ah, gomen. A better way would throw an exception.
    Code:
    #include <iostream>
    #include <stdexcept>
    #include <cmath>
    
    using namespace std;
    
    // the way you had it was okay, but this is probably better
    void compute_area(double&, double&, double, double, double);
    
    int main()
    {
        double area,perimeter;
    
        // always initialize if you can
        int a = 8;
        int b = 8;
        int c = 30;
    
        try
        {
            compute_area(area,perimeter,a,b,c);
            cout << "The area of a triangle with sides of length " << a
                << " , " << b << " and " << c << " is " << area <<
                "and the  perimeter is "<<perimeter<< endl;
        }
        catch (const std::invalid_argument& e)
        {
            std::cerr << e.what() << '\n';
        }
    
        cin.get(); // better way to keep the window open
    
        return 0;
    }
    
    void compute_area (double& area, double& perimeter, double a, double b, double c)
    {
        double s;
    
        if( (a<(b+c))||(b<(a+c))||(c<(b+a)) )
        {
            throw std::invalid_argument("Bad combination");
        }
        else
        {
            s = (a + b + c)/ 2.0;// semiperimeter
            area = sqrt (s*(s-a)*(s-b)*(s-c)); // area triangle
            perimeter=a+b+c;
        }
    }
    That's probably not allowed. The only other way I can think of is a global variable to flag errors. It's a bad option though. I only mention it because it seems like your teacher is forcing that kind of error handling method.
    Code:
    #include <iostream>
    #include <stdexcept>
    #include <cmath>
    
    using namespace std;
    
    namespace
    {
        bool error = false;
    }
    
    // the way you had it was okay, but this is probably better
    void compute_area(double&, double&, double, double, double);
    
    int main()
    {
        double area,perimeter;
    
        // always initialize if you can
        int a = 8;
        int b = 8;
        int c = 30;
    
        compute_area(area,perimeter,a,b,c);
    
        if (!error)
        {
            cout << "The area of a triangle with sides of length " << a
                << " , " << b << " and " << c << " is " << area <<
                "and the  perimeter is "<<perimeter<< endl;
        }
    
        cin.get(); // better way to keep the window open
    
        return 0;
    }
    
    void compute_area (double& area, double& perimeter, double a, double b, double c)
    {
        double s;
    
        if( (a<(b+c))||(b<(a+c))||(c<(b+a)) )
        {
            std::cerr << "Bad combination\n";
            error = true;
        }
        else
        {
            s = (a + b + c)/ 2.0;// semiperimeter
            area = sqrt (s*(s-a)*(s-b)*(s-c)); // area triangle
            perimeter=a+b+c;
        }
    }

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Or my idea (post #5), with the -1's with this function -

    Code:
    void compute_area (double& area, double& perimeter, double a, double b, double c)
    {
        if( (a<(b+c)) || (b<(a+c)) || (c<(b+a)) )
        {
            area = -1;
            perimiter = -1;
        }
        else
        {
            double s;
    
            s    = (a + b + c)/ 2.0; // semiperimeter
            area = sqrt (s*(s-a)*(s-b)*(s-c)); // area triangle        perimeter=a+b+c;
        }
    }
    Last edited by twomers; 09-09-2006 at 06:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in displaying records in a file
    By logicwonder in forum C Programming
    Replies: 9
    Last Post: 11-30-2005, 12:13 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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