Code:
#include <iostream>
#include <string>
using namespace std;
void getInput (int& weight, int& height, int& age, char& sex, string& activity_lvl);
double calculate_bmr (int weight, int height, int age, char sex);
double increase_bmr (string activity_lvl, double sum1);
void outputResults (double sum2, int weight, int height, int age, string full_sex, string activity_lvl);
int main()
{
int height;
int weight;
int age;
char sex;
string activity_lvl;
string full_sex;
char again = 'Y';
double sum1;
double sum2;
while (again == 'Y' || again == 'y')
{
getInput (weight, height, age, sex, activity_lvl);
calculate_bmr (weight, height, age, sex);
sum1 = calculate_bmr (weight, height, age, sex);
increase_bmr (activity_lvl, sum1);
sum2 = increase_bmr (activity_lvl, sum2);
outputResults (sum2, weight, height, age, full_sex, activity_lvl);
cout << "Another y/n --> ";
cin >> again;
if (again != 'Y' && again != 'y' && again != 'N' && again != 'n')
{
cout << "Please enter a valid response: Another y/n --> ";
cin >> again;
}
}
return (0);
}
void getInput (int& weight, int& height, int& age, char& sex, string& full_sex, string& activity_lvl)
{
using namespace std;
cout << "Enter the height in inches --> ";
cin >> height;
cout << "Enter the weight in pounds --> ";
cin >> weight;
cout << "Enter the age --> ";
cin >> age;
cout << "Enter the sex (M for male and F for female) --> ";
cin >> sex;
while (sex != 'm' && sex != 'M' && sex != 'f' && sex != 'F')
{
cout << "Please enter a valid sex (M for male and F for female) --> ";
cin >> sex;
}
if (sex == 'M' || sex == 'm')
full_sex = "Male";
else if (sex == 'F' || sex == 'f')
full_sex = "Female";
cout << "Enter the activity level"
<< endl << endl
<< " Sedentary Somewhat Active Highly --> ";
cin >> activity_lvl;
while (activity_lvl != "Sedentary" && activity_lvl != "sedentary" &&
activity_lvl != "Somewhat" && activity_lvl != "somewhat" &&
activity_lvl != "Active" && activity_lvl != "active" &&
activity_lvl != "Highly" && activity_lvl != "highly")
{
cout << "Please enter a valid activity level"
<< endl << endl
<< " Sedentary Somewhat Active Highly --> ";
cin >> activity_lvl;
}
return;
}
double calculate_bmr (int weight, int height, int age, char sex)
{
double sum1 = 0;
if (sex == 'F' || sex == 'f')
sum1 = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age);
else if (sex == 'M' || sex == 'm')
sum1 = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age);
return (sum1);
}
double increase_bmr (string activity_lvl, double sum1)
{
double sum2 = 0;
if (activity_lvl == "Sedentary" || activity_lvl == "sedentary")
sum2 = sum1 * 1.2;
if (activity_lvl == "Somewhat" || activity_lvl == "somewhat")
sum2 = sum1 * 1.3;
if (activity_lvl == "Active" || activity_lvl == "active")
sum2 = sum1 * 1.4;
if (activity_lvl == "Highly" || activity_lvl == "highly")
sum2 = sum1 * 1.5;
return (sum2);
}
void outputResults (double sum2, int weight, int height, int age, string full_sex, string activity_lvl)
{
using namespace std;
cout << "A " << full_sex << " with the weight "
<< weight << " pounds, height " << height
<< " inches " << age << " years of age and with "
<< activity_lvl << " activity level has a BMR of "
<< sum2 << "." << endl << endl;
return;
}