none
C# Programming Loops RRS feed

  • Question

  • Write an application that can be used to determine if three line segments can form a triangle. Prompt the user for the length of three line segments as integers. If non-numeric characters are entered, re-prompt the user for new values. If the three lines could form a triangle, print the integers and a message indicating that they form a triangle. Use a state-controlled loop to allow users to enter as many different combinations as they want.

    A property that applies to triangles:
     *   The sum of the lengths of any two sides of a triangle is greater than the length of the third side.
     *   If you take the three sides of a triangle and add them in pairs, 
     *   the sum should be greater than the third side. i.e. 3-4-5  or  5-8-12
     *
     *  If that is not true, then it is not possible to construct a triangle with the given side lengths.



    Monday, July 8, 2019 3:39 PM

All replies

  • This is what i have so far....

    using System;
    using System.Windows.Forms;
    using static System.Console;

    namespace Triangle_Class
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool result;
                bool inputMoreValues = true;
                int first, second, third;

                DisplayIntro();
                Write("Is It a Triangle Application?\n\n");

                do
                {


                    first = GetNumber("first");
                    second = GetNumber("second");
                    third = GetNumber("third");

                    result = DetermineIfTriangle(first, second, third);
                    Write(ProduceMessage(result, first, second, third));
                    GetInput(ref inputMoreValues);
                } while (inputMoreValues);

                WriteLine("\n\n");
            }
            static void DisplayIntro()
            {
                WriteLine("\n\n\nThis Application allows you to enter"
                                 + "\nintegers and determines if "
                                 + "the numbers form a triangle\n");
                ReadKey();
            }
            static int GetNumber(string whichOne)
            {
                string inValue;
                int aNumber;
                Write("\nPlease enter the integer:", whichOne);
                inValue = ReadLine();
                while (int.TryParse(inValue, out aNumber) == false)
                {
                    Write("\n INVALID DATA ENTERED -RE-ENTER INTEGER", whichOne);
                    inValue = ReadLine();
                }
                return aNumber;
            }
            static bool DetermineIfTriangle(int num1, int num2, int num3)
            {
                bool isItTriangle = true;

                if (num1 > num2)
                {
                    if (num1 > num3)
                    {
                        isItTriangle = ((num2 + num3) > num1) ? true : false;
                    }
                }
                else
                {
                    if (num2 > num3)
                    {
                        isItTriangle = ((num1 + num2) > num3) ? true : false;
                    }

                    else
                    {
                        if (num2 > num3)
                        {
                            isItTriangle = ((num1 + num3) > num2) ? true : false;
                        }


                        else
                        {
                            isItTriangle = ((num1 + num2) > num3) ? true : false;
                        }


                    }

                }
                return isItTriangle;
            }
            static string ProduceMessage(bool isTriangle, int num1, int num2, int num3)
            {
                string result = "\n\nMy Message";

                return result;
            }
            static void GetInput(ref bool choice)
            {

                bool moreData = true;
                while (moreData)
                {      
                    if (MessageBox.Show("Do you want another number ?",
                                 "State Controlled Loop", MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Question) == DialogResult.No)
                   
                    {
                        moreData = false;
                    }
                }
            }
        }
    }

     

    I need a message that determines if inputs are a triangle

    Monday, July 8, 2019 3:41 PM
  • This is a homework assignment so nobody in the forums will provide the answer to this question. We will guide you on what to do next though.

    I assume the message you're talking about is inside `ProduceMessage`. In that case you can call your `DetermineIfTriangle` method (there is no reason to pass that as a parameter to the function). If it returns true then display your message. It is unclear what type of app you're building but it seems like you're building a console app. Therefore that reference to `System.Windows.Forms` and the call to `MessageBox.Show` will do nothing. Remove them.

    To display something on the screen use `Console.WriteLine`.

    Console.WriteLine("a message");
    I notice that you use a `using static` declaration at the top of your file for Console. This is non-standard. Remove that line and simple call it like you normally would. You are gaining nothing but confusion by going that route.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, July 8, 2019 6:11 PM
    Moderator
  • You have misunderstood the requirements for being a triangle.  Your code is checking the LARGEST side against the other two (well, you tried but did it incorrectly), but the requirement is that ALL of the combinations must meet that requirement.  That is, A+B > C, and  A+C > B, and B+C > A.  Your isItTriangle function can be written in one line.

    And let me point out that this:

        isItTriange = ((num1 + num3) > num2) ? true : false;

    is exactly the same as:

        isItTriangle = (num1 + num3) > num2;

    And shouldn't your ProduceMessage function be using the value of isTriangle to decide what to print?


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, July 8, 2019 9:58 PM
  • Therefore that reference to `System.Windows.Forms` and the call to `MessageBox.Show` will do nothing. Remove them.

    Actually if we add a reference for System.Windows.Forms then MessageBox works in a console application. I assume the instructor provided the instructions for doing that.

    I notice that you use a `using static` declaration at the top of your file for Console. This is non-standard. Remove that line and simple call it like you normally would. You are gaining nothing but confusion by going that route.

    That seems to be something else the instructor has instructed them to do. The "using static System.Console;" allows the "Console." to be left off. It is probably standard for the instructor.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, July 9, 2019 6:31 PM