Solution

/*

Programmer: Jennifer Leopold

Date: September 27, 2016

File: hw5.cpp

Purpose: Simulate a carbon footprint survey where the user

enters information about wasteful food consumption,

etc. (as selections from a menu), and the program

calculates a value indicating how guilty they are

of carbon pollution.

To compile: fg++ hw5.cpp -o hw5

To execute: ./hw5

*/

#include <iostream>

using namespace std;

//***** Constant declarations *****

// Menu selections, etc.

const string WASTEFUL_FOOD = "Wasteful (food) Consumption";

const string PUBLIC_TRANSIT = "Public Transit Usage";

const string INDUSTRIAL_COMPLICITY = "Industrial Complicity";

const string METHANE = "Farm-related Methane Production";

const string GUILT = "GUILT VALUE";

const int NUM_MENU_SELECTIONS = 5;

const string ALREADY_DONE_MSG = "You've already done this!";

// Min's and max's for validating user inputs

const int WASTED_FOOD_MIN = 0;

const int WASTED_FOOD_MAX = 100;

const int MILES_TRAVELED_MIN = 1;

const int MILES_TRAVELED_MAX = 250;

const int MIN_AGE = 0;

const int MAX_AGE = 100;

const int PIGS_EATEN_MIN = 0;

const int PIGS_EATEN_MAX = 10;

// Other constants used in formulas for computing guilt, etc.

const int BURNS_INDUSTRIAL_COEFFICIENT = 570;

const float WASTED_FOOD_MULTIPLIER = 1.5;

const int WASTED_FOOD_OFFSET = 6;

const float TRANSIT_USAGE_MULTIPLIER = -2;

const int TRANSIT_USAGE_OFFSET = 1;

//***** Function prototypes *****

void outputGreeting();

void displayMenu();

float wastefulFood();

int publicTransit();

int industrialComplicity();

float methaneProduction(float wastedFood);

float computeGuiltValue(float wastedFood, int transitUsage,

int complicity, float methane);

float adjustValue(float value, float multiplier, int offset);

int getIntInput(string prompt, int MIN_VALUE, int MAX_VALUE);

float getFloatInput(string prompt, int MIN_VALUE, int MAX_VALUE);

bool getInputYN(string prompt);

void outputSignOff();

//***** Main program starts here *****

int main()

