Ask the user to enter their weight (in pounds) and their height in inches. Calculate their Body Mass Index (BMI) and tell them whether they are underweight, overweight, or at optimal weight.
BMI formula: weight * 703 / (height * height)
Optimal weight is a BMI from 19 to 26. Lower is underweight, higher is overweight.
Prompts:
Enter your weight (in pounds):
Enter your height (in inches):
Possible Outputs:
Your BMI is 18.9964, which means you are underweight.
Your BMI is 29.2612, which means you are overweight.
Your BMI is 25.8704, which means you are at optimal weight.
Notes and Hints:
1) For simplicity, assume user entries will be in whole numbers. However, BMI must be calculated with decimals.
2) You must use an if/else if structure for this
3) NOTE: The three exercises in this HW are going to make you think a bit. If you want really basic practice with if/else and related concepts, read your author's examples in section 4.5 - 4.9. He has plenty of easy code (with explanationa) in the main text.
#include <iostream>
using namespace std;
#define MIN_OPTIMAL 19
#define MAX_OPTIMAL 26
int main() {
int weight;
int height;
double bmi;
cout << "Please enter your weight (in pounds): ";
cin >> weight;
if ( weight <= 0 ) {
cout << "Error: the weight must be a positive number greater than zero";
return 0;
}
cout << "Please enter your height (in inches): ";
cin >> height;
if ( height <= 0 ) {
cout << "Error: the height must be a positive number greater than zero";
return 0;
}
bmi = (double)weight * 703 / ((double)height * (double)height);
if ( bmi > MIN_OPTIMAL && bmi < MAX_OPTIMAL ) {
cout << "Your BMI is " << bmi << ", which means you are at optimal weight.\n";
} else if ( bmi < MIN_OPTIMAL ) {
cout << "Your BMI is " << bmi << ", which means you are underweight.\n";
} else if ( bmi > MAX_OPTIMAL ) {
cout << "Your BMI is " << bmi << ", which means you are overweight.\n";
}
return 0;
}
Comments
Leave a comment