Dec 4, 2013 at 3:18am UTC
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 Dec 4, 2013 at 3:28am UTC
Dec 4, 2013 at 9:45am UTC
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?
Dec 4, 2013 at 10:38am UTC
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 Dec 4, 2013 at 10:40am UTC
Dec 4, 2013 at 10:43am UTC
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 Dec 4, 2013 at 10:44am UTC
Dec 4, 2013 at 2:09pm UTC
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;
}
Dec 4, 2013 at 2:37pm UTC
Please use code tags when posting code, as mutexe asked you to do.
Dec 4, 2013 at 2:58pm UTC
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.
Dec 4, 2013 at 3:30pm UTC
Im totally stumped. Maybe the book Im reading to produce this code has a bug. I will continue switching numbers around.
Dec 4, 2013 at 3:36pm UTC
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 Dec 4, 2013 at 3:39pm UTC
Dec 4, 2013 at 3:38pm UTC
Did you try stepping through the code with a debugger?
Dec 4, 2013 at 3:41pm UTC
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 Dec 4, 2013 at 3:47pm UTC
Dec 4, 2013 at 3:51pm UTC
okay im gonna edit that line mutexe. brb
Dec 4, 2013 at 3:51pm UTC
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).