Wrong result when calculating BMI

Asked

Viewed 166 times

2

I am doing an exercise with the C language to calculate the BMI, there is no error in the compiler but the result goes wrong, I have already looked at the code and I do not find the error at all.

The code is as follows::

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

int main()
{
    setlocale(LC_ALL,"PORTUGUESE");
    printf("Claculo de imc\n");
    //variaveis peso e altura
    double peso;
    double altura;
    printf("Por favor digite seu peso: ");
    scanf("%f",&peso);
    printf("por favor digite sua altura: ");
    scanf("%f",&altura);



  double imc;
  imc = peso/(altura *altura );
  printf("Seu imc é de %.2f",imc);



    system("pause");
    return 0;
}
  • 1

    Change printf formats from f for lf and use stitch instead of comma when reporting weight/height. I did this and it worked here.

  • 1

    @Leonardo do scanf, no? :-)

  • 1

    Yes, both (printf and scanf).

  • 1

    Or change the variables to float, but ai loses in accuracy.

  • 1

    You have to consider the sex of the person tbm and make the adjustment in the formula.

  • A micro optimization would declare all variables in one line

  • @Marceloboni, which won’t make any difference. I think you already have an answer for this, but how can I not find the duplicate, if no one can find who will answer? Leonardo? I didn’t even question other small problems in code that’s more style.

Show 2 more comments

1 answer

4


The problem was that you were using the format %f instead of %lf, which is the right one to use with the double. Just change to %lf in both printf and scanf.

In the printf is optional but is recommended.

Altered code:

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

int main()
{
    setlocale(LC_ALL,"PORTUGUESE");
    printf("Claculo de imc\n");
    //variaveis peso e altura
    double peso;
    double altura;
    printf("Por favor digite seu peso: ");
    scanf("%lf",&peso);
    printf("por favor digite sua altura: ");
    scanf("%lf",&altura);

    double imc;
    imc = peso/(altura *altura );
    printf("Seu imc é de %.2lf",imc);

    system("pause");
    return 0;
}
  • 2

    In addition to what has been said above, beware of setlocale(LC_ALL,"PORTUGUESE");. If the language is set to Portuguese then the decimal places of the floating point numbers will be represantated by comma, while in English they will be represented by a dot.

  • 1

    What I found strange was that even with this setlocale, if you put a comma number instead of a dot scanf just read the number before the comma. I don’t know if this is normal because I don’t have much experience in C/C++.

  • 1

    I confess that I did not have this problem. Here it turned of good

  • 1

    It’s weird. I compiled using gcc 4.8.4. Look: http://i.imgur.com/Xocanx3.png

  • in the second tentative, as was setlocale(LC_ALL, "PORTUGUESE"); ?

  • Both are the same, I just put the number with comma in the first and point in the second (there at the time).

Show 1 more comment

Browser other questions tagged

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