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 <iostream>
#include <cctype>
#include <limits>
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 << "Enter the weight in pounds: ";
while(!(std::cin >> weight))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input. Please enter a number: ";
}
// get input for height, re-read input if not a number
std::cout << "Enter the height in inches: ";
while(!(std::cin >> height))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input. Please enter a number: ";
}
// get input for age, re-read input if not a number
std::cout << "Enter your age: ";
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: ";
}
// get input for weight
std::cout << "Enter your gender (M/f): ";
std::cin >> 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 << "You aren't human." << std::endl;
const double numBars = bmr / 230; // divide by the number of calories in a chocolate bar
std::cout << "The person with the entered metrics is allowed to consume " << numBars << " bars of chocolate." << std::endl;
}