i'm a beginner at C++ programming, i've started studying it a month ago at school and they gave us a chance to do some projects
mine was to calculate the BMI for someone and to see if he/she was underweight, healthy.. etc.
i tried to do it with 'switch' but i was so confused so i changed it to if.. else statment
i don't know why but it's not working, it suppose to calculate the bmi and give the right results but no matter what numbers i put it still give me "underweight".
i'm so confused, could someone help please?
#include<iostream.h>
#include<conio.h>
main()
{
char gender ;
int bmi,h,w;
cout<<"enter your gender, F for female, M for male= "<<endl;
cin>>gender;
cout<<"enter your height in cm= "<<endl;
cin>>h;
cout<<"enter your weight in kg= "<<endl;
cin>>w;
bmi=w/(h*2);
if(bmi<=18.5)
{
cout<<"underweight\n";
cout<<"Eat more frequently\n";
cout<<"Choose nutrient-rich foods\n";
cout<<"Have an occasional treat.";
}
elseif (18.5<bmi<=24.9)
{ cout<<"healthy"<<endl;
cout<<"\n exercise, don't skip breakfast, eat 5 servings of fruits and veggies a day";
}
elseif(24.9<bmi<=29.9)
{cout<<"overweight"<<endl;
cout<<"Be Sure to Get Enough Vitamin D to Reduce Fat Accumulation\n";
cout<<"Eat Plenty of Foods Rich in Vitamin C \n";
cout<<"Drink Plenty of Water and exercise \n";
cout<<"Don't eat too much sweats";
}
else
{ cout<<"obesty"<<endl;
cout<<"Eat High fiber foods \n";
cout<<"Eat small meals \n";
cout<<"Change Your Eating Habits\n";
cout<<"Exercise regularly";
}
getch();
}
What numbers are you inputting for height and weight?
I advise you to step through your code a debugger, so you can see exactly what the contents of memory are at the points where decisions are being made.
Also, those conditions at lines 22 and 27 don't work the way you think they do.
okay so if i corrected the formula then what about the if statement?
i'm confused right now, as i told you i'm a beginner who knows almost nothing about programming, could anyone correct it for me?
a lot of programming langs are a little non-intuitive (at first) in the way they do multiple comparisons -- to compound them, combine with logical AND.
To express the mathematical 3 < x ≤ 10 , you'd say 3<x && x<=10
#include<iostream.h>
#include<conio.h>
main()
{
char gender ;
int bmi,h,w;
cout<<"enter your gender, F for female, M for male= "<<endl;
cin>>gender;
cout<<"enter your height in cm= "<<endl;
cin>>h;
cout<<"enter your weight in kg= "<<endl;
cin>>w;
bmi=w/(h*h);
if(bmi<=18.5)
{
cout<<"underweight, your bmi= \n"<<bmi<<endl;
cout<<"Eat more frequently\n";
cout<<"Choose nutrient-rich foods\n";
cout<<"Have an occasional treat.";
}
elseif (19.0>bmi && bmi<=24.9)
{
cout<<"healthy, your bmi="<<bmi<<endl;
cout<<"\n exercise, don't skip breakfast, eat 5 servings of fruits and veggies a day";
}
elseif(30>bmi && bmi<=29.9)
{
cout<<"overweight, your bmi= "<<bmi<<endl;
cout<<"Be Sure to Get Enough Vitamin D to Reduce Fat Accumulation\n";
cout<<"Eat Plenty of Foods Rich in Vitamin C \n";
cout<<"Drink Plenty of Water and exercise \n";
cout<<"Don't eat too much sweats";
}
else
{
cout<<"obesty, your bmi= "<<bmi<<endl;
cout<<"Eat High fiber foods \n";
cout<<"Eat small meals \n";
cout<<"Change Your Eating Habits\n";
cout<<"Exercise regularly";
}
getch();
}
i tried what Thomas1965 did.
but when i tried to use it, it didn't show the correct results!
i guess it took only the weight and the results appeared without using the formula
the output was:
enter your gender, F for female, M for male=
F
enter your height in cm=
154
enter your weight in kg=
43.3
obesty, your bmi= 43
Eat High fiber foods
Eat small meals
Change Your Eating Habits
Exercise regularly
char gender;
double h, w;
cout << "enter your gender, F for female, M for male= " << endl;
cin >> gender;
cout << "enter your height in cm= " << endl;
cin >> h;
cout << "enter your weight in kg= " << endl;
cin >> w;
h = h / 100; // convert to meter
double bmi = w / (h * h);
if (bmi <= 18.5)
{
cout << "underweight, your bmi= \n" << bmi << endl;
cout << "Eat more frequently\n";
cout << "Choose nutrient-rich foods\n";
cout << "Have an occasional treat.";
}
elseif (bmi > 18.5 && bmi <= 24.9)
{
cout << "healthy, your bmi=" << bmi << endl;
cout << "\n exercise, don't skip breakfast, eat 5 servings of fruits and veggies a day";
}
elseif (bmi > 25 && bmi <= 30)
{
cout << "overweight, your bmi= " << bmi << endl;
cout << "Be Sure to Get Enough Vitamin D to Reduce Fat Accumulation\n";
cout << "Eat Plenty of Foods Rich in Vitamin C \n";
cout << "Drink Plenty of Water and exercise \n";
cout << "Don't eat too much sweats";
}
else
{
cout << "obesty, your bmi= " << bmi << endl;
cout << "Eat High fiber foods \n";
cout << "Eat small meals \n";
cout << "Change Your Eating Habits\n";
cout << "Exercise regularly";
}
getch();
Output:
enter your gender, F for female, M for male=
F
enter your height in cm=
154
enter your weight in kg=
43.3
underweight, your bmi=
18.2577
Eat more frequently
Choose nutrient-rich foods
Have an occasional treat.