Solution

/*

Programmer: Jennifer Leopold

Date: October 1, 2015

File: hw5.cpp

Purpose: Generate a 4-letter password where each letter is

calculated based on user's input of brain weight,

age, and whether s/he regularly eats glue.

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

To execute: ./hw5

*/

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

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

const short PASSWORD_LENGTH = 9; // # chars in password

const short HOMER_IQ = 26; // Homer's IQ

const short HOMER_DONUT_CONSUMPTION = 65; // Homer's daily

// donut consumption

const short AVG_GLUE_EATER_IQ = 32; // Avg IQ of people

// who regularly eat

// glue

const short K_FACTOR_INCREMENT = 100; // Amount to

// increment kFactor

// for each letter

// of password

const short MIN_AGE = 1; // Valid range for

const short MAX_AGE = 100; // user's age

const short MIN_BRAIN_WEIGHT = 0; // Valid range for

const short MAX_BRAIN_WEIGHT = 10; // user's brain weight

const short CHANCE_OF_DIGIT = 50; // % chance a digit is chosen

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

// Display a greeting message to the user.

// Preconditions: None

// Postconditions: A message has been output to the screen.

void outputGreeting();

// Prompt for and get input from the user, validating

// the input to make sure it is within the specified range.

// Preconditions: Parameter 'prompt' contains a string

// describing what information the user is to enter.

// Parameters 'minVal' and 'maxVal' constitute the valid

// range of values for user input.

// Postconditions: A value within the specified range will be

// returned.

short getShortInput(const string prompt,

const short minVal, const short maxVal);

// Calculate a single letter for a password.

// Preconditions: Parameters 'brainWeight', 'age', and

// kFactor should have positive values.

// Postconditions: An ASCII character will be returned.

char calculatePasswordLetter(const short brainWeight,

const short age,

const bool isGlueEater,

const int kFactor);

// Display a sign-off message to the user.

// Preconditions: None

// Postconditions: A message has been output to the screen.

void outputSignOff();

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

int main()

{

short age, // user inputs

brainWeight,

regularlyEatsGlue;

bool isGlueEater; // true if regularlyEatsGlue

// is 1; otherwise, 0

int kFactor; // product of password const's,

// incremented for each letter

// in password

char letter; // single char of password

// Seed the random # generator

//srand(time(NULL));

srand(5); // for grading

// Greeting

outputGreeting();

// Get user inputs

age = getShortInput("Enter your age: ", MIN_AGE, MAX_AGE);

brainWeight =

getShortInput("Enter the weight of your brain (lbs): ",

MIN_BRAIN_WEIGHT, MAX_BRAIN_WEIGHT);

regularlyEatsGlue =

getShortInput("Do you eat glue regularly? (1=yes, 0=no) ",

0, 1);

isGlueEater = regularlyEatsGlue == 1;

// Calculate and output each of the letters of user's

// password

cout << "Your password is ";

kFactor = HOMER_IQ * HOMER_DONUT_CONSUMPTION *

AVG_GLUE_EATER_IQ;

for (short i = 1; i <= PASSWORD_LENGTH; i++)

{

// %chance insert a digit into the password

if ((rand() % 100) < CHANCE_OF_DIGIT)

{

letter = static_cast<char>((rand() % 10) + 48);

}

else

{

letter = calculatePasswordLetter(brainWeight, age,

isGlueEater, kFactor);

kFactor += K_FACTOR_INCREMENT;

}

cout << letter;

}

// Sign-off

outputSignOff();

return 0;

}

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

void outputGreeting()

{

cout << "Welcome to the password generator!\n";

return;

}

short getShortInput(const string prompt,

const short minVal, const short maxVal)

{

short input;

bool invalidInput;

do

{

cout << prompt;

cin >> input;

invalidInput = (input < minVal) || (input > maxVal);

if (invalidInput)

cout << "Invalid: input must be between "

<< minVal << " and " << maxVal

<< " (inclusive)!\n\n";

} while (invalidInput);

return(input);

}

char calculatePasswordLetter(const short brainWeight,

const short age,

const bool isGlueEater,

const int kFactor)

{

short asciiValue;

char letter;

asciiValue = (static_cast<int>

(static_cast<float>(brainWeight) / age *

kFactor)

% HOMER_IQ) +

HOMER_DONUT_CONSUMPTION +

(isGlueEater * AVG_GLUE_EATER_IQ);

letter = static_cast<char>(asciiValue);

return(letter);

}

void outputSignOff()

{

cout << "\n\nHave a nice day "

<< "and don't forget that password!\n";

}