QUESTION TWO (2)

The Weight Loss Journal defines underweight as a Body Mass Index (BMI) of 18 or less, overweight as a BMI of 25 to 29.9, and obesity as a BMI of 30 and above.

Using this information, write a [tex]$C++$[/tex] program to compute the BMI (given as [tex]$\frac{\text{weight}}{\text{height}^2}$[/tex]) of a patient given his or her weight as input from the keyboard. Your program should also determine and display the appropriate weight category of the patient onto the standard output stream. Additionally, your program should validate user input and display an appropriate error message if necessary.



Answer :

Sure, let's break down the steps required to write a C++ program to compute Body Mass Index (BMI) and determine the weight category.

### 1. Understand the BMI Calculation
BMI is calculated using the formula:
[tex]\[ \text{BMI} = \frac{\text{weight (kg)}}{\text{height (m)}^2} \][/tex]

### 2. Weight Categories
Based on the BMI value, we classify the BMI into the following categories:
- Underweight: BMI of 18 or less
- Normal weight: BMI between 18 and 24.9
- Overweight: BMI between 25 and 29.9
- Obesity: BMI of 30 and above

### 3. Steps to Write the Program
1. Prompt the user for input: This includes weight in kilograms and height in meters.
2. Validate the input: Ensure that both values are positive numbers.
3. Calculate the BMI using the given formula.
4. Determine the weight category based on the BMI value.
5. Display the BMI and the weight category.

Here is a C++ program that accomplishes this:

```cpp
#include

int main() {
// Declare variables for weight and height
double weight, height;

// Prompt the user to enter weight
std::cout << "Enter your weight in kilograms: ";
std::cin >> weight;

// Validate weight input
if (weight <= 0) {
std::cout << "Error: Weight must be a positive number." << std::endl;
return 1; // Exit the program with an error code
}

// Prompt the user to enter height
std::cout << "Enter your height in meters: ";
std::cin >> height;

// Validate height input
if (height <= 0) {
std::cout << "Error: Height must be a positive number." << std::endl;
return 1; // Exit the program with an error code
}

// Calculate BMI
double bmi = weight / (height height);

// Determine the weight category
std::string category;
if (bmi <= 18) {
category = "underweight";
} else if (25 <= bmi && bmi <= 29.9) {
category = "overweight";
} else if (bmi >= 30) {
category = "obesity";
} else {
category = "normal weight";
}

// Display the BMI and weight category
std::cout << "Your BMI is: " << bmi << std::endl;
std::cout << "You are classified as " << category << "." << std::endl;

return 0;
}
```

### Explanation:
1. Input Reading:
- Prompts the user to enter their weight and height.
- Uses `std::cin` to capture user input.
- Validates that the inputs are positive numbers before proceeding.

2. BMI Calculation:
- Calculates BMI using the formula `weight / (height
height)`.

3. Weight Category Determination:
- Compares the BMI value against defined thresholds to determine the category.

4. Output Results:
- Outputs the calculated BMI and the corresponding weight category.

This C++ program ensures the user inputs are valid and provides appropriate feedback if they are not. If the inputs are valid, it calculates the BMI, determines the weight category, and displays the results to the user.