Thread: Having trouble with a C program

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    4

    Having trouble with a C program

    I am trying to write a c program which you input 3 sides of a triangle and it tells you if it is a valid triangle of not and what kind of triangle it is, it compiles and runs without errors but the only problem is that is always tells me the triangle is isoscles.

    Code:
    #include <stdio.h>
    int main()
    {
    	int a, b, c;
    	
    	printf("Please enter side a ");
    	scanf("%d%*c", &a);
    	
    	printf("Please enter side b :");
    	scanf("%d%*c", &b);
    	
    	printf("Please enter side c :");
    	scanf("%d%*c", &c);
    	
    	if(a + b > c) {
    		printf("This is a valid "); 
    	if(a == b == c)
    		printf("Equalateral Triangle");
    	else
    		if(a == b != c)
    			printf("Isoscles Triangle");
    		else
    			if(a == c != b)
    				printf("Isoscles Triangle");
    			else
    				if(a != b == c)
    					printf("Isoscles Triangle");
    				else
    				if	(a != b != c)
    						printf("Scalene Triangle");
    	}
    	else
    		printf("Triangle is not valid");
    	return(0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	if(a == b == c)
    The value of boolean operators, such as == is always true or false. So what happens is the "a == b" part turns into either true or false, then true or false is compared with c, at which point, the only way c is false is if c equals zero.

    The rest of your checks are broken in the same manner.
    Code:
    if( a == b && b == c )
    Says: if the first test is true, and the second test is true...


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with program
    By kblanch09 in forum C Programming
    Replies: 3
    Last Post: 03-31-2010, 03:30 PM
  2. My first program trouble
    By pal1ndr0me in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2006, 12:09 AM
  3. Having trouble with program
    By Bonedaddy in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 06:14 PM
  4. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM
  5. big trouble in little program
    By jonesy in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 02:04 PM