BMI Index

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;

cin.get();
return 0;
}

double BMI (int height, int weight)
{
return (double)(weight * 703 / (height * height));
}

Listen to your compiler, it should tell you line numbers too:


(26): error C2143: syntax error : missing ';' before ':'
(27): error C2181: illegal else without matching if
(27): error C2059: syntax error : '<'
(28): error C2143: syntax error : missing ';' before ':'
(29): error C2181: illegal else without matching if
(29): error C2059: syntax error : '<'
(31): error C2181: illegal else without matching if
(32): error C2146: syntax error : missing ';' before identifier 'cout'
(32): error C2143: syntax error : missing ';' before ':'


(you have a bunch of speech marks missing when you try and print stuff out to the screen).

I would also cast your denominator in your function to double, rather than at the end of the calculation.
Last edited on
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;

cin.get();
return 0;
}

{
return (double)(weight * 703 / (height * height));
}
You don't need the semicolons at the end of the lines with the else conditions, and they would need to be else if if you use a condition. Also - you need to include another bmi to complete the condition.

1
2
else if (bmi >18.5 && bmi <24.9 (or 25?)); // <- remove semicolon
else if (bmi >30 ); // <- remove semicolon 


or...

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;
else if (bmi > 25) // you've already taken out bmi > 30, so these will be range > 25 and < 30
    cout << "BMI: " << bmi << " Overweight" << endl;
else if (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.

1
2
3
4
double BMI (int height, int weight) // <-
{
return (double)(weight * 703 / (height * height));
}


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 :)
Last edited on
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;


cin.get();
return 0;
}
double BMI (int height, int weight)
{
return (double)(weight * 703 / (height * height));
}
Last edited on
I think you lost this line somewhere to call the function.

bmi = BMI (height, weight);


Also, you may not have seen this edit to my post - use the >> instead of a comma here, so that you read into two variables.

cin >> height >> weight;
Thank you so much wildblue! It's working correctly now!
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);
Topic archived. No new replies allowed.