/*
Programmer: Jennifer Leopold
Date: February 23, 2016
File: hw5.cpp
Purpose: Simulate a pharmacy where the user chooses an item
from a menu, and the program outputs the item
ingredients.
To compile: g++ hw5.cpp -o hw5
To execute: ./hw5
*/
#include <iostream>
#include <ctime>
using namespace std;
//***** Constant declarations *****
// Ingredients for the various items
const string INGREDIENT_1 = "sugar";
const string INGREDIENT_2 = "Karo syrup";
const string INGREDIENT_3 = "baking soda";
const string INGREDIENT_4 = "rotgut vodka";
const string INGREDIENT_5 = "ground acorn hulls";
const string INGREDIENT_6 = "rendered possum fat";
const string INGREDIENT_7 = "used motor oil";
// Amounts for the various ingredients for each item
const short ASPIRIN_AMT_INGREDIENT_1 = 15;
const short ASPIRIN_AMT_INGREDIENT_3 = 2;
const short ASPIRIN_AMT_INGREDIENT_5 = 6;
const short SNAKEOIL_AMT_INGREDIENT_2 = 2;
const short SNAKEOIL_AMT_INGREDIENT_4 = 2;
const short SNAKEOIL_AMT_INGREDIENT_7 = 5;
const short MOUTHWASH_AMT_INGREDIENT_2 = 1;
const short MOUTHWASH_AMT_INGREDIENT_4 = 2;
const short MOUTHWASH_AMT_INGREDIENT_7 = 5;
// Min and max age (for validating user input)
const short MIN_AGE = 0;
const short MAX_AGE = 120;
// Min and max # toes (for validating user input)
const short MIN_TOES = 0;
const short MAX_TOES = 10;
// Cutt-off age for determining ingredient inclusion in
// mouthwash
const short MOUTHWASH_CUTOFF_AGE = 12;
// Factors for determining amount of possum fat
const short TOOTHPASTE_POSSUM_FAT = 4;
const short SNAKEOIL_POSSUM_FAT = 6;
// Message that's output preceding each item ingredient list
const string PRESCRIPTION_MSG = "\nOK, we need to mix up:\n";
//***** Function prototypes *****
void outputGreeting();
void aspirin();
void snakeOil();
void mouthwash();
void toothpaste();
int getIntInput(const string prompt, const int lowerBound,
const int upperBound);
int calculatePossumFatAmount(const int personalAttribute,
const int prescriptionFactor);
void outputSignOff();
//***** Main program starts here *****
int main()
{
bool quit = false; // true when user wants to quit
short option; // menu option
// Display a greeting
outputGreeting();
// Display menu and get user input until s/he wants to quit
do
{
cout << "\nPrescription Recipes:\n";
cout << "1. Aspirin\n";
cout << "2. Snake oil\n";
cout << "3. Mouthwash\n";
cout << "4. Toothpaste\n";
cout << "5. Quit\n";
cout << "What do you want (1-5): ";
cin >> option;
switch (option)
{
case 1 : aspirin();
break;
case 2 : snakeOil();
break;
case 3 : mouthwash();
break;
case 4 : toothpaste();
break;
case 5 : quit = true;
break;
default : cout << "C'mon, y'all gots to pick one of"
<< " the above options!\n";
}
} while (! quit);
// Display a sign-off message
outputSignOff();
return 0;
}
//***** Function definitions *****
/*
Output greeting message (i.e., when user first starts
program).
*/
void outputGreeting()
{
cout << "\nWelcome to Cletus' Farmassy!\n";
return;
}
/*
Output ingredients for aspirin.
*/
void aspirin()
{
cout << PRESCRIPTION_MSG;
cout << ASPIRIN_AMT_INGREDIENT_1 << " grams "
<< INGREDIENT_1 << endl
<< ASPIRIN_AMT_INGREDIENT_3 << " grams "
<< INGREDIENT_3 << endl
<< ASPIRIN_AMT_INGREDIENT_5 << " grams "
<< INGREDIENT_5 << endl;
return;
}
/*
Output ingredients for aspirin based on input for # toes.
*/
void snakeOil()
{
int toes = getIntInput("number of toes", MIN_TOES, MAX_TOES);
cout << PRESCRIPTION_MSG;
cout << SNAKEOIL_AMT_INGREDIENT_2 << " tbls "
<< INGREDIENT_2 << endl
<< calculatePossumFatAmount(toes, SNAKEOIL_POSSUM_FAT)
<< " tbls " << INGREDIENT_6 << endl
<< SNAKEOIL_AMT_INGREDIENT_7 << " tbls "
<< INGREDIENT_7 << endl
<< SNAKEOIL_AMT_INGREDIENT_4 << " liters "
<< INGREDIENT_4 << endl;
return;
}
/*
Output ingredients for mouthwash based on input for age.
*/
void mouthwash()
{
int age = getIntInput("age", MIN_AGE, MAX_AGE);
cout << PRESCRIPTION_MSG;
if (age < MOUTHWASH_CUTOFF_AGE)
cout << MOUTHWASH_AMT_INGREDIENT_2 << " liter "
<< INGREDIENT_2 << endl;
else cout << MOUTHWASH_AMT_INGREDIENT_4 << " liters "
<< INGREDIENT_4 << endl;
cout << MOUTHWASH_AMT_INGREDIENT_7 << " tbls "
<< INGREDIENT_7 << endl;
return;
}
/*
Output ingredients for toothpaste based on input for age.
*/
void toothpaste()
{
int age = getIntInput("age", MIN_AGE, MAX_AGE);
aspirin();
cout << calculatePossumFatAmount(age, TOOTHPASTE_POSSUM_FAT)
<< " tbls " << INGREDIENT_6 << endl;
return;
}
/*
Output specified prompt, and get input for an integer in
specified range lowerBound..upperBound (inclusive). Entered
integer is returned.
*/
int getIntInput(const string prompt, const int lowerBound,
const int upperBound)
{
int x;
bool invalidEntry;
do
{
cout << "\nPlease enter your " << prompt << ": ";
cin >> x;
invalidEntry = (x < lowerBound) || (x > upperBound);
if (invalidEntry)
cout << "Now, dagnabbit, that just can't be!\n";
} while (invalidEntry);
return(x);
}
/*
Calculate amount of possum fat to use as product of
specified personalAttribute (e.g., age) and a pre-
scription factor. Calculated value is returned.
*/
int calculatePossumFatAmount(const int personalAttribute,
const int prescriptionFactor)
{
return(personalAttribute * prescriptionFactor);
}
/*
Output sign-off message (i.e., when user quits program).
*/
void outputSignOff()
{
cout << "\nY'all have a real nice day!\n";
return;
}