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;
Related
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));
I am not so familiar with C. Therefore, maybe someone will easily find a solution, I will not mind if you share it.
After entering the data in the first scanf() always gives the option else(): "Error".
I was looking for possible options for the problem. I found a lot of things like that, but nothing that to help me specifically. I think the mistake is in the strcmp(). But I can not say for sure. Will you help?
#include <stdio.h>
#include <string.h>
int main()
{
float celsius, fahrenheit;
char tempConvering[10];
printf("Enter what to convert to what (F to C; C to F): ");
scanf(" %s", &tempConvering[10]);
if(strcmp(tempConvering, "F to C") == 0)
{
printf("\nEnter temperature in Fahrenheit: ");
scanf(" %f", &fahrenheit);
celsius = fahrenheit * 1.8 + 32;
printf("%.2f Fahrenheits = %.2f Celsius\n", fahrenheit, celsius);
}
else if(strcmp(tempConvering, "C to F") == 0)
{
printf("\nEnter temperature in Celsius: ");
scanf(" %f", &celsius);
celsius = (fahrenheit - 32) / 1.8;
printf("%.2f Celsius = %.2f Fahrenheits\n", celsius, fahrenheit);
}
else
{
puts("\nError!");
}
}
I believe there's an error with how you're using scanf, specifically, at this point:
scanf(" %s", &tempConvering[10]);
^
|
+---- here
The second argument to scanf should be the address of the place to store the result. Here, you're saying "place the string that's read in in the memory just after the buffer that I've set up," which isn't probably want you wanted to do. Instead, write this:
scanf(" %s", tempConvering);
This says "place the string inside the buffer named tempConverting." If you're just getting started with C and haven't learned much about pointers and arrays, a good rule of thumb is that if you're reading a string with `scanf, you should just give the name of the array variable where you want to store the string rather than using an ampersand.
Hope this helps!
#include <stdio.h>
#include <string.h>
int main()
{
float celsius, fahrenheit;
char tempConvering[20];
printf("what do you want to convert? ");
scanf("%s", tempConvering);
if(strcmp(tempConvering, "Fahrenheits") == 0)
{
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) / 1.8;
printf("%.2f Fahrenheits = %.2f Celsius\n", fahrenheit, celsius);
}
else if(strcmp(tempConvering, "Celsius") == 0)
{
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = celsius * 9 / 5 + 32;
printf("%.2f Celsius = %.2f Fahrenheits\n", celsius, fahrenheit);
}
else
{
puts("\nError!");
}
}
This was the answer. I have to thank you for the tips, I will try to remember all the details about the scanf(). However, the problem disappeared only when I changed the desired answer not to "F to C" but to "Fahrenheits". Well, respectively, I changed the question. The program instantly earned. Nevertheless, attempts to do something with the scanf() are unsuccessful, to the extent that the same thing happens with the fgets().
In any case, somehow the problem is solved, thank you all!
Don't put extra space while you execute the scanf() function.
This might be an issue sometimes!
This should work fine:
scanf("%s", tempConvering);
By the way, while you take a string as an input, there is no need to use & before the string name.
Someone has already described the details above.
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.
I am attempting to write a program that will take a user's input of weight and height and then return a BMI value and tell the user if they are under/over or normal weight. The code compiles with no errors, however no matter what numbers I input for weight and height, the result is always "You have a BMI of 0 and your weight status is overweight". Is there something wrong with my code or is my math just incorrect?
#include <stdio.h>
int main()
{
double wt_lb, ht_in, bmi, ht_ft;
printf("Please enter your weight in whole pounds: ");
scanf("%lf", &wt_lb);
printf("Please enter your height in whole inches: ");
scanf("%lf", &ht_in);
ht_ft = ht_in/12;
bmi = (703*wt_lb)/(ht_ft*ht_ft);
if (bmi < 18.5) {
printf("You have a BMI of %.lf, and your weight status is underweight\n" &bmi);
} else if (bmi >= 18.5 && bmi < 25) {
printf("You have a BMI of %.lf, and your weight status is normal\n", &bmi);
} else {
printf("You have a BMI of %.lf, and your weight status is overweight\n", &bmi);
}
}
Remove & from aal of your printf's argument.
printf("You have a BMI of %f, and your weight status is underweight\n" &bmi);
^
|
Remove this &
It should be
printf("You have a BMI of %f, and your weight status is underweight\n", bmi);
Also never use %lf specifier for double in printf (in scanf you have to use) instead use %f.
In the printf statement don't use &bmi, use simple bmi.
It should work
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);