C++ BMI Calculator Project
Compute Body Mass Index from height and weight, with a health category.
What you will build
Enter height and weight, get a BMI value and its category — underweight, healthy, overweight, obese. A tiny program, but it teaches clean numeric input, unit choices, and mapping a continuous value onto labelled ranges, a pattern you'll reuse constantly.
What you will learn
- Floating-point arithmetic and unit conversion
- Chained if/else range classification
- Formatting output to fixed decimal places
- Sanity-checking numeric input
Feature checklist
- Accepts metric (kg, m) input
- Calculates BMI = kg / m²
- Prints the standard WHO category
- Rounds output to one decimal place
- Rejects zero or negative height/weight
Starter code
#include <iostream>
#include <iomanip>
std::string category(double bmi) {
if (bmi < 18.5) return "Underweight";
if (bmi < 25.0) return "Healthy weight";
if (bmi < 30.0) return "Overweight";
return "Obese";
}
int main() {
double weightKg, heightM;
std::cout << "Weight (kg): "; std::cin >> weightKg;
std::cout << "Height (m): "; std::cin >> heightM;
if (weightKg <= 0 || heightM <= 0) {
std::cout << "Values must be positive.\n";
return 1;
}
double bmi = weightKg / (heightM * heightM);
std::cout << "BMI: " << std::fixed << std::setprecision(1)
<< bmi << " (" << category(bmi) << ")\n";
}Full walkthrough
Step 1: Write the category() function first
category(double bmi) maps a number onto a label with a chain of ifs: below 18.5 'Underweight', below 25 'Healthy weight', below 30 'Overweight', otherwise 'Obese'. Notice the thresholds only need the upper bound of each band — because the previous if already excluded everything below it, each test asks just one question. That cascading-range pattern appears in grading, tax brackets, and shipping rates everywhere.
Step 2: Read and validate the two inputs
main() prompts 'Weight (kg): ' and 'Height (m): ' into two doubles. Immediately reject weightKg <= 0 or heightM <= 0 with 'Values must be positive.' and return 1 — a zero height would divide by zero in the next step, and a negative weight is a typing error, not a person.
if (weightKg <= 0 || heightM <= 0) {
std::cout << "Values must be positive.\n";
return 1;
}Step 3: Compute BMI with the real formula
bmi = weightKg / (heightM * heightM). Write the square as heightM * heightM, not heightM ^ 2 — in C++ the ^ operator is bitwise XOR, not power, and it will happily compile while producing nonsense. (std::pow from <cmath> exists, but multiplication is faster and clearer for a square.)
Step 4: Format the answer to one decimal place
BMI is quoted to one decimal, so print with std::fixed << std::setprecision(1) from <iomanip>, then append the category in parentheses: 'BMI: 22.9 (Healthy weight)'. fixed matters: without it, setprecision counts significant digits instead of decimal places and values print inconsistently.
Step 5: Test at the category boundaries
Use 70 kg at 1.75 m: 70 / 3.0625 = 22.9, Healthy. Then probe the edges — a BMI of exactly 25.0 must be Overweight (the code says bmi < 25.0 is Healthy, so 25.0 falls through correctly), and 18.4 must be Underweight. Boundary tests are where cascading ifs earn their keep.
Step 6: Sanity-check the units story
The formula expects kilograms and metres. If your users think in centimetres, either prompt for metres clearly (the starter does) or divide a cm input by 100 before squaring — forgetting that conversion inflates BMI by 10,000×, the most dramatic wrong answer this tiny program can produce.
Common mistakes
- Using ^ for squaring — it compiles, and it is XOR, so the result is garbage that looks like a number.
- Writing height in centimetres into a formula that expects metres, producing a BMI in the hundred-thousands.
- Testing bmi <= 25 for Healthy, which quietly moves the WHO boundary and misclassifies exactly-25.0 users.
Compile and run
g++ -std=c++17 -o bmi bmi.cppSample run
$ ./bmi
Weight (kg):
> 70
Height (m):
> 1.75
BMI: 22.9 (Healthy weight)
$ ./bmi
Weight (kg):
> 95
Height (m):
> 1.75
BMI: 31.0 (Obese)
$ ./bmi
Weight (kg):
> -3
Height (m):
> 1.75
Values must be positive.Try it in your browser
This project has a browser demo in the classic site. The demo is a browser re-implementation of the same logic, not the compiled C++ program: Enter height and weight to get a BMI value and its WHO category — demo only, not medical advice.