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
Let's get user input for weight:
Followed by user input
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:
Now let's display the user's BMI.
[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.
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!
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): ";
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);
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 Thinness | 0 - 24.9 |
Normal | 24.9 - 29.9 |
Overweight | 29.9 - up |
Obese | up |
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!