How do I write a function that receives three integer inputs for the lengths of the sides of a triangle using C++?

  How do I write a function that receives three integer inputs for the lengths of the sides of a triangle and returns one of four values to determine the triangle type (1=scalene, 2=isosceles, 3=equilateral, 4=error) using C++?  
   I followed a thread wherein a member posted a question similar to mine; however, the solution was provided using Perl.  And no, this is not homework-related as I have been out of school for many years.  This is what I have come up with so far...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Check code snippet

int CheckTri(int a, int b, int c){
     if(a == b && b == c){
          return 3;
     if(a == b || a == c || b == c){
          return 2;
     //I guess that what you mean by error is if it doesn't form a triangle. Not sure how to check that yet though
     return 1;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
what means the 4? What kind of error do you need to check?
evilrixSenior Software Engineer (Avast)Commented:
As far as I remember (it's been a long time since I was at school) it is not possible to make a triangle if the sum of any 2 sides is less than the 3rd so something like below should allow you to test for error.

if (((a + b) < c) | ((b + c) < a) | ((a + c) < b)) {  return 4; }

NB. I have not tested nor proven this formula works so caveat emptor :)
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

>> What kind of error do you need to check?

Probably if one of the sides has a negative or 0 length ...

Expanding on trigger-happy's code :

#include <cmath>
typedef enum {
  TriangleType_scalene = 1,
  TriangleType_isosceles = 2,
  TriangleType_equilateral = 3,
  TriangleType_error = 4
} TriangleType;
inline int cmpDouble(double &d1, double &d2) {
  if (fabs(d1 - d2) < 0.00001) return 0;
  if (d1 < d2) return -1;
  else return 1;
TriangleType checkTriangle(double a, double b, double c) {
    if ((a <= 0.0) || (b <= 0.0) || (c <= 0.0)) {
        return TriangleType_error;
    if (!cmpDouble(a, b) && !cmpDouble(b, c)) {
        return TriangleType_equilateral;
    if (!cmpDouble(a, b) || !cmpDouble(b, c) || !cmpDouble(a, c)) {
        return TriangleType_isosceles;
    return TriangleType_scalene;

Open in new window

>> if the sum of any 2 sides is less than the 3rd

Yep, that sounds like an error to me too ;) You can easily add that to the code heh.
you can also consider they are all positive;

if  ( (a<0) | (b<0) | (c<0) ) {
   return 4;

or to be really crazy

int max = (a>b) ? ( (a>c) ? a : c ) : ( (b>c) ? b : c);
int min = (a<b) ? ( (a<c) ? a : c ) : ( (b<c) ? b : c);

so your error can be

if (min < 0 | 2*max>a+b+c) {
   return 4;

I can also get you nicier expressions, but I steel don't know what he considers a error.
LYRiversAuthor Commented:
Thanks for the responses, everyone.  The error condition is that if it isn't one of the 3 types of triangles listed, then it isn't a triangle; hence, the output  is "error".  Thanks.
LYRiversAuthor Commented:
I took bits from all of the solutions posted and thus, had to divide the total points.  Thanks again, everyone.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.