0

I want to code a program that can calculate the area of a triangle based on the user's input, I am Brazilian so the print is in Portugues, however i think that it won't change anynthing. So my problem is the first "if" runs perfect, but the second "if" only prints the first line (Voce tem o valor dos 3 lados? (s ou n): ) and the program stop running, i can't find where my mistake is if someone can help me it would be good, and if the portuguese is making it more difficult let me know and I translate it. thanks.

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


void triangulo(){

int angulo;
float a, b, c, s, area, base, altura, seno, rad;
char resposta1, resposta2;


printf("Voce tem os valores da base e da altura? (s ou n):  ");
scanf("%c", &resposta1);

if (resposta1 == 's'){

   printf("Digite a base do triangulo : ");
   scanf("%f", &base);


   printf("Digite a altura do triangulo : ");
   scanf("%f", &altura);

   area = (base * altura)/2;

   printf("A area do triangulo eh : %f", area);

}

else{

  if(resposta1 == 'n'){

        printf("Voce tem o valor dos 3 lados? (s ou n): ");
        scanf("%c", &resposta2);

        if(resposta2 == 's'){


        printf("Digite o valor do lado : ");
        scanf("%f", &a);

        printf("Digite o valor do lado : ");
        scanf("%f", &b);

        printf("Digite o valor do lado : ");
        scanf("%f", &c);

        s = (a + b + c)/2;

        area = sqrt(s * (s - a) * (s - b) * (s - c));

        printf("A area do triangulo eh : %f", area);
   }


    else{   
       if(resposta2 == 'n'){

         printf("Digite o lado : ");
         scanf("%f", &a);

         printf("Digite o outro lado : ");
         scanf("%f", &b);

         printf("Digite o angulo entre esses dois lados : ");
         scanf("%d", &angulo);

         rad = 3.14159265358979323846/180;

         seno = sin(rad * angulo);

         area = (a * b * seno)/2;

         printf("A area do triangulo eh : %f", area);
 }


}
   }
      }




 return;
        }

int main (void){

triangulo();


return 0;
 }
  • 2
    Please read this. http://c-faq.com/stdio/scanfprobs.html – Rod Talingting Jul 09 '20 at 01:49
  • 3
    ⟼This code could benefit greatly by adopting an [indentation style](https://en.wikipedia.org/wiki/Indentation_style) and applying it consistently. Indentation conveys structure and intent which makes it easier for us to understand your code without having to invest a lot of time deciphering it, and it can also make mistakes more obvious as they stand out visually. – tadman Jul 09 '20 at 01:51
  • I don't think that there's any problem with your `if-else` conditions. I guess there's this problem with the subsequent calls of `scanf`. You should check whether they are `scanning` right values or not, using this example: `int test = scanf("%d", &b);`. And I would suggest you to use `switch-cases` in this context. – Shubham Jul 09 '20 at 01:57
  • 1
    Thanks guys i changed the second scanf("%c") for %s, and i reorganized my code and it worked, thanks – Rodrigo Vernizzi Jul 09 '20 at 02:02

3 Answers3

0

For a quick fix, change to scanf(" %c", &resposta2);, with a whitespace before the %c.

For a more complete response, check How to do scanf for single char in C

felipeek
  • 1,193
  • 2
  • 10
  • 31
0

The problem is in the scanf function, you are trying to get a character from the standard input but if you answer with n and press ENTER the standard input will buffer the \n character (when you pressed ENTER) thus skipping the second if when the answer is 'n'. what you can do is add the \n character to your scanf function like this scanf("%c\n", &resposta1);

Another solution is to put a getchar() after the scanf function to read that extra \n character.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
corlo
  • 19
  • 3
-1

I changed the second scanf("%c") for %s and it worked