Triangle

This is a discussion on Triangle within the C++ Programming forums, part of the General Programming Boards category; Program is supposed to get 3 sides of a tri, list them in nondecreasing order, list the type of triangle. ...

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

    Triangle

    Program is supposed to get 3 sides of a tri, list them in nondecreasing order, list the type of triangle. It works like a beauty sometimes, but for others, the second input turns into garbage.

    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 isSca(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 a,b,c;
      float x,y,z;
    //Inputs
     cout << " Please enter the triangle lengths: "<<endl;
     cout << " Length 1: ";
     cin >> a;
     cout << " Length 2: ";
     cin >> b;
     cout << " Length 3: ";
     cin >> c;
    //Order
     
     
      if (c>=a && a<=b)
        x = a;
      if (c>=b && b<=a)
        x = b;
      if (b>=c && c<=a)
        x = c;
      if (a<=b && b<=c)
        y = b;
      if (a<=c && c<=b)
        y = c;
      if (c<=a && b<=a)
        y = a;
      if (a>=b && a>=c)
        z = a;
      if (b>=c && b>=a)
        z = b;
      if (c>=b && c>=a)
        z = c;
     
     cout << "Lengths in nondecreasing order: "<<x<<", "<<y<<", "<<z<<endl;
     
    //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 (isSca(x,y,z))
        {
         cout << "Scalene ";
        }
       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;
     }
    //isSca
    bool isSca(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
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by howeezy
    Program is supposed to get 3 sides of a tri, list them in nondecreasing order, list the type of triangle. It works like a beauty sometimes, but for others, the second input turns into garbage.
    So? What is the problem?

  3. #3
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    Quote Originally Posted by howeezy
    , but for others, the second input turns into garbage.
    that

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    The way you have it setup, x,y and z are not always necessarily set as something in all your if statements, so the memory space is left blank and whatever happened to be there before is used.

    You can either initialize them as say 0 or 1 when you declare them, or put in an else statement to initialize them to something else.

    But, the method you are using when you are currently getting garbage you will get the wrong answer.




    I am not even sure why you are using the if statements to set x,y,z as the side lengths when the user has already input them?
    Last edited by Enahs; 10-10-2005 at 08:27 AM.

  5. #5
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    The assignment requires me to list them in nondecreasing order.

  6. #6
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    why are they not set??? what am i missing?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> but for others, the second input turns into garbage.

    What others? What input do you give? What output do you get?

  8. #8
    Your Father
    Join Date
    Sep 2005
    Posts
    34
    an example is attached
    Attached Images Attached Images  

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 01:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  3. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 05:40 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. Determining a Triangle using get and pointer
    By naynay in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 05:55 AM

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