Identifying Triangles

This is a discussion on Identifying Triangles within the C++ Programming forums, part of the General Programming Boards category; This program is supposed to let the user input three numbers, rearrange them in increasing order, and identify the properties ...

  1. #1
    Your Father
    Join Date
    Sep 2005
    Posts
    34

    Identifying Triangles

    This program is supposed to let the user input three numbers, rearrange them in increasing order, and identify the properties of the triangle.

    For some reason the program works like a beauty, until it hits isRig. It doesn't display anything for isRig's answer. I'm boggled. I looked it over 5 or 6 times, but I'm just starting so I can't find anything. Or maybe im just blind.


    Thank you!

    Code:
    //Included Files
     #include <iostream>
     using namespace std;
    
    //Function Prototypes
     bool isTri(float, float, float);
     bool isIso(float, float, float);
     bool isEqu(float, float, float); 
     bool isAcu(float, float, float); 
     bool isRig(float, float, float); 
     bool isObt(float, float, float); 
    
    //Main Function
     int main() {
    //Variable Declarations
      float x,y,z;
    //Inputs
     cout << " Please enter the triangle lengths: "<<endl;
     cout << " Length 1: ";
     cin >> x;
     cout << " Length 2: ";
     cin >> y;
     cout << " Length 3: ";
     cin >> z;
    
     
    //Answers 
    cout<<"The triangle is: ";
     if (isTri(x,y,z))
      { 
       if (isIso(x,y,z))
        {
         cout << "Isoceles ";
        }
       if (isEqu(x,y,z))
        {
         cout << "Equilateral ";
        }
       if (isAcu(x,y,z))
        {
         cout << "Acute ";
        }
       if (isRig(x,y,z))
        {
         cout << "Right ";
        }
       if (isObt(x,y,z))
        {
         cout << "Obtuse ";
        }  
       }  
     else
      { 
       cout <<"illegal ";
      }
    cout<<endl;
    return 0; } 
     
    //isTriangle
    
    bool isTri(float x,float y,float z)
     { 
      if (x > 0 && ((x + y) > z)) 
         return true;
      else
         return false;
     }
    
    //isIso
    bool isIso(float x,float y,float z)
     { 
      if (x == y || y == z)
         return true;
      else
         return false;
     }
    
    //isEqu
    bool isEqu(float x,float y, float z)
     { 
      if (x == y && y == z) 
         return true;
      else
         return false;
     }
    //isAcu
    bool isAcu(float x,float y, float z)
     { 
      if ((x*x)+(y*y)>(z*z))
         return true;
      else 
         return false;
     }
    //isRig
    bool isRig(float x,float y, float z)
     { 
      if ((x+x)*(y*y) == (z*z))
         return true;
      else
         return false;
     }
    //isObt
    bool isObt(float x,float y,float z)
     { 
      if ((x+x)*(y*y) < (z*z))
         return true;
      else 
         return false;
      }

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    First of all, you're not rearranging them to increasing order. (I assume you mean that x should be the smallest, followed by y, and z.)
    Second, to check if it is a right triangle, you have
    Code:
    ((x+x)*(y*y) == (z*z))
    I think you want
    Code:
    ((x*x)+(y*y) == (z*z))
    Edit: OK so bold doesn't work so well with symbols. oops.

    Edit2: I apparantly can't type today, but thankfully Thantos is there to pick up the slack.
    Last edited by Decrypt; 10-08-2005 at 12:12 PM. Reason: well that didn't work
    There is a difference between tedious and difficult.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    if ((x+x)*(y*y) == (z*z))
    are you sure thats right?

    Edit: Beaten by Decrypt so I'll respond to him:
    I think you want *.
    Well he has the right number of * and + but just not in the right order.

  4. #4
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    Sorry, I forgot to say that I hadn't started the increasing order yet and I think you figured it out... lemme check

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > if ((x+x)*(y*y) == (z*z))
    1. go back to school and look up pythagoras
    It's if ((x*x)+(y*y) == (z*z))

    Your isObt() looks similarly broken

    2. Comparing floats for equality doesn't work - just one very tiny error will turn your otherwise apparent equality into inequality.

    For floats, you generally need an approximate equality
    Code:
    bool isApproxEqual ( double x, double y ) {
      return fabs(x - y) < 0.000001;
    }
    Then you say
    if ( isApproxEqual( x*x+y*y, z*z) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    yes! Ok the blind part was right... Now I need to figure out how to do the increasing order. My teacher tried to explain how to set x as y temporarily??? But it just confused me.

    nevermind.. i think i figured it out
    Last edited by howeezy; 10-08-2005 at 12:18 PM. Reason: im an idiot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2009, 10:13 AM
  2. Normals to triangles
    By disruptivetech in forum Game Programming
    Replies: 5
    Last Post: 04-30-2008, 07:32 PM
  3. Drawing triangles in C#
    By John_L in forum C# Programming
    Replies: 2
    Last Post: 03-15-2008, 08:48 AM
  4. identifying first letter of word
    By speedyboy in forum C Programming
    Replies: 11
    Last Post: 03-19-2006, 06:56 AM
  5. Equilateral Right Triangles
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-06-2002, 02:32 PM

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