Creating a code that prints out Body Mass Index
printf("What is your height in inches?\n");
scanf("%d", height);
printf("What is your weight in pounds?\n");
scanf("%d", weight);
I have height and weight as initialized as int height, int weight, but the program is not letting me run it because it says the format is type int* on both scanf lines. What am I doing wrong to get this program to run?
A prefix with d, i, o, u, x, X, and n types that specifies that the argument is a long long int or unsigned long long int. L. A prefix with a, A, e, E, f, F, g, or G types that specifies that the argument is long double.
scanf requires the format (your "%d") and also a memory address of the variable where it should put the value that was read. height and weight are int, not the memory address of an int (this is what int * type 'says': a pointer to a memory address of an int). You should use the operator & to pass the memory address to scanf.
Your code should be:
printf("What is your height in inches?\n");
scanf("%d", &height);
printf("What is your weight in pounds?\n");
scanf("%d", &weight);
Update: As The Paramagnetic Croissant pointed out, reference is not the correct term.So I changed it to memory address.
taking into account what the other users have said, try something like this;
int height;                  <---- declaring your variables
int weight;
float bmi;                   <---- bmi is a float because it has decimal values
printf("What is your height in inches?\n");
scanf("%d", &height);           <----- don't forget to have '&' before variable you are storing the value in
printf("What is your weight in pounds?\n");
scanf("%d", &weight);
bmi = (weight / pow(height, 2)) * 703;    <---- the math for calculating BMI
printf("The BMI is %f\n", bmi);
(for this you will need to include the math.h library.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With