Home > Software design >  Doesn't show a float that has a formula when I execute
Doesn't show a float that has a formula when I execute

Time:09-17

I have a formula for the imc float but when I execute, it show 0.0 (in bold in the execution at the end) I can't find my error. pls help!

#include <stdio.h>

int main(){

  float poids, taille, imc; // translate as weight, height, imc
  char reponse; // translate as response

do
{
  printf("\nTapez le poids en kg et la taille en metre "); // translate as Enter the weight in kg and the height in meter
  scanf("%f%f", &poids, &taille); 
  
  imc = poids / (taille * taille); // translate as imc = weight / (height * height)

  printf("L'usager pese %.1f kg, mesure %.2f metre et son imc est de %.1f\n ", poids, taille, imc); // translate as User weighs %.1f kg, measures %.2f meter and his BMI is %.1f

  if(imc < 18.5)

    printf("MAIGREUR, RISQUE ELEVE A ACCRU\n");

  else
    if(imc < 25)

      printf("POIDS NORMAL, RISQUE FAIBLE\n");

    else
      if(imc < 30)

        printf("EMBONPOINT, RISQUE ELEVE\n");

      else
        printf("OBESITE, RISQUE TRES ELEVE\n");

   printf("\nVoulez-vous continuer (o/n) ");
      scanf(" %c", &reponse);
    }while(reponse == 'o' || reponse == 'O');

    
  return 0;
}

Execution:

make -s  ./main

Tapez le poids en kg et la taille en metre 75 130 L'usager pese 75.0 kg, mesure 130.00 metre et son imc est de 0.0

MAIGREUR, RISQUE ELEVE A ACCRU

Voulez-vous continuer (o/n)

CodePudding user response:

Your code is behaving correctly and the formula is correct as well as far as I can tell.

It just so happens that people having a height of 130 meters and a weight of 75 kg can indeed be considered to be very skinny, thus having a very low BMI.

The result turns out to be something around 0.004, which you format as 0.0.

I guess you wanted to enter the height in centimeters, which would require an extra step to divide the height by 100. This leads to a result of roughly 44. Still not a very healthy, but it does qualify as an actual human being ;)

CodePudding user response:

Show your answer with more decimal places.

When your input is 75 180, the answer is 0.002315. Then when you write .1f it shows 0.0

Thus you can write:

printf("L'usager pese %.1f kg, mesure %.2f metre et son imc est de %f\n ", poids, taille, imc);

Or for example:

printf("L'usager pese %.1f kg, mesure %.2f metre et son imc est de %.4f\n ", poids, taille, imc);
  •  Tags:  
  • c
  • Related