Stupid Logic Problem Need Outside Viewpoint

This is a discussion on Stupid Logic Problem Need Outside Viewpoint within the C++ Programming forums, part of the General Programming Boards category; Well basically I need to find a way to fix this problem I keep on having. Here is my code. ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    28

    Stupid Logic Problem Need Outside Viewpoint

    Well basically I need to find a way to fix this problem I keep on having.

    Here is my code.


    Code:
        Triangle calculateTriangleType(int &sOne,int &sTwo,int &sThree)
      {
        Triangle result;
    
        if(sOne + sTwo < sThree || sOne + sThree < sTwo || sTwo + sThree < sOne)
             result = NO_TRIANGLE;
    
        if(sOne == sTwo && sOne == sThree)
             result = EQUILATERAL;
    
        if(sOne == sTwo && sOne != sThree || sTwo == sThree && sTwo != sOne || sThree == sOne && sThree != sTwo)
            result = ISOCELES;
    
        if(sOne != sTwo && sOne != sThree)
            result = SCALENE;
    
         return result;
      }

    Here is my problem. At the way this is currently setup I have no issues with most things. However my problem arises when it should be identifying a No_Triangle as a Scalene. Also keep in mind for any triangle to be considered a triangle any two of its sides must be greater than the third.

    Here is an example of my various outputs and what problem I keep having.

    Code:
    Please enter a value for side 1 of your triangle:   1
    Please enter a value for side 2 of your triangle:   1
    Please enter a value for side 3 of your triangle:   1
    
    The lengths of the sides that you entered were:  1, 1, and 1.
    Equilateral!
    
    Would you like to continue (Y or N)?  y
    
    Please enter a value for side 1 of your triangle:   12
    Please enter a value for side 2 of your triangle:   12
    Please enter a value for side 3 of your triangle:   11
    
    The lengths of the sides that you entered were:  12, 12, and 11.
    isoceles!
    
    Would you like to continue (Y or N)?  y
    
    Please enter a value for side 1 of your triangle:   1
    Please enter a value for side 2 of your triangle:   2
    Please enter a value for side 3 of your triangle:   3
    
    The lengths of the sides that you entered were:  1, 2, and 3.
    Scalene!
    
    Would you like to continue (Y or N)?  y
    
    Please enter a value for side 1 of your triangle:   900
    Please enter a value for side 2 of your triangle:   1
    Please enter a value for side 3 of your triangle:   3
    
    The lengths of the sides that you entered were:  900, 1, and 3.
    Scalene!
    
    Would you like to continue (Y or N)?  y
    
    Please enter a value for side 1 of your triangle:   3
    Please enter a value for side 2 of your triangle:   1
    Please enter a value for side 3 of your triangle:   1
    
    The lengths of the sides that you entered were:  3, 1, and 1.
    Scalene!
    Note that the bolded ones should not be Scalene, but in fact should be No_Triangle.

    Not sure on the logic needed to make this happen.

    Any help would be greatly appreciated.

  2. #2
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    EDIT: See below.
    Last edited by Kybo_Ren; 03-03-2005 at 10:53 PM.

  3. #3
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Except that if any of theose are true, then no triangle exists. So it should stand at ||, and not &&. You could switch the less than to a greater than and use &&, but this way is fine.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Umm, have you tried using an if elsif else structure, instead of just all if's, as that is quite possibly the problem.

    Another posibility, just return what you determine, instead of putting it into result, that way once it finds the correct type, then it won't have a chance to check, and accedentily get changed.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  5. #5
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    By golly, you're right. How stupid of me.


    if(sOne + sTwo < sThree || sOne + sThree < sTwo || sTwo + sThree < sOne)
    Code:
    if((sOne + sTwo <= sThree) || (sOne + sThree <= sTwo) || (sTwo + sThree <= sOne))
    EDIT: Take notice that the last one that isn't bolded is not a triangle either. The sum of the lengths of two sides of a triangle must be greater than the length of the third.

    You could also place a return result; after every if statement if you wanted to.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by Kybo_Ren
    By golly, you're right. How stupid of me.




    Code:
    if((sOne + sTwo <= sThree) || (sOne + sThree <= sTwo) || (sTwo + sThree <= sOne))
    EDIT: Take notice that the last one that isn't bolded is not a triangle either. The sum of the lengths of two sides of a triangle must be greater than the length of the third.

    You could also place a return result; after every if statement if you wanted to.
    If he doesn't either use returns after every if, or change it to if/elsif structure, its going to keep getting the SCALENE result. look

    Code:
    if(sOne != sTwo && sOne != sThree)
            result = SCALENE;
    sOne was never equal in either of the test cases, which means this if would be taken, and result would be assigned SCALENE.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Josephus problem variant logic troubles
    By misterMatt in forum C++ Programming
    Replies: 0
    Last Post: 04-29-2009, 03:38 PM
  2. Stupid problem
    By Adrian20XX in forum C Programming
    Replies: 6
    Last Post: 08-23-2008, 09:16 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM
  5. Weird (stupid?) Problem: error C2448: '<Unknown>'
    By Dual in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2001, 03:28 AM

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