Informatics - BMI Calculator (Week 20)

 Do you ever wonder if you're underweight or overweight, but you don't know how to calculate your BMI ( Body Mass Index). Body Mass Index (BMI) is a simple calculation used to determine if a person has a healthy body weight based on their height and weight.

A BMI Calculator program is a simple program that calculates a person's Body Mass Index (BMI) based on their weight and height. It categorizes the BMI result into different health ranges such as Underweight, Normal weight, Overweight, or Obese based on standard BMI classifications. 

Here's the code of BMI Calculator program!

//program BMICalculator.cpp

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

float weight, height_cm, height_m, bmi;
void calculateBMI (){
    height_m = height_cm / 100.0;
    bmi = weight / (height_m * height_m);

    cout << fixed << setprecision(1);
    cout << "Your BMI score is " << bmi << ", which means you are ";

    if (bmi < 18.5){
        cout << "underweight" << endl;
    }else if (bmi >= 18.5 && bmi <= 24.9){
        cout << "normal weight" << endl;
    }else if (bmi >= 25 && bmi <=29.9){
        cout << "overweight" << endl;
    }else {
        cout << "obese" << endl;
    }
}
void getInput (){
bool valid = false;
do {
    cout << "Enter your weight (kg): ";
    cin >> weight;
    if (weight > 0){
        valid = true;
    } else {
    cout << "     !!!INVALID INPUT!!!    \n";
    cout << "NO WAY YOUR WEIGHT IS NEGATIVE\n";
    }
}while (!valid);

valid = false;
do {
    cout << "Enter your height (cm): ";
    cin >> height_cm;
    if (height_cm > 0){
        valid = true;
    }else {
         cout << "        !!!INVALID INPUT!!!          \n";
         cout << "IT'S IMPOSSIBLE YOUR WEIGHT IS NEGATIVE\n";
    }
}while (!valid);
}
int main (){
float weight, height_cm;
char choice;

do {
    cout << "\nBMI Calculator Menu:\n";
    cout << "1. Calculate BMI\n";
    cout << "2. Exit\n";
    cout << "Choose option 1 or 2: ";
    cin >> choice;

    switch (choice){
    case '1' :
       getInput();
       calculateBMI ();
       break;
    case '2' :
       cout << "Exiting program. Bye bye!!!\n";
       return 0;
    default:
       cout << "Invalid choice! Please select 1 or 2.\n";
       return 0;
    }
}while (true);
return 0;
}

This BMI Calculator program is designed to help users determine their Body Mass Index (BMI) based on their weight and height. The program starts by displaying a menu where users can either calculate their BMI or exit the program. If the user chooses to calculate BMI, the program prompts them to enter their weight in kilograms and height in centimeters. To ensure valid input, the program checks if the values are greater than zero; otherwise, it displays an error message and asks for input again.

Once valid inputs are received, the program converts the height from centimeters to meters and then applies the BMI formula: BMI = weight / (height in meters²). The BMI result is then categorized into different health ranges: underweight (BMI < 18.5), normal weight (BMI 18.5–24.9), overweight (BMI 25–29.9), and obese (BMI ≥ 30). The output is displayed with one decimal precision, making it clear and easy to read.

The program uses a do-while loop to keep asking for valid input if incorrect values are provided. It also implements a switch statement to handle the user's menu selection. If the user enters an invalid choice, the program displays an error message and exits. The use of functions (getInput() and calculateBMI()) makes the code well-structured, ensuring that input handling and BMI calculations are separate for better readability and maintainability.

Output:

I choose option 1 which it'll calculate the BMI. Then I enter the weight which is 54kg and the height 165cm. The Result of the BMI score is 19.8 which means normal weight. The menu appears again, and i selects option 2 to exit. The program then prints "Exiting program. Bye bye!!!". 

That's all the BMI Calculator program. I hope it'll help you to count your BMI. Thank you and see you when I see you!

Komentar

Postingan populer dari blog ini

Informatics - Making a Main C++ Program to Run all the Calculator Program (week 11)

Informatics - Triangles (week 18)

Informatics - Making Minimarket Discount Program in C++ version 1 (Week 12)