Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ How I built my BMI calculator in C++

Malcolm

Administrator
Administrator
Staff Team
Code Plus
Company Plus
Hello Coders!

Today, I want to teach you how to build a calculator to measure your BMI in C++. We will only need to collect two things from the user, you will need their weight (lbs) and then their height (in).

Within our main() we will delcare float variables: float weight; int height; float bmi;. Now, we should probably make our BMI calculator have a welcome message:

C++:
std::cout << "Welcome to BMI calculator" << std::endl;    
std::cout << "Enter your weight (lbs): ";

Let's get user input for weight: std::cin >> weight;. Now we ask the users weight:
C++:
  std::cout << "Enter your height (in): ";
Followed by user input std::cin >> height;

Now we have to calculate the inputted data and give the user their BMI. For this, we will need to initialize the bmi variable. Here's how we are going to do it: We are going to multiply the weight by 703 then divide the height * height. Just like the code below:
C++:
bmi = (703*weight)/(height*height);
Now let's display the user's BMI. std::cout << bmi << std::endl;

[CODE lang="cpp" title="All together"]#include <iostream>

int main() {
float weight;
int height;
float bmi;

std::cout << "Welcome to BMI calculator" << std::endl;
std::cout << "Enter your weight (lbs): ";
std::cin >> weight;

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

bmi = (703*weight)/(height*height);
std::cout << bmi << std::endl;

return 0;
}
[/CODE]

Hmm but let's go a little further, let's tell the user if their BMI is healthy or not. Let's add a function. So we want it where if bmi is equal to or less than display one of the following: severe thinness, normal, overweight and obese. To do this we want to take the bmi and compare it to the standard for BMI.

Severe Thinness0 - 24.9
Normal24.9 - 29.9
Overweight29.9 - up
Obeseup

Now we can do this with the code below, we use else if for more options and when it goes beyond our specific conditions we use else.

[CODE lang="cpp" title="Function"]if(bmi <= 18.5)
{
std::cout << "Severe Thinness\n";
} else if (bmi <= 24.9) {
std::cout << "normal\n";
} else if (bmi <= 29.9) {
std::cout << "Overweight\n";
} else {
std::cout << "Obese\n";
}
[/CODE]

I hope that I inspired you guys to learn, practice, build and share! :)
 

New Threads

Buy us a coffee!

Back
Top Bottom