patterncppModerate
Living off of chocolate
Viewed 0 times
livingoffchocolate
Problem
This is the second project for my CS1 class, this time I'm actually getting it reviewed before I submit it
The Harris-Benedict equation estimates the number of calories your
body needs to maintain your weight if you do no exercise. This is
called your basal metabolic rate, or BMR.
The formula for the calories needed for a woman to maintain her weight
is
$$ \text{BMR} = 655 + \left( 4.3 \cdot \text{weight in pounds} \right) + \left( 4.7 \cdot \text{height in inches} \right) - \left( 4.7 \cdot \text{age in years}\right)$$
The formula for the calories needed for a man to maintain his weight
is
$$ \text{BMR} = 66 + \left( 6.3 \cdot \text{weight in pounds} \right)
+ \left( 12.9 \cdot \text{height in inches} \right) - \left( 6.8 \cdot \text{age in years}\right)$$
A typical chocolate bar will contain around 230 calories. Write a
program that allows the user to input his or her weight in pounds,
height in inches, age in years, and the character 'M' for male and 'F'
for female. The program should then output the number of chocolate
bars that should be consumed to maintain one's weight for the
appropriate sex of the specified weight, height, and age.
```
/**
* @file calorie.cpp
* @brief Determines the number of chocolate bars you need to eat in order to live
* @author syb0rg
* @date 9/12/14
*/
#include
#include
#include
int main()
{
double weight = 0;
double height = 0;
double age = 0;
double bmr = 0;
char gender = 'm';
// get input for weight, re-read input if not a number
std::cout > weight))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits::max(), '\n');
std::cout > height))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits::max(), '\n');
std::cout > age))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits::max(), '\n');
std::cout > gender;
if ('m' ==
;)The Harris-Benedict equation estimates the number of calories your
body needs to maintain your weight if you do no exercise. This is
called your basal metabolic rate, or BMR.
The formula for the calories needed for a woman to maintain her weight
is
$$ \text{BMR} = 655 + \left( 4.3 \cdot \text{weight in pounds} \right) + \left( 4.7 \cdot \text{height in inches} \right) - \left( 4.7 \cdot \text{age in years}\right)$$
The formula for the calories needed for a man to maintain his weight
is
$$ \text{BMR} = 66 + \left( 6.3 \cdot \text{weight in pounds} \right)
+ \left( 12.9 \cdot \text{height in inches} \right) - \left( 6.8 \cdot \text{age in years}\right)$$
A typical chocolate bar will contain around 230 calories. Write a
program that allows the user to input his or her weight in pounds,
height in inches, age in years, and the character 'M' for male and 'F'
for female. The program should then output the number of chocolate
bars that should be consumed to maintain one's weight for the
appropriate sex of the specified weight, height, and age.
calorie.cpp:```
/**
* @file calorie.cpp
* @brief Determines the number of chocolate bars you need to eat in order to live
* @author syb0rg
* @date 9/12/14
*/
#include
#include
#include
int main()
{
double weight = 0;
double height = 0;
double age = 0;
double bmr = 0;
char gender = 'm';
// get input for weight, re-read input if not a number
std::cout > weight))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits::max(), '\n');
std::cout > height))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits::max(), '\n');
std::cout > age))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits::max(), '\n');
std::cout > gender;
if ('m' ==
Solution
Just a quick glance.
But:
That could be a function.
This would reduce code and make the application easier to read.
Remember code should be DRY.
The formula is the same here:
So why not pick the constants from some indexed value:
but pick better names than I did.
Don't like magic numbers (230).
Use a constant that has a name that makes this have meaning.
Don't declare all your variables at the top:
Declare them as you need them as close to the point of first use as possible.
But:
std::cout > age))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits::max(), '\n');
std::cout << "Invalid input. Please enter a number: ";
}That could be a function.
This would reduce code and make the application easier to read.
Remember code should be DRY.
The formula is the same here:
if ('m' == gender)
{
bmr = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age);
}
else if ('f' == gender)
{
bmr = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age);
}So why not pick the constants from some indexed value:
bmr = bmrconst[gender].base
+ (bmrconst[gender].weight * weight)
+ (bmrconst[gender].height * height)
- (bmrconst[gender].age * age);but pick better names than I did.
Don't like magic numbers (230).
const double numBars = bmr / 230;Use a constant that has a name that makes this have meaning.
Don't declare all your variables at the top:
double weight = 0;
double height = 0;
double age = 0;
double bmr = 0;
char gender = 'm';Declare them as you need them as close to the point of first use as possible.
Code Snippets
std::cout << XXXX
while(!(std::cin >> age))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input. Please enter a number: ";
}if ('m' == gender)
{
bmr = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age);
}
else if ('f' == gender)
{
bmr = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age);
}bmr = bmrconst[gender].base
+ (bmrconst[gender].weight * weight)
+ (bmrconst[gender].height * height)
- (bmrconst[gender].age * age);const double numBars = bmr / 230;double weight = 0;
double height = 0;
double age = 0;
double bmr = 0;
char gender = 'm';Context
StackExchange Code Review Q#62797, answer score: 17
Revisions (0)
No revisions yet.