triangle determine if it is isosceles, scalene or equilateral

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erljan
    New Member
    • Aug 2012
    • 2

    triangle determine if it is isosceles, scalene or equilateral

    Code:
    #include<stdio.h>
    
    main()
    {
        float sideA,sideB,sideC;
        int num1 ;
        float eq;
    
    printf("Enter sideA of the triangle:");
    scanf("%s",&sideA);
    printf("Enter sideB of the triangle:");
    scanf("%s",&sideB);
    printf("Enter sideC of the triangle:");
    scanf("&s",&sideC);
     
    
    
    
        if(sideA==sideB && sideB==sideC)
        {
        (eq=Equilateral);
        }
        if(sideA==sideB || sideB==sideC || sideA==sideC)
        {
        (eq=Isosceles);
        }
        if(sideA!=sideB && sideB!=sideC)
        {
        (eq=Scalene);
     
    printf("The triangle is: ",eq);
       
    getche();
    }
    Last edited by Rabbit; Aug 30 '12, 05:34 PM. Reason: Please use code tags when posting code.
  • erljan
    New Member
    • Aug 2012
    • 2

    #2
    tell me whats wrong about my code... pls help....

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      scanf("%s",&sid eA);

      %s is string format specifier. for float, use "%f" or for double, "%lf"

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Please use code tags when posting code.

        1) Unless Equilateral, Isosceles, and Scalene are constants/variables defined and assigned elsewhere, they have no meaning in of itself.

        2) eq is a float, I suspect you actually want to assign a string to that.

        3) Your printf passes one value but you have no placeholder for it in your code.

        4) I have no idea what getche() is.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          You have a sequence of three if statements. This should be replaced by an if, else-if, else sequence. Notice that both the Equalateral and Isosceles conditions are true for an equalateral triangle. No need to examine the sides again to determine if the triangle is scalene -- it must be scalene if it isn't equilateral or isosceles.

          It is never a good idea to compare floating point numbers for strict equality or inequality (don't use "==" or "!="). Take a look at what every computer scientist should know about floating-point arithmetic.

          In the future, please do more than simply ask us to tell you what's wrong with your code. Tell us why you think something is wrong (compiler error, runtime error, wrong answer, etc). Those details will typically make it much easier to help you.

          Comment

          • divideby0
            New Member
            • May 2012
            • 131

            #6
            I'd suspect the main problem is not getting input properly stored. You can test the logic by hard coding sideA, sideB, and sideC; if you get expected results, then work on the UI. If not, then work on the logic first.

            scanf stinks... check its return status before trusting its data. the function returns the number of items successfully stored; beware of the stray newline char left in the input buffer. just make sure you use the correct format specifier with the desired variable.

            Code:
            float sideA;
            
            if((scanf("%f", &sideA)) == 1)
               // input successfully stored

            Comment

            Working...