Output not displaying correctly for some "if else" statements calculating BMI (body weight category)

Hello,

My C++ program is compiling correctly and providing the correct output for the first two computations (Test Cases #1 & #2), but in computations 3 and 4 (Test Cases #3 & #4), the output is showing the output for Test Case #2 (normal). Perhaps I need another variable?

The instructions read, "Write a program that asks for the height and weight of a person. Calculate and display this person’s BMI. Determine and display this person’s body weight category. The weight is measured in pounds and the height is in inches. BMI = 703 * weight / (height * height). If the "BMI Range" is under 18.5, you are underweight; 18.5 to under 25, your weight is normal; 25 to under 30, you are overweight; and 30 or above, you are obese. Perhaps I need to declare another variable?

The Test cases are as follows:

Test Case #1:

INPUT
height: 70
weight: 110

EXPECTED OUTPUT
BMI: 15.7816
body weight category: underweight


Test Case #2:

INPUT
height: 64
weight: 140

EXPECTED OUTPUT
BMI: 24.0283
body weight category: normal


Test Case #3:

INPUT
height: 66
weight: 170

EXPECTED OUTPUT
BMI: 27.4357
body weight category: overweight


Test Case #4:


INPUT
height: 62
weight: 210

EXPECTED OUTPUT
BMI = 38.4053
body weight category: obese


Here's my code:

#include<iostream>

using namespace std;

int main ()
{
double height = 0.0;
double weight = 0.0;
double bmi = 0.0;

cout << "Enter your height (in inches): ";
cin >> height;

cout << "Enter your weight (in pounds): ";
cin >> weight;
bmi = 703 * weight / (height * height);

if (bmi < 18.5)
{
cout << "Your body weight category is underweight." << endl;
}
else if (bmi = 18.5 < 25)
{
cout << "Your body weight category is normal." << endl;
}
else if (bmi = 25 < 30)
{
cout << "Your body weight category is overweight." << endl;
}
else if (bmi >= 30)
{
cout << "Your body weight category is obese." << endl;
}

system ("pause");
return 0;
}


Any help would be greatly appreciated...

THANKS!
Last edited on
Change the statements

1
2
3
4
else if (bmi = 18.5 < 25)
...
else if (bmi = 25 < 30)
...


to

1
2
3
4
else if (bmi < 25)
...
else if (bmi < 30)
...
Hey Vlad,

I changed the code per your input, but when I enter the Test Case #3 and Test Case #4 data, the output still reads, "Your body weight category is normal." Hmm...

Tate
Show the updated code.
Last edited on
#include<iostream>

using namespace std;

int main ()
{
double height = 0.0;
double weight = 0.0;
double bmi = 0.0;

cout << "Enter your height (in inches): ";
cin >> height;

cout << "Enter your weight (in pounds): ";
cin >> weight;
bmi = 703 * weight / (height * height);

if (bmi < 18.5)
{
cout << "Your body weight category is underweight." << endl;
}
else if (bmi = 18.5 < 25)
{
cout << "Your body weight category is normal." << endl;
}
else if (bmi < 25)
{
cout << "Your body weight category is overweight." << endl;
}
else if (bmi < 30)
{
cout << "Your body weight category is obese." << endl;
}

system ("pause");
return 0;
}
Problem too is that the BMI range needs to be 25 to under 30 for "overweight," and the BMI range needs to be 30 or above for "obese."
You did not update the code as I showed. Shall be

if (bmi < 18.5)
{
cout << "Your body weight category is underweight." << endl;
}
else if (bmi < 25)
{
cout << "Your body weight category is normal." << endl;
}
else if (bmi < 30)
{
cout << "Your body weight category is overweight." << endl;
}
else
{
cout << "Your body weight category is obese." << endl;
}
Oh, I see! Guess I was over-thinking the problem.... Thank you!
Topic archived. No new replies allowed.