Scanf always returning 0.000000 - c

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.

Related

Simple C Coding Problem: Skipping Over Printf and Skipping User Input

https://onlinegdb.com/-9jZX-uU0 [link]
I want to have the second line of code to have the user input the Fahrenheit not the Celsius twice. The int main() code is correct and the output number is correct but the user does not input the Fahrenheit. How do I fix this? enter image description here
What is expected is that the user inputs the Fahrenheit and the code continues. I do not want the output to skip over lines 12-14. I don't know how to fix this. I expect that once the user inputs Fahrenheit the code will continue to run as it currently does
#include <stdio.h>
#include <math.h>
double ask_for_air_celcius()
{
double temp_celcius, temp;
printf("Enter the current air temperature(in Celcius): ");
scanf(" %lf", &temp_celcius);
return(temp_celcius);
printf("Enter the current air temperature(in Fahrenheit): ");
scanf(" %lf", &temp);
return (temp);
}
int main()
{
double temp_celcius, spsound_1,spsound_convert_1,temp,
spsound,spsound_convert;
{
temp_celcius = ask_for_air_celcius();
spsound_1 = 1086 * sqrt(((5 * (temp_celcius*1.8+32)) + 297)
/ 247);
spsound_convert_1=spsound_1*1.09728;
temp = ask_for_air_celcius();
spsound = 1086 * sqrt(((5 * temp) + 297) / 247);
spsound_convert=spsound*1.09728;
printf("\nThe speed of sound at %.2f degrees Celcius is %.2f
ft/s.\n", temp_celcius, spsound_1);
printf ("The speed of sound at this temperature in Celciusis
equivalent to %.2f km/h.\n\n", spsound_convert_1);
printf("The speed of sound at %.2f degrees Fahrenheit is %.2f
ft/sec.\n", temp, spsound);
printf ("The speed of sound at this temperature in Fahrenheit
is equivalent to %.2f km/h.\n", spsound_convert);
}
return(0);
}
You used used return twice in the function so that it won't go ahead for asking the fahrenhiet question. Whenever return is encountered while compiling it terminates the function there and there only.
Now what you can do is that just make another function for fahrenhiet and copy paste the fahrenhit part in it.
#include <stdio.h>
#include <math.h>
double ask_for_air_celcius(){
double temp_celcius;
printf("Enter the current air temperature(in Celcius): ");
scanf(" %lf", &temp_celcius);
return(temp_celcius);
}
double ask_for_air_Fahrenhiet(){
double temp;
printf("Enter the current air temperature(in Fahrenheit): ");
scanf(" %lf", &temp);
return (temp);
}
int main()
{
double temp_celcius, spsound_1,spsound_convert_1,temp,
spsound,spsound_convert;
{
temp_celcius = ask_for_air_celcius();
spsound_1 = 1086 * sqrt(((5 * (temp_celcius*1.8+32)) + 297)
/ 247);
spsound_convert_1=spsound_1*1.09728;
temp = ask_for_air_Fahrenhiet();//changes here
spsound = 1086 * sqrt(((5 * temp) + 297) / 247);
spsound_convert=spsound*1.09728;
printf("\nThe speed of sound at %.2f degrees Celcius is %.2f ft/s.\n", temp_celcius, spsound_1);
printf ("The speed of sound at this temperature in Celciusis equivalent to %.2f km/h.\n\n", spsound_convert_1);
printf("The speed of sound at %.2f degrees Fahrenheit is %.2f ft/sec.\n", temp, spsound);
printf ("The speed of sound at this temperature in Fahrenheit is equivalent to %.2f km/h.\n", spsound_convert);
}
return(0);
}
Hope it helps!!!
;)

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

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

Implementing a BMI Calculator in 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);

Resources