Solution
// Programmer: Clayton Price
// File: hw5.cpp
// Purpose: this file contains the main function of the program that
#include <cstdlib>
#include <iostream>
using namespace std;
// global constants
const int MIN_LEGS = 1;
const int MIN_DIST_OF_LEG = 20; //constraints on user inputs
const int MAX_DIST_OF_LEG = 120;
const int MIN_ANXIETY = 1;
const int MAX_ANXIETY = 10;
const int MIN_DAY = 1;
const int MAX_DAY = 7;
const int MIN_PILLS = 0;
const int MAX_HOURS_SLEPT = 24;
const int MIN_TEMP = -50;
const int MAX_TEMP = 200;
const int MIN_HEIGHT = 0;
const int MAX_HEIGHT = 96; //8 feet
// prototypes
void greetings();
char menu();
int num_legs_journey();
int the_walk(const int numberLegs);
int get_anxiety();
int get_day();
int calc_meds(const int day, const int anxiety);
void display_meds(const int num_pills);
int get_sleep_hours();
float calc_naptime(const int hours, const int dist, const int pills);
void display_naptime(const float nap_time);
int get_wt();
int get_height();
int get_temp();
float calc_calories(const int ht,const int wt,const int temp,const int dist);
void display_calories(const float cals);
int main()
{
bool quit = false; //use to terminate main loop
char action; //user input for menu
int legsOfWalk; //user input for number of journey segs
int total_distance = 0; //dist of a walk
int today; //integer for the day 1 - 7
int my_anxiety; //int for anx level 1 - 10
int num_pills; //humber of pills to take
int hours_slept; //hours slept last night
float nap_mins; //mins computed to sleeep in naptime
int wt; //user weight
int ht; //user height
int temperature; //user temperature
float cals; //computed calories
bool option1_chosen = false; //flags for menu use
bool option2_chosen = false;
srand(time(NULL));
greetings();
do
{
action = menu();
switch (action) //actions to take for user choice
{
case '1': case 't': //walk stats
legsOfWalk = num_legs_journey();
total_distance = the_walk(legsOfWalk);
option1_chosen = true;
break;
case '2': case 'y': case 'm': //medications
today = get_day();
my_anxiety = get_anxiety();
num_pills = calc_meds(today,my_anxiety);
display_meds(num_pills);
option2_chosen = true;
break;
case '3': case 'n': //naptime computations
if(option1_chosen && option2_chosen)
{
hours_slept = get_sleep_hours();
nap_mins = calc_naptime(hours_slept,total_distance,num_pills);
display_naptime(nap_mins);
}
else
cout<<"\n****ERROR...you must choose options 1 and 2 first!"<<endl;
break;
case '4': case 'c': //caloric intake
if (option1_chosen)
{
wt = get_wt();
ht = get_height();
temperature = get_temp();
cals = calc_calories(wt, ht, temperature, total_distance);
display_calories(cals);
}
else
cout<<"\n****ERROR...you must choose option 1 first!"<<endl;
break;
case '5': case 'q':
quit = true;
break;
default:
cout<<"\n*******INVALID INPUT.....try again"<<endl;
}
} while (!quit);
cout<<"\n\n\tTerminating Health-o-Matic...good luck with your health!!"
<<endl<<endl;
return 0;
}
// the greet function
void greetings()
{
cout<<"\n\n\nWelcome to the Health-o-Matic 500 program! ....."<<endl;
return;
}
// display menu and return response
char menu()
{
char choice;
cout<<"\n\t\tOptions"<<endl
<<"\t\t------"<<endl;
cout<<"1. Taking a Walk"<<endl
<<"2. Medications"<<endl
<<"3. Nap Time"<<endl
<<"4. Caloric Intake"<<endl
<<"5. Quit"<<endl<<endl;
cout<<"\tYou'ns choice? ";
cin>>choice;
return choice;
}
// get input for number of legs of journey
int num_legs_journey()
{
int num_legs;
do
{
cout<<"Please enter a number of legs of the journey greater than "
<<MIN_LEGS<<": ";
cin>>num_legs;
} while(num_legs < MIN_LEGS);
return num_legs;
}
//compute total walking dist and map out path
int the_walk(const int numberLegs)
{
int total_distance_walked = 0;
int segment;
int dir;
for(int i = 1; i <= numberLegs; i++)
{
segment = rand()%(MAX_DIST_OF_LEG - MIN_DIST_OF_LEG + 1) + MIN_DIST_OF_LEG;
dir = rand()%2;
cout<<endl<<segment<<"\t\t"<<(dir ? 'R' : 'L');
total_distance_walked += segment;
}
cout<<endl<<"total distance walked is: "<<total_distance_walked<<" steps"
<<endl;
return total_distance_walked;
}
//get input of anxiety level from user
int get_anxiety()
{
int anx;
do
{
cout<<"Enter your anxiety level("<<MIN_ANXIETY<<" - "<<MAX_ANXIETY<<"): ";
cin>>anx;
} while (anx < MIN_ANXIETY || anx > MAX_ANXIETY);
return anx;
}
//get input of day number from user
int get_day()
{
int day;
do
{
cout<<"Enter the day ("<<MIN_DAY<<" - "<<MAX_DAY<<"): ";
cin>>day;
} while (day < MIN_DAY || day > MAX_DAY);
return day;
}
//compute number of pills for the user
int calc_meds(const int day, const int anxiety)
{
int num_pills = anxiety - day;
if (num_pills < MIN_PILLS)
num_pills = MIN_PILLS;
return num_pills;
}
//output pills to take
void display_meds(const int num_pills)
{
cout<<"\n\tYou should take "<<num_pills<<" pills."<<endl;
return;
}
//input from user time slept night before
int get_sleep_hours()
{
int hours;
do
{
cout<<"Enter the number of hours slept last night: ";
cin>>hours;
} while (hours <= 0 || hours > MAX_HOURS_SLEPT);
return hours;
}
//compute mins to sleep during nap
float calc_naptime(const int hours, const int dist, const int pills)
{
float mins = hours + static_cast<float>(dist)/pills;
return mins;
}
//output nap minutes computed
void display_naptime(const float mins)
{
cout<<"\nYou should sleep "<<mins<<" minutes for a naptime."<<endl;
return;
}
//input wt of user
int get_wt()
{
int wt;
do
{
cout<<"Enter your weight: ";
cin>>wt;
} while (wt <= 0);
return wt;
}
//input ht of user
int get_height()
{
int ht;
do
{
cout<<"Enter your height: ";
cin>>ht;
} while (ht < MIN_HEIGHT || ht > MAX_HEIGHT );
return ht;
}
//input user's temperature
int get_temp()
{
int temp;
do
{
cout<<"Enter the temperature: ";
cin>>temp;
} while (temp < MIN_TEMP || temp > MAX_TEMP);
return temp;
}
//computes caloric intake for user
float calc_calories(const int ht,const int wt,const int temp,const int dist)
{
float cals;
cals = 6*wt + 2*ht + 1.0/temp + dist;
return cals;
}
//output caloric intake for user
void display_calories(const float cals)
{
cout<<"You may consume "<<cals<<" calories now"<<endl;
return;
}