/*
Programmer: Jennifer Leopold
Date: February 1, 2018
File: hw3.cpp
Purpose: Get user input for opening 3 locks to gain
access to cookies locked up in a jar.
To compile: fg++ hw3.cpp -o hw3
To execute: ./hw3
*/
#include <iostream>
#include <cmath>
using namespace std;
// Secret value for opening lock #1
const int LOCK1_SECRET_VALUE = 15;
// Maximum # attempts user can try to open lock #1
const int LOCK1_MAX_ATTEMPTS = 2;
// Secret values for opening lock #2
const float LOCK2_SECRET_DISTANCE_VALUE = 17.45;
const float LOCK2_SECRET_TOLERANCE_VALUE = 0.1;
// Secret values for opening lock #3
const float LOCK3_SECRET_MAGIC_RATIO = 3.4;
const int LOCK3_SECRET_PASSWORD_LENGTH = 1;
int main()
{
int passCode; // user inputs
float sliderAmt;
string password;
string userName;
char yesNo;
bool open1, open2, open3; // whether lock 1..3 is open
int numTries; // # attempts at opening a lock
// Greeting
cout << "Welcome to BoJack's Cookie Jar Security "
<< "System!\n\n";
// Get user's name
cout << "Enter your name (with no spaces): ";
cin >> userName;
// Let the user try to open the locks
do
{
// Initially, all locks are not open
open1 = false;
open2 = false;
open3 = false;
// Let user try to open 1st lock
numTries = 1;
do
{
cout << "\nEnter a 5-digit passcode for lock #1: ";
cin >> passCode;
// Check that the product of 10's digit and 1000's digit
// is a particular value
open1 = (((passCode / 10) % 10) *
((passCode / 1000) % 10)) == LOCK1_SECRET_VALUE;
if (!open1)
{
cout << "That is the incorrect code for lock #1!";
if (numTries < LOCK1_MAX_ATTEMPTS)
cout << " You get another try.\n";
else cout << "\nYou don't get to try this lock again"
<< " or go on to the next lock!\n";
}
numTries++;
} while ((!open1) && (numTries <= LOCK1_MAX_ATTEMPTS));
// If 1st lock opened, let user try to open next locks
if (open1)
{
cout << "\nEnter amount to move the lock #2 slider: ";
cin >> sliderAmt;
// Check that the amount entered is within a particular
// tolerance
open2 = fabs(LOCK2_SECRET_DISTANCE_VALUE - sliderAmt) <=
LOCK2_SECRET_TOLERANCE_VALUE;
if (!open2)
cout << "That does not open lock #2!\n"
<< "You don't get to try this lock again"
<< " or go on to the next lock!\n";
else
{
// Let user try to open 3rd lock
cout << "\nEnter a password (with no spaces) for "
<< "lock #3: ";
cin >> password;
// Check that the integer part of the lock #2 distance
// divided by the password length is a certain value,
// or that the length of the password is 1
open3 = ((static_cast<int>(LOCK2_SECRET_DISTANCE_VALUE)
/ static_cast<float>(password.length())) ==
LOCK3_SECRET_MAGIC_RATIO)
||
(password.length() ==
LOCK3_SECRET_PASSWORD_LENGTH);
if (!open3)
cout << "That does not open lock #3!"
<< " You don't get to try this lock again!\n";
else cout << "\nCongratulations, " << userName << "! "
<< "You can get into BoJack's cookies!\n";
}
}
// If user didn't open the cookie jar, see if they
// want to start over
if (!open3)
{
cout << "\nDo you want to start all over again? (y/n) ";
cin >> yesNo;
}
else yesNo = 'N';
} while ((yesNo == 'y') || (yesNo == 'Y'));
// Sign-off
cout << "\nGoodbye, " << userName << "!\n";
return 0;
}