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
Related
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;
I am still learning to code and wrote this code to get the total after the weight and distance are calculated. Can someone tell my why the math is not working?
Example: When I enter 5 for weight and 1500 miles instead of getting 8.20 back I get 3.50.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
double weight,mileCost;
int miles, segment, remainder, stop = 1;
while(stop == 1){
printf("Charge by weight:(We don't tale packages over 10lbs\n");
printf("\n 1-2 lbs: $1.50\n 3-6 lbs: $3.70\n 7-10 lbs: $5.25\n ");
printf("Enter your package's weight:\n");
scanf("%f", &weight);
printf("Charge by mile: \n");
printf("$1.50 for every 500 miles\n");
printf("Enter the total miles for your package:\n");
scanf("%d", &miles);
if(miles == 0 || weight == 0 || weight > 10){
printf("Invalid entry! Try Again.");
}
segment= ceil((double) miles / 500);
remainder = miles % 500;
if(remainder > 0)
remainder = 1;
if(weight <= 2){
mileCost = 1.50 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
else if(weight > 2 && weight <= 6){
mileCost = 3.70 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
if(weight > 6 && weight <= 10){
mileCost = 5.25 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
system("pause");
printf("Would you like to ship another package?\n Enter 1 to continue or 0 to stop. \n");
scanf("%d", &stop);
if(stop == 0){
printf("Thank you for your business! \n");
}
}
return 0;
}
Changing double weight, mileCost; to float weight, mileCost; will make it work since you are reading the variable from stdin using scanf("%f", &weight) (for floats) and not scanf("%lf", &weight) (for double).
I am still puzzled by why you do this:
if(remainder > 0)
remainder = 1;
Wouldn't ceil() already handle that in:
segment = ceil((double) miles / 500);
Also, based on the formula you provided, inputting weight=5 and miles=1500 should output 11.1.
You must use "lf" for a reading of a double variable:
scanf("%lf", &weight);
For an assignment I was asked to create a small program which asks for the users input which determines the converter they wish to operate. My question is why doesn't the program ask for the users input, AFTER they have entered which convertor they wish to use (1 or 2). Instead of calling scanf, it just runs the entire statement in one go.
#include <stdio.h>
int main()
{
float cm;
float inches;
int operation;
printf("Hello and welcome to the Inches to Centimetres converter. \n");
printf("Choose from the options below by entering the corresponding number:\n");
printf("Inches to CM converter (1)\n");
printf("CM to Inches converter (2)\n");
scanf("&d", &operation);
if (operation == 1)
{
printf("Please enter the amount of Inches you wish to convert : \n");
scanf("%f", &inches);
cm = inches * 2.54;
if (inches <= 0)
{
printf("Invalid number");
}
else
printf("%f inches is equal to %f centimetres.", inches, cm);
}
else if (operation == 2);
{
printf("Please enter the amount of Centimetres you wish to convert : ");
scanf("%f", &cm);
inches = cm / 2.54;
if (cm <= 0)
{
printf("Invalid number");
}
else
printf("%f centimetres is equal to %f inches.", cm, inches);
}
}
Output
Two problems here. First:
scanf("&d", &operation);
There's a typo, "&d" should be "%d", and it's why you get prompted twice right away. You want:
scanf("%d", &operation);
Second is this:
}
else if (operation == 2);
{
The ; immediately ends the else block. So the block in braces will always run. Get rid of the ;
}
else if (operation == 2)
{
Better yet:
} else if (operation == 2) {
Formatting your braces this way will practically eliminate this type of error.
For this lab I am not allowed to edit the main function, everything must be done in the function below main. I can't seem to find my problem here. I think it has something to do with the call to the calculateBMI function.
#include <stdio.h>
FILE *fp;
//For loop, which allows up to 4 entries.
int main(void) {
int i;
fp = fopen("csis.txt", "w");
for (i = 1; i <= 4; ++i) {
calculateBMI();
}
fclose(fp);
return 0;
}
//Function that calculates the BMI of the Input.
double calculateBMI(int weightInPounds, int heightInInches) {
double BMI;
BMI = weightInPounds * 703 / heightInInches * heightInInches;
//If BMi is less then 18.5 print this.
if (BMI < 18.5) {
printf("Your BMI is %d, you are underweight.", BMI);
fprintf(fp, "Your BMI is %d, you are underweight.", BMI);
}
//if BMI is between 18.5 and less then 25 print this.
else if (BMI > 18.5 & BMI < 25) {
printf("Your BMI is %d, you are Normal.", BMI);
fprintf(fp, "Your BMI is %d, you are Normal.", BMI);
}
//if BMI is greater then 25 and less then 30 print this.
else if (BMI > 25 & BMI < 30) {
printf("Your BMI is %d, you are Overweight.", BMI);
fprintf(fp, "Your BMI is %d, you are Overweight.", BMI);
}
//if BMI is greater then 30 print this.
else (BMI > 30) {
printf("Your BMI is %d, you are Obese.", BMI);
fprintf(fp, "Your BMI is %d, you are Obese.", BMI);
}
//Asks user for input weight in pounds.
printf("What is your weight in pounds?");
fprintf(fp, "What is your weight in pounds?");
scanf("%d\n", weightInPounds);
fscanf(fp, "%d\n", weightInPounds);
// Asks user for input height in inches.
printf("What is your height in inches?");
fprintf("What is your height in inches?");
scanf("%d\n", heightInInches);
fscanf(fp, "%d\n", heightInInches);
getchar(0);
return (0);
}
In the else if statement you used the & operator , but in this case you need to use && operator . The & operator is a bitwise operator .
For example if you have two 4 bit variable 1001 and 1010 . You use the & operator the result will be 1000 .
In this case you have to use && operator
It should look like this:
else if (BMI > 18.5 && BMI < 25)
There are many simple mistakes in your code.
You should define your calculateBMI function before main or You should declare it before main.
while calling calculateBMI function pass the parameter for the function / read the values inside the calculateBMI function.
if you declare BMI as double then use %lf as format specifier in printf statement.
cant give condition for else statement, so make it else if
use bracket for equation
BMI = weightInPounds * 703 / heightInInches * heightInInches;
you should pass the address of the variable for scanf statement (i.e &variable)
here is the modified code.
#include <stdio.h>
FILE *fp;
double calculateBMI();
//For loop, which allows up to 4 entries.
int main(void) {
int i;
fp = fopen("csis.txt", "w");
for (i = 1; i <= 4; ++i) {
calculateBMI();
}
fclose(fp);
return 0;
}
//Function that calculates the BMI of the Input.
double calculateBMI(int weightInPounds, int heightInInches) {
double BMI=0;
//Asks user for input weight in pounds.
printf("What is your weight in pounds?");
fprintf(fp, "What is your weight in pounds?");
scanf("%d\n", &weightInPounds);
fscanf(fp, "%d\n", weightInPounds);
// Asks user for input height in inches.
printf("What is your height in inches?");
fprintf(fp,"What is your height in inches?");
scanf("%d\n", &heightInInches);
fscanf(fp, "%d\n", heightInInches);
BMI = (weightInPounds * 703) / (heightInInches * heightInInches);
//If BMi is less then 18.5 print this.
if (BMI < 18.5) {
printf("Your BMI is %f, you are underweight.", BMI);
fprintf(fp, "Your BMI is %f, you are underweight.", BMI);
}
//if BMI is between 18.5 and less then 25 print this.
else if (BMI > 18.5 & BMI < 25) {
printf("Your BMI is %f, you are Normal.", BMI);
fprintf(fp, "Your BMI is %f, you are Normal.", BMI);
}
//if BMI is greater then 25 and less then 30 print this.
else if (BMI > 25 & BMI < 30) {
printf("Your BMI is %f, you are Overweight.", BMI);
fprintf(fp, "Your BMI is %f, you are Overweight.", BMI);
}
//if BMI is greater then 30 print this.
else if(BMI > 30) {
printf("Your BMI is %f, you are Obese.", BMI);
fprintf(fp, "Your BMI is %f, you are Obese.", BMI);
}
getchar();
return (0);
}
extra info. i think the in BMI formula you should give height in meters/ convert it into meters.
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.