Solution

/*

Programmer: Jennifer Leopold

File: hw5.cpp

Purpose: Simulate office workers assisting people by

"talking" to them, giving them potato chips, and

giving them points that will determine whether

or not they get to talk to "the boss."

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

To execute: ./hw5

*/

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

// Points are accumulated on a "card."

// The following constants define max initial points, # points

// sufficient to go see secretary, # pts to impress the

// secretary, and # pts sufficient to go see the boss,

// respectively

const int MAX_INITIAL_POINTS = 5;

const int ENOUGH_POINTS = 30;

const int MEDIUM_POINTS = 40;

const int HIGH_POINTS = 60;

// There are multiple office workers named "Dylan"

const int NUM_DYLANS = 5;

// Each "Dylan" may give out a certain # of potato chips

const int DYLAN1_NUM_CHIPS = 5;

const int DYLAN2_NUM_CHIPS = 10;

const int DYLAN3_NUM_CHIPS = 3;

const int DYLAN4_NUM_CHIPS = 0;

const int DYLAN5_NUM_CHIPS = 4;

// Each "Dylan" office worker can give out certain # of points

const int DYLAN1_PTS = 7;

const int DYLAN2_PTS = 2;

const int DYLAN3_PTS = 8;

const int DYLAN4_PTS = -2;

const int DYLAN5_PTS_BELOW_THRESHOLD = 2;

const int DYLAN5_PTS_AT_OR_ABOVE_THRESHOLD = 5;

const int DYLAN5_PTS_THRESHOLD = 10;

// Each "Dylan" office worker says something

const string DYLAN1_MSG = "Hey. Are you ok? Here, have some "

"chips...they're free.";

const string DYLAN2_MSG = "Ignore him. Chips?! What kind?";

const string DYLAN3_MSG = "Hull LO.";

const string DYLAN4_MSG = "ZZzzzzzzz";

const string DYLAN5_MSG = "You won't do it; you're scared.";

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

void beginSimulation();

int rachel();

int dylan1(int &cardNum);

int dylan2(int &cardNum);

int dylan3(int &cardNum);

int dylan4(int &cardNum);

int dylan5(int &cardNum);

void elaina(const int cardNum);

void leave(const int points, const int chips);

int main( )

{

int cardNum; // pts accumulated on card

int whichDylan; // which office worker (named

// "Dylan") user will see

int numChipsGiven = 0; // chips accumulated during visit

// Seed random number generator

srand(12345); //srand(time(NULL));

// Output welcome message

beginSimulation();

// Init points on user's card by seeing secretary "Rachel"

cardNum = rachel();

// Continue giving user points and chips until they have

// enough points to see the secretary "Elaina"

while (cardNum <= ENOUGH_POINTS)

{

// Randomly pick an office worker (i.e., a "Dylan") to see

whichDylan = (rand() % NUM_DYLANS) + 1;

switch (whichDylan)

{

case 1 : numChipsGiven += dylan1(cardNum);

break;

case 2 : numChipsGiven += dylan2(cardNum);

break;

case 3 : numChipsGiven += dylan3(cardNum);

break;

case 4 : numChipsGiven += dylan4(cardNum);

break;

case 5 : numChipsGiven += dylan5(cardNum);

}

}

// Go see the secretary "Elaina"

elaina(cardNum);

// Output sign-off message

leave(cardNum, numChipsGiven);

return(0);

}

//***** Function definitions *****

// Output welcome message.

void beginSimulation()

{

cout << "\nWelcome to the office!\n\n";

return;

}

// Randomly choose card number (i.e., points), output

// message informing user of those points, and

// return card number.

int rachel()

{

int startingCardNum = rand() % MAX_INITIAL_POINTS;

cout << "Hi, I'm Rachel. Watch out for the Dylans.\n";

cout << "Here's your starting card number ("

<< startingCardNum << ")\n";

return(startingCardNum);

}

// Given current card number (i.e., points), increment

// points by a particular amount, output a greeting message

// and a message about giving the user some potato chips.

// Return updated card number (i.e., points) as pass-by-

// reference parameter and number of potato chips.

int dylan1(int &cardNum)

{

cardNum += DYLAN1_PTS;

cout << DYLAN1_MSG << endl;

cout << "Here are " << DYLAN1_NUM_CHIPS << " chips\n";

return(DYLAN1_NUM_CHIPS);

}

// Given current card number (i.e., points), multiply

// points by a particular amount, output a greeting message

// and a message about giving the user some potato chips.

// Return updated card number (i.e., points) as pass-by-

// reference parameter and number of potato chips.

int dylan2(int &cardNum)

{

cardNum *= DYLAN2_PTS;

cout << DYLAN2_MSG << endl;

cout << "Here are " << DYLAN2_NUM_CHIPS << " chips\n";

return(DYLAN2_NUM_CHIPS);

}

// Given current card number (i.e., points), increment

// points by a particular amount, output a greeting message

// and a message about giving the user some potato chips.

// Return updated card number (i.e., points) as pass-by-

// reference parameter and number of potato chips.

int dylan3(int &cardNum)

{

cardNum += DYLAN3_PTS;

cout << DYLAN3_MSG << endl;

cout << "Here are " << DYLAN3_NUM_CHIPS << " chips\n";

return(DYLAN3_NUM_CHIPS);

}

// Given current card number (i.e., points), increment

// points by a particular amount, output a greeting message.

// Return updated card number (i.e., points) as pass-by-

// reference parameter and number of potato chips.

int dylan4(int &cardNum)

{

cardNum += DYLAN4_PTS;

cout << DYLAN4_MSG << endl;

return(DYLAN4_NUM_CHIPS);

}

// Given current card number (i.e., points), increment

// points by a particular amount, output a greeting message

// and a message about giving the user some potato chips.

// Return updated card number (i.e., points) as pass-by-

// reference parameter and number of potato chips.

int dylan5(int &cardNum)

{

if (cardNum < DYLAN5_PTS_THRESHOLD)

cardNum *= DYLAN5_PTS_BELOW_THRESHOLD;

else cardNum += DYLAN5_PTS_AT_OR_ABOVE_THRESHOLD;

cout << DYLAN5_MSG << endl;

cout << "Here are " << DYLAN5_NUM_CHIPS << " chips\n";

return(DYLAN5_NUM_CHIPS);

}

// Given current card number (i.e., points), output a

// message commenting on the points and telling

// the user what to do next (e.g., leave the office).

void elaina(const int cardNum)

{

if (cardNum <= MEDIUM_POINTS)

cout << "Only " << cardNum << " points?\n"

<< "Please leave through that door.\n"

<< "Here's a dum-dum for your efforts.\n";

else if (cardNum <= HIGH_POINTS)

cout << "Wow, you have " << cardNum << " points.\n"

<< "You get a smarty.\n"

<< "Please leave through that door.\n";

else cout << "Double WOW, you have " << cardNum

<< " points.\nYou get 2 smarties.\n"

<< "You get to talk with the boss.\n";

return;

}

// Output sign-off message that tells how many points

// and potato chips user accumulated.

void leave(const int points, const int chips)

{

cout << "\nI have " << points << " points, and "

<< chips << " chips! Goodbye!\n";

return;

}