Implementing a BMI Calculator in C - c

I am having trouble making the BMI calculator program and I am wondering what I haven't done correctly. I am a beginner so go easy on me, thanks!
#include <stdio.h>
main()
{
// Variables for height, weight, and bmi
float height;
float weight;
float bmi;
printf("\aEnter your height: ");
scanf(" %f", height);
printf("\a\nEnter your weight: ");
scanf(" %f", weight);
bmi = (height * 4.88) / (weight * weight);
printf("\a\nYour BMI is: %f", bmi);
getchar();
return 0;
}

scanf requires a pointer to parameters following the format string.
Use the & operator thus:
printf("\aEnter your height: ");
scanf(" %f", &height);
printf("\a\nEnter your weight: ");
scanf(" %f", &weight);

You need to pass the address of your variables to scanf, so that it can modify the values at that address:
scanf(" %f", &height);
^
\ Address-of operator (Returns the memory address of the float)
And:
scanf(" %f", &weight);

Related

C code / BMI / need help please - BMI value result always wrong

This is my first post, so if something goes wrong, sorry xD
Basically, i'm helping a friend in his C class, and i'm leaning at the same time, well, literally the basic there
This probably is something basic, that we don't know how to correct, the problem is: the BMI value is getting aways "0" and the "bmi corrected" doesn't shows the results, he just end the code, ah, yeah, the addition(+54) and subtraction(-93), they are flat values that the teacher asks to put.
Can you guys help us?
edit: i solved the problem about always show 0, we didn't put a dot in the height value
#include<stdio.h>
int main (){
float bmi, height, weight, valueone, valuetwo;
printf("please your height: ");
scanf(" %f", &height);
printf("please your weight : ");
scanf(" %f", &weight);
bmi = weight/(height*height);
printf("bmi=", &bmi);
valueone = bmi + 54;
valoetwo = valueone - 93;
printf("BMI corrected: ", &valoetwo);
getch();
return 0;
}
If you correct errors it would compile and seems to produce some value, not sure if it really is what you want...
float bmi, height, weight, valueone, valuetwo;
printf("please your height: ");
scanf(" %f", &height);
printf("please your weight : ");
scanf(" %f", &weight);
bmi = weight/(height*height);
printf("bmi=%0.6f\n", bmi);
valueone = bmi + 54;
valuetwo = valueone - 93;
printf("BMI corrected: %f\n", valuetwo);
return 0;

float number is not displaying correctly in my C program

I'm just trying out a BMI (body mass index) calculator in C, but I keep on getting 0 as the final answer. Here's the code:
#include <stdio.h>
int main(){
int weight, height;
printf("Enter your weight in kilograms: ");
scanf("%d", &weight);
printf("Enter your height in meters: ");
scanf("%d", &height);
printf("Your BMI(body's mass index) is %f\n", weight/height*height);
return 0;
}
It displays 0 as the result.
I just need it to display the number with decimals (using %f and using int for weight and height).
Since the variables are integers, it's doing integer arithmetic, and returning an integer result. Printing an integer with %f causes undefined behavior.
Cast one of the variables to float to get a float result.
printf("Your BMI(body's mass index) is %f\n", (float)weight/(height*height));
Also, you have the formula wrong, it should be weight/(height*height).
i figured it out.
i just used this:
int weight;
float height;
printf("Enter your weight in kilograms: ");
scanf("%d", &weight);
printf("Enter your height in meters: ");
scanf("%f", &height);
printf("Your BMI(body's mass index) is %.2f\n", (weight)/(height*height));

Why is this prgram not working in C?

