How to use "if-Else" sets correctly in C?

Asked

Viewed 575 times

1

I have a problem with this code in Devc++, because in my view the conditions part if-else is perfectly indented and organized (all if possesses its else and your keys). The error is in the last else, but I don’t know how to fix it, because I need this condition.

The objective of the program is to classify the 3 values inserted in a triangle, and to form, classify in equilateral, isosceles and scalene. However, if you do not form a triangle, send a message. This is where the problem lies, because the program does not compile if I insert this last condition.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, 
system("pause") or input loop */

int main(int argc, char *argv[]) {

int a, b, c;
char equi[] = "Triangulo equilatero.";
char isos[] = "Triangulo isosceles." ; 
char esc [] = "Triangulo escaleno."  ;

scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);

if ((a > 0) && (b > 0) && (c > 0) && (a<(b+c)) && (b<(a+c)) && (c<(a+b)))
{
    if( (a==b) && (b==c) )
    {
        printf("%s", equi);
    }else{
        if( (a==b) || (b==c) || (c==a) )
        {
            printf("%s", isos);
        }else{
            printf("%s", esc);
        }   
    }

/* O problema está aqui neste ultimo else abaixo, pois se eu retirar essa 
linha o programa compila. No entanto se eu deixar, dá um erro "id returned 1 
exit status".*/

}else{
    print("Nao e possivel formar um triangulo."); 
} 
return 0;
}
  • One tip I’d give you is to use Else if (){} for better code performance.

1 answer

4


Not perfect indented and not well organized, not so easy to read and this is a complicator. There is a typo where there is a print() when it should actually be printf(). See how easy it is to follow the code:

#include <stdio.h>
int main() {
    int a, b, c;
    scanf("%d", &a);
    scanf("%d", &b);
    scanf("%d", &c);
    if (a > 0 && b > 0 && c > 0 && a < b + c && b < a + c && c < a + b) {
        if (a == b && b == c) printf("Triangulo equilatero.");
        else printf((a == b || b == c || c == a) ? "Triangulo isosceles." : "Triangulo escaleno.");
    } else printf("Nao e possivel formar um triangulo."); 
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.