Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format specifies type "int*" but argument has type "int"

Tags:

c

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?

like image 359
Josh Fair Avatar asked Aug 29 '14 19:08

Josh Fair


People also ask

What specifies that the argument is a long long int?

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.


2 Answers

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.

like image 194
braindf Avatar answered Oct 25 '22 15:10

braindf


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.)

like image 25
Xavier Dass Avatar answered Oct 25 '22 15:10

Xavier Dass