{

bool quit = false; // true when user is done

short selection; // menu selection

bool wastefulFoodDone = false; // true when each respective

bool publicTransitDone = false; // menu item has been done

bool industrialCompDone = false;

bool methaneDone = false;

float wastedFood; // values determined by

int transitUsage; // respective menu items

int complicity;

float methane;

float guilt;

// Display a greeting

outputGreeting();

// Display menu and get user input until s/he is done

do

{

displayMenu();

cout << "What do you want to do (1-"

<< NUM_MENU_SELECTIONS << "): ";

cin >> selection;

switch (selection)

{

case 1 : if (wastefulFoodDone)

cout << ALREADY_DONE_MSG << endl;

else

{

wastedFood = wastefulFood();

wastefulFoodDone = true;

}

break;

case 2 : if (publicTransitDone)

cout << ALREADY_DONE_MSG << endl;

else

{

transitUsage = publicTransit();

publicTransitDone = true;

}

break;

case 3 : if (industrialCompDone)

cout << ALREADY_DONE_MSG << endl;

else

{

complicity = industrialComplicity();

industrialCompDone = true;

}

break;

case 4 : if (methaneDone)

cout << ALREADY_DONE_MSG << endl;

else if (!wastefulFoodDone)

cout << "You first must do "

<< WASTEFUL_FOOD << "!\n";

else

{

methane = methaneProduction(wastedFood);

methaneDone = true;

}

break;

case 5 : if (wastefulFoodDone && publicTransitDone &&

industrialCompDone && methaneDone)

{

guilt =

computeGuiltValue(wastedFood, transitUsage,

complicity, methane);

cout << "Your GUILT value is: "

<< guilt << endl;

quit = true;

}

else cout << "You first must complete all of "

<< "the other menu options!\n";

break;

default : cout << "You have to enter one of"

<< " the above selections!\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 << "Welcome to Lisa Simpson's Carbon "

<< "Footprint Survey!\n\n";

return;

}

/*

Output menu of survey selections.

*/

void displayMenu()

{

cout << "\nCarbon Footprint Survey\n"

<< "-----------------------\n";

cout << "1. " << WASTEFUL_FOOD << endl;

cout << "2. " << PUBLIC_TRANSIT << endl;

cout << "3. " << INDUSTRIAL_COMPLICITY << endl;

cout << "4. " << METHANE << endl;

cout << "5. " << GUILT << endl;

return;

}

/*

Prompt for and get input for # lbs of food prepared for, but

not eaten per day. Return the # lbs, which will have been

validated to be > WASTED_FOOD_MIN and < WASTED_FOOD_MAX.

*/

float wastefulFood()

{

float wastedFood;

wastedFood =

getFloatInput("Enter lbs of food prepared but not eaten: ",

WASTED_FOOD_MIN, WASTED_FOOD_MAX);

return(wastedFood);

}

/*

Prompt for and get input for # miles traveled via public

transit per day. Return that # miles, or 0 if public transit

isn't used.

*/

int publicTransit()

{

int milesTraveled;

if (!getInputYN("Do you use public transit?"))

milesTraveled = 0;

else milesTraveled =

getIntInput("Enter # miles traveled by per day: ",

MILES_TRAVELED_MIN, MILES_TRAVELED_MAX);

return(milesTraveled);

}

/*

Return value for complicity, which is based on user's input

for being Mr. Burns, or being related to Mr. Burns, or

the user's age.

*/

int industrialComplicity()

{

int complicity;

if (getInputYN("Are you Mr. Burns?"))

complicity = BURNS_INDUSTRIAL_COEFFICIENT;

else if (getInputYN("Are you related to Mr. Burns?"))

complicity = BURNS_INDUSTRIAL_COEFFICIENT / 2;

else complicity = getIntInput("Enter your age: ",

MIN_AGE, MAX_AGE);

return(complicity);

}

/*

Prompt for and get input for # pigs eaten. Then compute

(and return) methane production using that value and the

specified value for wasted food.

*/

float methaneProduction(float wastedFood)

{

int numPigsEaten;

numPigsEaten =

getIntInput("Enter # pigs your father ate this week: ",

PIGS_EATEN_MIN, PIGS_EATEN_MAX);

return(wastedFood * numPigsEaten * numPigsEaten + 3 *

numPigsEaten + 5);

}

/*

Compute 'guilt' value based on the specified values for

wasted food, transit usage, complicity, and methane

production. Return the value computed.

*/

float computeGuiltValue(float wastedFood, int transitUsage,

int complicity, float methane)

{

float adjTransitUsage;

wastedFood = adjustValue(wastedFood, WASTED_FOOD_MULTIPLIER,

WASTED_FOOD_OFFSET);

adjTransitUsage = adjustValue(transitUsage,

TRANSIT_USAGE_MULTIPLIER,

TRANSIT_USAGE_OFFSET);

return(wastedFood + adjTransitUsage + complicity + methane);

}

/*

Return the specified value multiplied by a given

multiplier and with a given offset added to it.

*/

float adjustValue(float value, float multiplier, int offset)

{

return(value * multiplier + offset);

}

/*

Output the specified prompt and return the integer value

entered, which has been validated to be > the specified

MIN_VALUE and < the specified MAX_VALUE.

*/

int getIntInput(string prompt, int MIN_VALUE, int MAX_VALUE)

{

bool invalidEntry;

int value;

do

{

cout << prompt;

cin >> value;

invalidEntry = (value < MIN_VALUE) || (value > MAX_VALUE);

if (invalidEntry)

cout << "Invalid entry! Can't be < " << MIN_VALUE

<< " or > " << MAX_VALUE << "\n";

} while (invalidEntry);

return(value);

}

/*

Output the specified prompt and return the float value

entered, which has been validated to be > the specified

MIN_VALUE and < the specified MAX_VALUE.

*/

float getFloatInput(string prompt, int MIN_VALUE, int MAX_VALUE)

{

bool invalidEntry;

float value;

do

{

cout << prompt;

cin >> value;

invalidEntry = (value < MIN_VALUE) || (value > MAX_VALUE);

if (invalidEntry)

cout << "Invalid entry! Can't be < " << MIN_VALUE

<< " or > " << MAX_VALUE << "\n";

} while (invalidEntry);

return(value);

}

/*

Output the specified prompt and return true if the user

enters 'Y' or 'y'.

*/

bool getInputYN(string prompt)

{

char responseYN;

bool invalidEntry;

do

{

cout << prompt << " (y/n) ";

cin >> responseYN;

invalidEntry = (responseYN != 'y') && (responseYN != 'Y') &&

(responseYN != 'n') && (responseYN != 'N');

if (invalidEntry)

cout << "Invalid entry! "

<< "You need to enter 'y' or 'n'!\n";

} while (invalidEntry);

return((responseYN == 'y') || (responseYN == 'Y'));

}

/*

Output sign-off message (i.e., when user quits program).

*/

void outputSignOff()

{

cout << "\nThank you for participating in the survey!\n";

return;

}