Thread: Triangle Question

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    43

    Triangle Question

    Hello, I'm currently working on a program that reads in 3 numbers then tells you what triangle it is or if its invalid

    I'm pretty much nearly finished except I need help on 2 things

    Code:
    #include <stdio.h>
    
    int main()
    {
        int side1 = 0;
    	int side2 = 0;
    	int side3 = 0;
    
        while(side1 != 'q')
        {
        printf("Please input the length of the Triangle Sides\n");
        printf("Side A: ");
        scanf("%d", &side1);
        printf("Side B: ");
        scanf("%d", &side2);
        printf("Side C: ");
        scanf("%d", &side3);
    
    			if (side1>side2){
    				if (side2==side3)
    				printf("Your triangle is Isoceles.\n");
    			}
    			if (side1 == side2){
    				if (side1 == side3)
    				printf("Your triangle is Equilateral.\n");
    			}
    			if (side1 != side2){
    				if (side2 != side3)
    				printf("Your triangle is Scalene.\n");
    			}
                else
                printf("Not a valid Triangle!\n");
        }
        return(0);
    }

    1) Whenever I input three equal numbers the message comes up "Your Triangle is Equilateral" but then on the next line it says "Not a valid Triangle".

    This doesn't happen for the others though...

    2) Is possible to make it the program loop forever until the user inputs 'q' at any time? At the moment it only quits when the user inputs q for side1


    Thanks for any advice!
    Last edited by DJ_Steve; 09-23-2009 at 01:13 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you only plan on checking one thing, you need an else statement for every if.
    Code:
    if X
    else
    if Y
    else
    if Z
    else...

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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    2) Yes. It is possible. All you need to do is incorporate some logic for looping until the user wants to quit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    Ok, I just put if statements for 'press q at any time' but im still having the same problem with an Equal Triangle

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    5
    The reason it is not working is because any else is related to its nearest if.So the else in you program is related to the last if statement and since it bacame false the else executed.You should create an else-if chain to work this properly.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int side1 = 0;
    	int side2 = 0;
    	int side3 = 0;
    	
        while(side1 != 'q')
        {
        printf("Please input the length of the Triangle Sides\n");
        printf("Side A: ");
        scanf("%d", &side1);
        printf("Side B: ");
        scanf("%d", &side2);
        printf("Side C: ");
        scanf("%d", &side3);
    
    			if (side1>side2){
    				if (side2==side3)
    				printf("Your triangle is Isoceles.\n");
    			}
    			else if (side1 == side2){
    				if (side1 == side3)
    				printf("Your triangle is Equilateral.\n");
    			}
    			else if (side1 != side2){
    				if (side2 != side3)
    				printf("Your triangle is Scalene.\n");
    			}
                else
                    printf("Not a valid Triangle!\n");
        }
        return(0);
    }
    Also you are taking a char input for an integer variable to break the loop which make it an infinite loop.
    Last edited by aaa111; 09-25-2009 at 12:01 PM.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by DJ_Steve View Post
    Thanks for any advice!
    A , B , C are the sides
    Code:
    if((A + B + C) < __max(__max(A , B) , C) * 2) printf("Invalid Triangle\n");

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    ahh thanks for the help!

    Its all sorted now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. for loop question
    By neo505 in forum C Programming
    Replies: 6
    Last Post: 03-16-2003, 04:26 PM
  4. OpenGL Question...simple (triangle drawing stuff)
    By incognito in forum Game Programming
    Replies: 7
    Last Post: 03-15-2003, 08:47 PM
  5. triangle function problem
    By yusiye in forum C Programming
    Replies: 1
    Last Post: 08-11-2002, 11:54 PM