im wrote a program to determine weather a triangle is scalene, isosceles or equilateral based on the given side lengths...its not working properly..i.e its giving isoceles when i enter data for an equilateral...etc etc... i fugured it was my logic in writing the program that was missing something....could anyone tell me how to tighten this up so that it works properly?

#include <stdio.h>
main () {
double side1, side2, side3 ;
int abc;

printf ("enter first side : ");
scanf ("%lf", &side1);

printf ("enter second side : ");
scanf ("%lf", &side2);


printf ("enter third side : ");
scanf ("%lf", &side2);


if ((side1 == side2)&&(side2 == side3)&&(side3 == side1))
   printf ("Equilateral Triangle");

      else if ((side1==side2)||(side2==side3)||(side3==side1))
               printf ("Isosceles Triangle");
               
               else if ((side1 != side2)&&(side2 != side3)&&(side3 !=side1))
                 printf ("Scalene Triangle");
                 
 scanf ("%d", &abc);
                 


return (0);
}

the scanf at the end is to keep the program on the screen after it executes and displays the output..it usually does that (im using bloodshed's DevC++).
im guessing that there is something else i could put into the if statement's conditions, but im not sure at all... thanks in advance for your help

Turning on some compile warnings will give you a clue!

gcc dani.c -O -Wall -Wextra -pedantic -Wuninitialized
dani.c:2: warning: return type defaults to `int'
dani.c: In function `main':
dani.c:3: warning: 'side3' might be used uninitialized in this function

Edited 4 Years Ago by VernonDozier: n/a

the scanf at the end is to keep the program on the screen after it executes and displays the output..

Use getch() function defined in conio.h thereby you can avoid using scanf() to hold the output screen.

And before you proceed, you need to verify the sides belong to a valid triangle. If you remember from old school mathematics, the 3 given sides are the sides of a triangle only when sum of any two sides is greater than the 3rd side. 1, 2, 5 can never be the lengths of sides of a triangle.

Moreover there is no need for the last "else if". A simple "else" is enough.

The error that you made in your code, is clearly shown in the warning from the previous post.

thanks for your help. i need to learn to read over my work before i start asking for help

Use getch() function defined in conio.h thereby you can avoid using scanf() to hold the output screen.

Very bad suggestion. getch() is
1) not portable
2) not Standard C
3) defined in very few compilers
The appropriate function to use is getchar() because it is
1) portable
2) Standard C
3) defined in all compilers

But his this case neither solution will appear to work since the NEWLINE from the last input is still in the buffer waiting to be read.

This article has been dead for over six months. Start a new discussion instead.