Living off of chocolate 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.


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 &lt;iostream&gt;
#include &lt;cctype&gt;
#include &lt;limits&gt;

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 &lt;&lt; "Enter the weight in pounds: ";
    while(!(std::cin &gt;&gt; weight))
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
        std::cout &lt;&lt; "Invalid input.  Please enter a number: ";
    }
    // get input for height, re-read input if not a number
    std::cout &lt;&lt; "Enter the height in inches: ";
    while(!(std::cin &gt;&gt; height))
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
        std::cout &lt;&lt; "Invalid input.  Please enter a number: ";
    }
    // get input for age, re-read input if not a number
    std::cout &lt;&lt; "Enter your age: ";
    while(!(std::cin &gt;&gt; age))
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
        std::cout &lt;&lt; "Invalid input.  Please enter a number: ";
    }
    // get input for weight
    std::cout &lt;&lt; "Enter your gender (M/f): ";
    std::cin &gt;&gt; gender;

    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);
    }
    else std::cout &lt;&lt; "You aren't human." &lt;&lt; std::endl;

    const double numBars = bmr / 230; // divide by the number of calories in a chocolate bar
    std::cout &lt;&lt; "The person with the entered metrics is allowed to consume " &lt;&lt; numBars &lt;&lt; " bars of chocolate." &lt;&lt; std::endl;
}
Just a quick glance.
But:

std::cout &lt;&lt; XXXX
while(!(std::cin &gt;&gt; age))
{
    std::cin.clear();
    std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
    std::cout &lt;&lt; "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.