Program ignores my if/else statements when I run it. Please Help

Hi all :) Need help fixing code to calculate male and female body fat percentages. Should a switch structure be used? Please show me what I am doing wrong. Here is what I got:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
int bodyWeight;
double bodyFatPercent,bodyFat;
double wristM,waistM,hipM,foreArmM;
double a1,a2,a3,a4,a5,b;
string gender;
string female;
string male;

cout << "Enter your body weight: " ;
cin >> bodyWeight;
cout << endl;

cout << "\nEnter your wrist measurement: " ;
cin >> wristM;
cout << endl;

cout << "\nEnter your gender: ";
cin >> female;
cout << endl;

if ( gender == female)
{
cout << "\nEnter your hip measurement: ";
cin >> hipM;
cout << endl;

cout << "\nEnter your waist measurement: ";
cin >> waistM;
cout << endl;

cout << "Enter your forearm measurement: ";
cin >> foreArmM;
cout << endl;

a1 = (bodyWeight * 0.732);
a2 = wristM * 3.140;
a3 = waistM * 0.157;
a4 = hipM * 0.249;
a5 = foreArmM * 0.434;

b = a1 + a2 - a3 - a4 + a5;
bodyFat = bodyWeight - b;
bodyFatPercent = bodyFat * 100 / bodyWeight;

cout << "\nYour body fat percentage is: " <<bodyFatPercent <<endl;
}
else if (gender == male)
{
a1 = (bodyWeight * 1.082) + 94.42;
a2 =wristM * 4.15;

b = a1 - a2;
bodyFat = bodyWeight - b;
bodyFatPercent = bodyFat * 100 / bodyWeight;

cout << "\nYour body fat is: " << bodyFatPercent << endl;
}

system("pause");


return 0;
}
Last edited on
Hey,

maybe this part:

1
2
3
cout << "\nEnter your gender: ";
cin >> female;
cout << endl;


should be:

1
2
3
cout << "\nEnter your gender: ";
cin >> gender;
cout << endl;


Hope that helped.
hi thanks for replying. I replaced the part you suggested with :


cout << "\nEnter your gender: ";
cin >> gender;
out << endl;


now the program stops after the user types in their gender. Never evaluated the body percentages.

any other ideas?
Use code tags.


and change this:
if ( gender == female)
to this:
if ( gender == "female")

and do the same for the male check too.


OR
declare male and female strings like this:
1
2
	const string female("female");
	const string male("male");
Last edited on
Hey you are comparing two string variables if ( gender == female) where female is not initialized.

Try this:
1
2
3
4
5
6
7
8
9
10
string female = "F";
string male = "M";
if ( gender == female)
{
    //
}
else if (gender == male)
{
    //
}

or
1
2
3
4
5
6
7
8
if ( gender == "F")
{
    //female
}
else if (gender == "M")
{
    //male
}
Last edited on
You guys are awesome. The female calculations are correct. The male calculations are in the negative numbers, which is wrong. Here is my revised code, why is the user getting a negative number for the male calculations ?

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

const string female ("female");
const string male ("male");

int main()
{
int bodyWeight;
double bodyFatPercent,bodyFat;
double wristM,waistM,hipM,foreArmM;
double a1,a2,a3,a4,a5,b;
string gender;
string female;
string male;

cout << "Enter your body weight: " ;
cin >> bodyWeight;
cout << endl;

cout << "\nEnter your wrist measurement: " ;
cin >> wristM;
cout << endl;

cout << "\nEnter your gender: ";
cin >> gender;
cout << endl;

if ( gender == "female")
{
cout << "\nEnter your hip measurement: ";
cin >> hipM;
cout << endl;

cout << "\nEnter your waist measurement: ";
cin >> waistM;
cout << endl;

cout << "Enter your forearm measurement: ";
cin >> foreArmM;
cout << endl;

a1 = (bodyWeight * 0.732);
a2 = wristM * 3.140;
a3 = waistM * 0.157;
a4 = hipM * 0.249;
a5 = foreArmM * 0.434;

b = a1 + a2 - a3 - a4 + a5;
bodyFat = bodyWeight - b;
bodyFatPercent = bodyFat * 100 / bodyWeight;

cout << "\nYour body fat percentage is: " <<bodyFatPercent <<endl;
}
else if (gender == "male")
{
a1 = (bodyWeight * 1.082) + 94.42;
a2 =wristM * 4.15;

b = a1 - a2;
bodyFat = bodyWeight - b;
bodyFatPercent = bodyFat * 100 / bodyWeight;

cout << "\nYour body fat is: " << bodyFatPercent << endl;
}

system("pause");

[/b]
return 0;
}
Please use code tags when posting code, as mutexe asked you to do.
Okay sorry, just that I am super new and had to google what a code tag is. hope this is better. My math calculations are wrong. why? :


1
2
3
b = a1 + a2 - a3 - a4 + a5;
bodyFat = bodyWeight - b;
bodyFatPercent = bodyFat * 100 / bodyWeight


correct female percentages outputed







1
2
3
4
5
6
a1 = (bodyWeight * 1.082) + 94.42;
a2 =wristM * 4.15;

b = a1 - a2;
bodyFat = bodyWeight - b;
bodyFatPercent = bodyFat * 100 / bodyWeight;


wrong male percentages( negative number) outputed
I'd recommend stepping through the calculation code with a debugger. That way you can see exactly what value each variable has at any given point, which should give you an insight into what's going wrong.

My guess would be that b is somehow coming out to be bigger than bodyWeight, so that bodyWeight - b is negative.
Im totally stumped. Maybe the book Im reading to produce this code has a bug. I will continue switching numbers around.
What units are you working in?
Is body weight in tonnes??
Is wrist measurement in millimetres?

I think you should tell the user what units to use when asking them stuff.

Last edited on
Did you try stepping through the code with a debugger?
Looks like your 'bodyFat' variable will always be -ve due to the relationship between 'bodyWeight' and 'a1'.

edit:
This line:
a2 =wristM * 4.15;

should use your waist NOT your wrist, if my powers of googling are working.
The male calculation does not use wrist at all I believe.
Last edited on
good point. i didn't specify in the code. it would be us units. weight in pounds and wrist measurement in inches :


1
2
3
4
5
6
a1 = (bodyWeight * 1.082) + 94.42;
a2 =wristM * 4.15;

b = a1 - a2;
bodyFat = bodyWeight - b;
bodyFatPercent = bodyFat * 100 / bodyWeight;



getting negative number
okay im gonna edit that line mutexe. brb
no it's not units. read my edit sorry.

you will now need to ask for waist measurement before checking for gender now (as both male and female use it).
Topic archived. No new replies allowed.