I program doesn't work after entering P or A. why?
it's challenge from udemy course. I am just a beginner in programming :)
#include <stdio.h>
#include <stdlib.h>
int main()
{
float height=0;
float width=0;
float area=0;
float perimeter=0;
printf("Enter height of rectangle :");
scanf("%f",&height);
printf("Enter width of rectangle :");
scanf("%f", &width);
char choice;
printf("Enter P for perimeter and A for area: ");
scanf(" %c", choice);
if(choice=='P' || choice=='p'){
printf("Width: %f", width);
printf("Height: %f", height);
perimeter= 2.0*(height*width);
printf("Perimeter of rectangle is: %f", perimeter);
}else if(choice=='A' || choice=='a'){
printf("Width: %f", width);
printf("Height: %f", height);
area= (height*width);
printf("Area of rectangle is: %f", area);
}else
printf("Invalid Input");
}
Correct this as follows:
scanf(" %c", &choice);
&choice will point to an address where you want to store the value.
This is not working Because you forgot & in scanf(" %c", choice); . It should be scanf(" %c", &choice); . In scanf function , you have to provide the address of the item to be scanned (Similar to scanf("%f", &height);).
Modified code :-
#include <stdio.h>
#include <stdlib.h>
int main()
{
float height = 0;
float width = 0;
float area = 0;
float perimeter = 0;
printf("Enter height of rectangle :");
scanf("%f", &height);
printf("Enter width of rectangle :");
scanf("%f", &width);
char choice;
printf("Enter P for perimeter and A for area: ");
scanf(" %c", &choice);
if (choice == 'P' || choice == 'p')
{
printf("Width: %f", width);
printf("Height: %f", height);
perimeter = 2.0 * (height * width);
printf("Perimeter of rectangle is: %f", perimeter);
}
else if (choice == 'A' || choice == 'a')
{
printf("Width: %f", width);
printf("Height: %f", height);
area = (height * width);
printf("Area of rectangle is: %f", area);
}
else
printf("Invalid Input");
}
Output :-
Enter height of rectangle :4
Enter width of rectangle :5
Enter P for perimeter and A for area: A
Width: 5.000000Height: 4.000000Area of rectangle is: 20.000000
I recommend to add \n before your printf() statements for better readability.

Debug assertion failed using float variable and scanf() to get user grades and printf() them

#include <stdio.h>
#include <stdlib.h>
int main()
{
float grade1 = 0.0;
float grade2 = 0.0;
float grade3 = 0.0;
printf("Enter your three test grades in decimal form: \n");
scanf(" %f", grade1);
scanf(" %f", grade2);
scanf(" %f", grade3);
float avg = (grade1 + grade2 + grade3) / 3;
printf("Average: %.2f \n", avg);
system("pause");
return 0;
}
I am using a tutorial on the new boston YT channel and this code does not work on my compiler while tutorial code does work. I have Visual Studio Community 2015.
The guide code could not be the same, it would not work.
I suggest you to enable the warnings in your compiler, which will save you from really trivial mistakes in the future.
So, the error is here:
scanf(" %f", grade1);
scanf(" %f", grade2);
scanf(" %f", grade3);
The three variables need & operator to receive the value.
scanf(" %f", &grade1);
scanf(" %f", &grade2);
scanf(" %f", &grade3);
I would recommend you to have a look at: https://www.tutorialspoint.com/cprogramming/c_operators.htm
You should scan the address of the float, etc
scanf( " %f", &grade1);

Scanf always returning 0.000000

I'm trying to make a simple program to calculate the body mass index, but the scanf(s) always return 0.00000, no matter what i try. I searched everywhere, tried many things,
Thanks to everyone.
#include <stdio.h>
#include <stdlib.h>
int main() {
float height;
float initialheight;
float weight;
float bmi;
float nothing;
printf("What's your weight? ");
scanf("%lf", &weight);
printf("%f", &weight);
printf("What's your height? ");
scanf("%lf", &initialheight);
printf("%f", &initialheight);
height = (initialheight * initialheight);
printf("%f", &height);
bmi = (weight / height);
printf("Your BMI is ");
printf("%f", &bmi);
scanf("%f", nothing); //just to keep the program open
return 0;
}
If you print a value you dont have to print the adress!
So change this:
printf("%f", &weight);
to this:
printf("%f", weight);
So that you actually print the value
An also you have to change %lf to %f in your scanf
So your program should look something like this:
#include <stdio.h>
#include <stdlib.h>
int main(){
float height, initialheight, weight, bmi;
printf("What's your weight?\n>");
scanf(" %f", &weight);
printf("%.2f\n\n", weight);
printf("What's your height?\n>");
scanf(" %f", &initialheight);
printf("%.2f\n\n", initialheight);
height = (initialheight * initialheight);
bmi = (weight / height)*10000;
printf("Your BMI is ");
printf("%.2f\n\n", bmi);
system("pause");
return 0;
}
As an example with the input:
70 and 175
The result/ BMI is:
22.86
Side Note:
BMI = mass(kg) / (height(m) * height(m))
BMI = mass(lb) / (height(in) * height(in)) * 703
Well you have to change two things. First, change printf("%f", &weight) to printf("%f", weight). And also change scanf("%lf", &weight) to scanf("%f", &weight) will make your program fine.

Resources