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;
}