closed I cannot figure out how to get my program to return a value of -1.

0 votes
asked Mar 7 by WillPope (190 points)
closed Mar 7 by WillPope
Would someone please help me figure out how to get my compiler to return a value of -1 for this function?

#include <iostream>
#include <cmath>
using namespace std;

double TriangleArea( double sideA, double sideB, double sideC )
{
    double S, area;
    S = (sideA + sideB + sideC) / 2.0;
    // Function to calculate the area of a triangle
    area = ( S * (S - sideA) * (S - sideB) * (S - sideC) );
    
    if(S > 0)
    {
        area = sqrt( area );
    }
        else
        {
            area = -1;
        }

    return area;
}

int main()
{
    double S, area, sideA, sideB, sideC;
    
    // Enter the sides of the triangle.
    cout << "Enter the sides of the triangle: \n";
    cin >> sideA >> sideB >> sideC;  
    
    // calling back The TriangleArea function
    area = TriangleArea( sideA, sideB, sideC );
    if(area == -1)
    {
        cout << "Triangle cannot be formed with given sides" << endl;
    }
    else
    {
        cout << "\nArea of triangle = " << TriangleArea( sideA, sideB, sideC ) << endl;
    }
    return 0;
}
closed with the note: figured the biotch out

1 Answer

–1 vote
answered Mar 7 by wyattbiker (240 points)
Obviously putting in junk values will do it.

E.g.

Enter the sides of the triangle:                                                                                                  

-2                                                                                                                                

-4                                                                                                                                

0                                                                                                                                 

Triangle cannot be formed with given sides
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...