I'm trying to write a C++ program that calculates BMI Index. The user has to put both their height in inches and weight at the same time after being asked.
This is what I have so far, but it is not compiling.
#include <iostream>
using namespace std;
double BMI (int height, int weight); //declaring variables
int main()
{
int weight;
int height;
double bmi = 0;
//Showing the BMI chart first
cout << "Underweight <18.5" << endl;
cout << "Normal weight >18.5 and <24.9" << endl;
cout << "Overweight >25 and <29.9" << endl;
cout << "Obese >30" << endl;
//Asking user to input height and weight (mandatory to use single step refinement)
cout << "Enter your height in inches and weight by pounds: ";
cin >> height, weight;
//Trying to have the program calculate the BMI and output the BMI AND category it's under
bmi = BMI (height, weight);
if (bmi <18.5)
cout<< BMI: << bmi <<Underweight<< endl;
else (bmi >18.5 && <24.9);
cout<< BMI: << bmi <<Normal<<endl;
else (bmi >25 && <29.9)
cout<< BMI: << bmi <<Overweight<<endl;
else (bmi >30 )
cout<< BMI: << bmi <<Obese<<endl;
I changed up some of the code but am still getting errors. I see I forgot to add the quotation marks for my cout and have fixed that. Also instead of having an "if and else, else, else" statement, I changed it to two different if elsestatements. I don't understand what I'm doing wrong?
#include <iostream>
using namespace std;
double BMI (int height, int weight); //declaring variables
int main()
{
int weight;
int height;
double bmi = 0;
double BMI (int height, int weight)
//Showing the BMI chart first
cout << "Underweight <18.5" << endl;
cout << "Normal weight >18.5 and <24.9" << endl;
cout << "Overweight >25 and <29.9" << endl;
cout << "Obese >30" << endl;
//Asking user to input height and weight (mandatory to use single step refinement)
cout << "Enter your height in inches and weight by pounds: ";
cin >> height, weight;
//Trying to have the program calculate the BMI and output the BMI AND category it's under
bmi = BMI (height, weight);
if (bmi <18.5)
cout<< "BMI:" << bmi <<"Underweight"<< endl;
else (bmi >18.5 && <24.9);
cout<< "BMI:" << bmi <<"Normal"<<endl;
if (bmi >25 && <29.9)
cout<< "BMI:" << bmi <<"Overweight"<<endl;
else (bmi >30 );
cout<< "BMI:" << bmi <<"Obese"<<endl;
You don't need the semicolons at the end of the lines with the else conditions, and they would need to be elseif if you use a condition. Also - you need to include another bmi to complete the condition.
You could flip the conditions around and have the condition start from the highest number like this.
1 2 3 4 5 6 7 8
if (bmi > 30)
cout << "BMI: " << bmi << " Obese" << endl;
elseif (bmi > 25) // you've already taken out bmi > 30, so these will be range > 25 and < 30
cout << "BMI: " << bmi << " Overweight" << endl;
elseif (bmi > 18.5)
cout << "BMI: " << bmi << " Normal" << endl;
else
cout << "BMI: " << bmi << " Underweight" << endl;
Edit:
Remove this line within main double BMI (int height, int weight) - you have the function prototype outside the main function. But you need the first line of the function definition here if this is supposed to be the BMI function.
cin >> height, weight; // change the , to another >> before weight
If you can highlight the code you post and click the <> button in the Format palette on the right side of the post - it'll format your code for the forum and make it easier to read :)
wildblue, thanks for your help! My program now compiles and runs, but it is not working correctly. After the user inputs their height in inches and weight in pounds, the program always returns a "BMI 0 Underweight" response.
#include <iostream>
using namespace std;
double BMI (int height, int weight); //declaring variables
int main()
{
int weight;
int height;
double bmi = 0;
//Showing the BMI chart first
cout << "Underweight <18.5" << endl;
cout << "Normal weight >18.5 and <24.9" << endl;
cout << "Overweight >25 and <29.9" << endl;
cout << "Obese >30" << endl;
//Asking user to input height and weight (mandatory to use single step refinement)
cout << "Enter your height in inches and weight by pounds: ";
cin >> height, weight;
//Trying to have the program calculate the BMI and output the BMI AND category it's under
if (bmi > 30)
cout << "BMI: " << bmi << " Obese" << endl;
else if (bmi > 25)
cout << "BMI: " << bmi << " Overweight" << endl;
else if (bmi > 18.5)
cout << "BMI: " << bmi << " Normal" << endl;
else
cout << "BMI: " << bmi << " Underweight" << endl;
As mentioned by mutexe you also have an integer division going on. return (double)(weight * 703 / (height * height)); try return weight * 703.0 / (height * height);