My program isnt running how i need it to. Help?

im supposed to 1. Write a function that computes and returns a persons Body Mass \

Where weight is measured in pounds and height is measured in inches.

2. Create a Body Mass Index calculator UI where the user can type in his weight and height in feet and inches.

3. Look up the U.S. Department of Health and Human Services BMI categories for adults. Categorize the users information as Underweight, Normal, Overweight, or Obese based on BMI.

and when i run the program i get all of the options my if and else isnt working how i hoped.. help? thanks in advance

---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  //Body Mass Index

#include<iostream>
using namespace std;
#include<cmath>


float BMI( float weight, float hieght);




int main()

{


	cout << "  please enter your weight in pounds" << endl;
		float w;
		cin >>w;

	cout << "please enter your height in inches" << endl;
		float h;
		cin>> h;

	

	if (w,h < 18.5 )
	{
		cout  << " You are underweight" << endl;

	}

	else (w,h >18.5, w,h < 24.9);
	{
		cout << " your normal weight" << endl;

	}
	
	 if ( w,h >= 25, w,h < 29.9)
	 {
		cout << " you are overweight " << endl;

		}
	else (w,h > 30);
	{ 
		cout<< " you are obese " << endl;
	}

	return(0);
	}


float BMI (float weight, float hieght)

{

	float b= (weight * 703) / pow(hieght,2);

	return (b);

}
Here is an example, which purposely does not use your formulas and variables. You also might want to look at comparing the value of b to the criteria for underweight/overweight etc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

void function(double area, double circumference, double radius);

int main()
{
    double r = -1;
    cout << "Welcome to this program:\n";
    cout << "Please enter the radius: ";
    while(r < 0)
    {
        cin >> r;
    }
    function(0,0,r); //We have to call the function we wrote! 
    return 0;
}

void function(double area, double circumference, double radius)
{
    area = radius * 3.141;
    circumference = 3.141 * 2 * radius;
    cout << "Area: " << area << "\nCircumference: " << circumference << "\n";
}
Lines 28, 34, 40, 45 - You can't put two variables on the left side of a comparison.
1
2
 
if (w,h < 18.5 )


Should be:
 
if (w < 18.5 )

etc.
im still confused so i fixed the two variables and it still does the same thing
Can we see your modified code?

//Body Mass Index

#include<iostream>
using namespace std;
#include<cmath>


float BMI( float weight, float hieght);

float bmi;


int main()

{


cout << " please enter your weight in pounds" << endl;
float w;
cin >>w;

cout << "please enter your height in inches" << endl;
float h;
cin>> h;



if (bmi < 18.5)
if (bmi >= 30)
cout << "Obesity";
else if (bmi >= 25)
cout << "Overwieght";
else if (bmi >= 18.5)
cout << "Normal weight";
else
cout << "Underweight";

return(0);
}


float BMI (float weight, float hieght)

{

float bmi= (weight * 703) / pow(hieght,2);

return (bmi);

}
You need to call your BMI function, and you don't need this if (bmi < 18.5)
Last edited on
i thought i did, i introduced the function and executed it at the bottom its not being called in the main function?
A C++ program does this (basically).

Look for main
do all the operations in main
finish

So if you don't put a call to your function in main, it won't happen. Look in my example. In line 16 in main, we call up the function. Otherwise, it would never get run!
its not being called in the main function?

At line 29, you reference the uninitialized variable bmi.
Your code is going to fail because you have never initialized this variable.

You never call the function BMI to do the calculation.

Your if logic is bogus. I've added braces, indentation and comments.
1
2
3
4
5
6
7
8
9
10
if (bmi < 18.5)
{  if (bmi >= 30)  // this can never be true 
        cout << "Obesity"; 
    else if (bmi >= 25)   // or this
        cout << "Overwieght"; 
    else if (bmi >= 18.5) // or this
        cout << "Normal weight"; 
}
else 
    cout << "Underweight";


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

Topic archived. No new replies allowed.