1. 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)) )
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. You should put this -

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

before int main().

3. 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)) )
{
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. 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)) )
{
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

5. >> 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

6. 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)) )
{
}
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. 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;
}
}```

Popular pages Recent additions