![]() |
|
[C++] Need help with this body fat calculating program - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: [C++] Need help with this body fat calculating program (/Thread-C-Need-help-with-this-body-fat-calculating-program) |
[C++] Need help with this body fat calculating program - OversouL - 08-02-2015 Can somebody tell me what I did wrong? I'm not getting correct answers. This should be giving me the body fat and body fat percentage. I'd appreciate some help ASAP. It would probably be too late by the time I get an answer, but what the heck. XD Problem: Quote:One way to determine how healthy a person is by measuring the body fat Code: #include <iostream>
using namespace std;
int main()
{
char gender;
double bodyweight, wrist, waist, hip, forearm, B, bodyfat, bfPercentage, A1, A2, A3, A4, A5;
bodyfat = bodyweight - B;
bfPercentage = (bodyfat * 100) / bodyweight;
cout << "Input gender (m/f): ";
cin >> gender;
switch (gender)
{
case 'F':
case 'f':
cout << "Enter body weight: ";
cin >> bodyweight;
cout << "Enter wrist measurement (at fullest point): ";
cin >> wrist;
cout << "Enter waist measurement (at navel): ";
cin >> waist;
cout << "Enter hip measurement (at fullest point): ";
cin >> hip;
cout << "Enter forearm measurement (at fullest point): ";
cin >> forearm;
A1 = (bodyweight * 0.723) + 8.987;
A2 = (wrist) / 3.140;
A3 = (waist) * 0.157;
A4 = (hip) * 0.249;
A5 = (forearm) * 0.434;
B = A1 + A2 - A3 - A4 + A5;
cout << "Your body fat is: " << bodyfat << endl
<< "Body fat percentage: " << bfPercentage << "%";
break;
case 'M':
case 'm':
cout << "Enter body weight: ";
cin >> bodyweight;
cout << "Enter wrist measurement (at fullest point): ";
cin >> wrist;
A1 = (bodyweight * 1.082) + 94.42;
A2 = wrist * 4.14;
B = A1 - A2;
cout << "Your body fat is: " << bodyfat << endl
<< "Body fat percentage: " << bfPercentage << "%";
break;
default:
cout << "Invalid gender.";
}
return 0;
}RE: [C++] Need help with this body fat calculating program - Dyme - 08-02-2015 (08-02-2015, 03:54 PM)OversouL Wrote: You realize you're performing calculations using the "bodyweight" and "B" variables before they are even assigned a value, right? RE: [C++] Need help with this body fat calculating program - lux - 08-02-2015 (08-02-2015, 11:13 PM)Dyme Wrote: You realize you're performing calculations using the "bodyweight" and "B" variables before they are even assigned a value, right? To expand on this, bodyfat = bodyweight - B; does not act as a placeholder for bodyweight - B, it takes the values at the present time and performs the calculation. When bodyfat is instantiated, bodyweight and B are undefined, so therefor bodyfat = undefined. I may be completely incorrect, I don't use CPP, and I am only assuming this is the issue. RE: [C++] Need help with this body fat calculating program - Dyme - 08-03-2015 (08-02-2015, 11:57 PM)Lux Wrote: To expand on this, stfu you just repeated what I said gtfo of here lux aka fux u RE: [C++] Need help with this body fat calculating program - Eclipse - 08-03-2015 (08-03-2015, 12:14 AM)Dyme Wrote: stfu you just repeated what I said gtfo of here lux aka fux u Down boy. RE: [C++] Need help with this body fat calculating program - Dyme - 08-03-2015 (08-03-2015, 01:09 AM)eclipse Wrote: Down boy. what are you his dad RE: [C++] Need help with this body fat calculating program - Eclipse - 08-03-2015 (08-03-2015, 04:11 AM)Dyme Wrote: what are you his dad why do u have to be like this son RE: [C++] Need help with this body fat calculating program - OversouL - 08-03-2015 (08-02-2015, 11:13 PM)Dyme Wrote: You realize you're performing calculations using the "bodyweight" and "B" variables before they are even assigned a value, right? Oh, ok. I thought I can just put it there so I can use it later on. Is this fine now, or is there a better way to write it? Should I initiate bodyfat and bfPercentage in both cases or I can just initialize it in the first case? I could try it myself, but I'm not home, I don't have a compiler. I'd appreciate an answer. ![]() Code: #include <iostream>
using namespace std;
int main()
{
char gender;
double bodyweight, wrist, waist, hip, forearm, B, bodyfat, bfPercentage, A1, A2, A3, A4, A5;
cout << "Input gender (m/f): ";
cin >> gender;
switch (gender)
{
case 'F':
case 'f':
cout << "Enter body weight: ";
cin >> bodyweight;
cout << "Enter wrist measurement (at fullest point): ";
cin >> wrist;
cout << "Enter waist measurement (at navel): ";
cin >> waist;
cout << "Enter hip measurement (at fullest point): ";
cin >> hip;
cout << "Enter forearm measurement (at fullest point): ";
cin >> forearm;
A1 = (bodyweight * 0.723) + 8.987;
A2 = (wrist) / 3.140;
A3 = (waist) * 0.157;
A4 = (hip) * 0.249;
A5 = (forearm) * 0.434;
B = A1 + A2 - A3 - A4 + A5;
bodyfat = bodyweight - B;
bfPercentage =(bodyfat * 100) / bodyweight;
cout << "Your body fat is: " << bodyfat << endl
<< "Body fat percentage: " << bfPercentage << "%";
break;
case 'M':
case 'm':
cout << "Enter body weight: ";
cin >> bodyweight;
cout << "Enter wrist measurement (at fullest point): ";
cin >> wrist;
A1 = (bodyweight * 1.082) + 94.42;
A2 = wrist * 4.14;
B = A1 - A2;
bodyfat = bodyweight - B;
bfPercentage =(bodyfat * 100) / bodyweight;
cout << "Your body fat is: " << bodyfat << endl
<< "Body fat percentage: " << bfPercentage << "%";
break;
default:
cout << "Invalid gender.";
}
return 0;
}RE: [C++] Need help with this body fat calculating program - Shebang - 08-03-2015 (08-03-2015, 02:11 PM)OversouL Wrote: Oh, ok. I thought I can just put it there so I can use it later on. There may be a better way of going about this, however the way you've done it is perfectly fine! It doesn't have anything useless in it, all of your variables are clearly labeled, and it does what it says on the tin. Well done
|