Name: Anonymous 2010-02-12 10:44
I have to write a c program which takes "height weight" in that order in one line and computes body mass index and checks if over under or normal and increments a counter also it checks if the inputs are and there formats are correct here is my code i need to add an if statement at the top to check for an empty line and print "empty line" also the first input taken loses a chracter cuz of the loop that lets it continue also when it is finished taking inputs user presses ctrl + d to tell it to stop taking input.any help would be appreciated code below
----------------------------------------------------------------
#include <stdio.h>
int underweights = 0;
int normals = 0;
int overweights = 0;
int obeses = 0;
float BMI(float heightInMeters,float weightInKG)
{
float temp;
temp = (weightInKG/(heightInMeters * heightInMeters));
return temp;
}
Category(float BMI)
{
if(BMI < 18.5)
{
printf("underweight");
underweights++;
}
else if(BMI >= 18.5 && BMI < 25)
{
printf("normal");
normals++;
}
else if(BMI >= 25 && BMI < 30)
{
printf("overweight");
overweights++;
}
else if(BMI >= 30)
{
printf("obese");
obeses++;
}
}
main()
{
float BMI_value;
float height;
float weight;
char heightFormat1;
char heightFormat2;
char weightFormat1;
char weightFormat2;
char c;
double IN_TO_CM = 2.54;
double LB_TO_KG = 0.45359;
while((c = getchar()) != EOF)
{
scanf("%f%c%c %f%c%c",&height, &heightFormat1,&heightFormat2, &weight, &weightFormat1, &weightFormat2);
if((heightFormat1 == 'c' && heightFormat2 == 'm') && (weightFormat1 == 'k' && weightFormat2 == 'g'))
{
if(height < 140 || height > 200)
{
printf("invalid value for height\n");
}
else if(weight < 40.8 || weight > 226.8)
{
printf("invalid value for weight\n");
}
else
{
float hInMeters = height / 100;
float tempBMI = BMI(hInMeters,weight);
printf("%.1f " , tempBMI);
Category(tempBMI);
printf("\n");
}
}
else if((heightFormat1 == 'c' && heightFormat2 == 'm') && (weightFormat1 == 'l' && weightFormat2 == 'b'))
{
if(height < 140 || height > 200)
{
printf("invalid value for height\n");
}
else if(weight < 90 || weight > 500)
{
printf("invalid value for weight\n");
}
else
{
float hInMeters = height / 100;
float wInKg = weight * LB_TO_KG;
float tempBMI = BMI(hInMeters,wInKg);
printf("%.1f " , tempBMI);
Category(tempBMI);
printf("\n");
}
}
else if((heightFormat1 == 'i' && heightFormat2 == 'n') && (weightFormat1 == 'k' && weightFormat2 == 'g'))
{
if(height < 55 || height > 79)
{
printf("invalid value for height\n");
}
else if(weight < 40.8 || weight > 226.8)
{
printf("invalid value for weight\n");
}
else
{
float hInMeters = (height * IN_TO_CM) / 100;
float tempBMI = BMI(hInMeters,weight);
printf("%.1f " , tempBMI);
Category(tempBMI);
printf("\n");
}
}
else if((heightFormat1 == 'i' && heightFormat2 == 'n') && (weightFormat1 == 'l' && weightFormat2 == 'b'))
{
if(height < 55 || height > 79)
{
printf("invalid value for height\n");
}
else if(weight < 90 || weight > 500)
{
printf("invalid value for weight\n");
}
else
{
float hInMeters = (height * IN_TO_CM) / 100;
float wInKg = weight * LB_TO_KG;
float tempBMI = BMI(hInMeters,wInKg);
printf("%.1f " , tempBMI);
Category(tempBMI);
printf("\n");
}
}
else if((heightFormat1 != 'c' || heightFormat1 != 'i') || (heightFormat2 != 'm' || heightFormat2 != 'n'))
{
printf("invalid format for height");
}
else if((weightFormat1 != 'k' || weightFormat1 != 'l') || (weightFormat2 != 'g' || weightFormat2 != 'b'))
{
printf("invalid format for weight");
}
}
printf("Summary: \n");
printf("Number of underweights: %d\n", underweights);
printf("Number of normals: %d\n", normals);
printf("Number of overweights: %d\n", overweights);
printf("Number of obeses: %d\n", obeses);
}
----------------------------------------------------------------
#include <stdio.h>
int underweights = 0;
int normals = 0;
int overweights = 0;
int obeses = 0;
float BMI(float heightInMeters,float weightInKG)
{
float temp;
temp = (weightInKG/(heightInMeters * heightInMeters));
return temp;
}
Category(float BMI)
{
if(BMI < 18.5)
{
printf("underweight");
underweights++;
}
else if(BMI >= 18.5 && BMI < 25)
{
printf("normal");
normals++;
}
else if(BMI >= 25 && BMI < 30)
{
printf("overweight");
overweights++;
}
else if(BMI >= 30)
{
printf("obese");
obeses++;
}
}
main()
{
float BMI_value;
float height;
float weight;
char heightFormat1;
char heightFormat2;
char weightFormat1;
char weightFormat2;
char c;
double IN_TO_CM = 2.54;
double LB_TO_KG = 0.45359;
while((c = getchar()) != EOF)
{
scanf("%f%c%c %f%c%c",&height, &heightFormat1,&heightFormat2, &weight, &weightFormat1, &weightFormat2);
if((heightFormat1 == 'c' && heightFormat2 == 'm') && (weightFormat1 == 'k' && weightFormat2 == 'g'))
{
if(height < 140 || height > 200)
{
printf("invalid value for height\n");
}
else if(weight < 40.8 || weight > 226.8)
{
printf("invalid value for weight\n");
}
else
{
float hInMeters = height / 100;
float tempBMI = BMI(hInMeters,weight);
printf("%.1f " , tempBMI);
Category(tempBMI);
printf("\n");
}
}
else if((heightFormat1 == 'c' && heightFormat2 == 'm') && (weightFormat1 == 'l' && weightFormat2 == 'b'))
{
if(height < 140 || height > 200)
{
printf("invalid value for height\n");
}
else if(weight < 90 || weight > 500)
{
printf("invalid value for weight\n");
}
else
{
float hInMeters = height / 100;
float wInKg = weight * LB_TO_KG;
float tempBMI = BMI(hInMeters,wInKg);
printf("%.1f " , tempBMI);
Category(tempBMI);
printf("\n");
}
}
else if((heightFormat1 == 'i' && heightFormat2 == 'n') && (weightFormat1 == 'k' && weightFormat2 == 'g'))
{
if(height < 55 || height > 79)
{
printf("invalid value for height\n");
}
else if(weight < 40.8 || weight > 226.8)
{
printf("invalid value for weight\n");
}
else
{
float hInMeters = (height * IN_TO_CM) / 100;
float tempBMI = BMI(hInMeters,weight);
printf("%.1f " , tempBMI);
Category(tempBMI);
printf("\n");
}
}
else if((heightFormat1 == 'i' && heightFormat2 == 'n') && (weightFormat1 == 'l' && weightFormat2 == 'b'))
{
if(height < 55 || height > 79)
{
printf("invalid value for height\n");
}
else if(weight < 90 || weight > 500)
{
printf("invalid value for weight\n");
}
else
{
float hInMeters = (height * IN_TO_CM) / 100;
float wInKg = weight * LB_TO_KG;
float tempBMI = BMI(hInMeters,wInKg);
printf("%.1f " , tempBMI);
Category(tempBMI);
printf("\n");
}
}
else if((heightFormat1 != 'c' || heightFormat1 != 'i') || (heightFormat2 != 'm' || heightFormat2 != 'n'))
{
printf("invalid format for height");
}
else if((weightFormat1 != 'k' || weightFormat1 != 'l') || (weightFormat2 != 'g' || weightFormat2 != 'b'))
{
printf("invalid format for weight");
}
}
printf("Summary: \n");
printf("Number of underweights: %d\n", underweights);
printf("Number of normals: %d\n", normals);
printf("Number of overweights: %d\n", overweights);
printf("Number of obeses: %d\n", obeses);
}