a problem with if.. else statement

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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.";
}
else
if (18.5<bmi<=24.9)
{ cout<<"healthy"<<endl;
cout<<"\n exercise, don't skip breakfast, eat 5 servings of fruits and veggies a day";
}
else
if(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();
}
Last edited on
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?
Last edited on
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
okay so i tried to fix a little but i found out that the formula doesn't work at all
i'm about to give up

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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.";
}
else
if (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";
}
else
if(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();
}
closed account (E0p9LyTq)
Try declaring your three int variables as float or double.

And PLEASE, learn to use indentation. Your current layout is very hard to read.
1
2
3
4
5
6
7
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.";
}
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  char gender;
  int 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 = double(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.";
  }
  else if (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";
  }
  else if (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";
  }

What is gender for - you don't use it.
Also consider using a modern compiler.
#include<iostream.h> was used 20+ years ago
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


it suppose to be 18.2, underweight ...
Sorry, weight and height need to be double as well.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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.";
  }
  else if (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";
  }
  else if (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.
it worked.. thank you so much seriously
Topic archived. No new replies allowed.