-1

I want the program to get the user's height and weight then input that into the bmi formula then output the result.

The height question works fine but when you enter a number into the weight question and press enter, it just makes a new line. Once you enter another number, it sets that to bmi and prints that.

#include<stdio.h>

int main()
{
int h, w, bmi;

printf("What is your height (in inches)? ");
scanf("%d", &h);

printf("What is your weight? ");
scanf("%d", &w);

bmi = (w/(h*h))*703;
scanf("%d", &bmi);

printf("\t\tBMI\n");
printf("US: %d\n", bmi);
}
ruakh
  • 175,680
  • 26
  • 273
  • 307
David Vlcek
  • 21
  • 1
  • 6
  • 5
    Why are you `scanf`-ing into `&bmi`? – ruakh Apr 15 '17 at 20:11
  • why you taking input of `bmi`? – Prosen Ghosh Apr 15 '17 at 20:12
  • `bmi = (w/(h*h))*703;` --> `bmi = 703.0 * w / (h*h);` – BLUEPIXY Apr 15 '17 at 20:16
  • You need another variable, because you input bmi and also calculate it. Is that an attempt at the user's opinion of their BMI? You also need a third prompt `printf("What is your bmi? ");` so the user does not think the program has broken. There is also an issue with `bmi = (w/(h*h))*703;` which is an integer calculation: do the division last. – Weather Vane Apr 15 '17 at 20:17

2 Answers2

5

Remove scanf("%d", &bmi); you set value for bmi and then ask for its value as an input !

O.Rares
  • 1,031
  • 1
  • 16
  • 19
  • The program is supposed to calculate the bmi. – David Vlcek Apr 15 '17 at 20:46
  • @David You did, on the line before that scanf. So why are you asking for it? – Mark Tolonen Apr 15 '17 at 20:55
  • I wrote what my objective is. I just started learning C last week so chances are I'm doing something wrong. – David Vlcek Apr 15 '17 at 21:04
  • @DavidVlcek bmi = (w/(h*h))*703; in this part the value of bmi is calculated. Remove scanf("%d", &bmi); and your problem is solved. – O.Rares Apr 15 '17 at 22:04
  • So that did help, but of course now there's a new issue, the output is always `US: 0` – David Vlcek Apr 15 '17 at 22:12
  • This is not a code factory. Ask yourself why it's always 0. Maybe try with something that will give an integers when divided by an integer's power of two. Like 5 height and 100 weight. Are you trying to give a height of x.y ? That's also a problem. Think. – Kostas Andrianos Apr 15 '17 at 22:25
  • @Dinos_12345: I agree with the general premise that "This is not a code factory", and that the OP needs to think; but I think the specific case of integer division is not easy to figure out on one's own. Most newbies will never guess that, for example, `3/4` is `0`. – ruakh Apr 15 '17 at 23:47
0

You are using int for all three variables. So when you divide w (weight) by square of h (height) then generally it would give the result 0.xxxxxx. But here you are storing this result in an integer variable, but it can't store that floating value. So it stores only 0 and then 0 is multiplied with 703 and that is of no use.

And you have defined the main function of type int so it must return a value.

Use this code instead

    #include<stdio.h>

    int main()
    {
      float h, w, bmi;

      printf("What is your height (in inches)? ");
      scanf("%f", &h);

      printf("What is your weight? ");
      scanf("%f", &w);
      bmi=((w/(h*h))*703);

      printf("US: %f\n", bmi);
      return 0;
    }
Mahesh Suthar
  • 146
  • 1
  • 12