How to use sets of" if-else " correctly in C?
I am having a problem in this code in DevC++, because in my view the part of the conditions of if-else
is perfectly indented and organized (all if
has its else
and its keys). The error is in the last else
, but I don't know how to solve as I need this condition.
The purpose of the program is to classify the 3 values inserted a triangle, and if they form, classify into equilateral, isosceles and scalene. However, if they do not form a triangle, issue a message. It is in this part that the problem lies, as the program does not compile if I enter 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;
}
1 answers
Is not perfect indented and is not well organized, it is not so easy to read and this is a difficulty. There is a typo where it has a print()
when in fact it should be printf()
. Here's how it gets easier 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.");
}
See working on ideone. E no repl.it. also I put on GitHub for future reference .