+1 (315) 557-6473 

Create a Program to Create Locker Safe in C++ Assignment Solution.


Instructions

Objective
Write a program to create locker safe in C++.

Requirements and Specifications

CIS-1275
C++ Programming Part I
Program 5: Combo Generator
Turn in Requirements:
  1. 5 pts. Name your project LastnameP6, such as EguchiP6.
  2. 10 pts. Remove the .sdf and debug files and then upload your zipped project through Brightspace
Program Requirements:
  1. 5 pts. Write your name, email address and file name at the top of your source code in a comment.
  2. 5 pts. Use cout statements to write your name, program title, and program objective to the screen so that it is the first thing I see when your program runs. This is your course header.
  3. 5 pts. Add comments as appropriate. Be sure that your program output is neatly presented to the user.
  4. 70 pts. Requirements as described below.
  5. BONUS 20 pts. Implement and call WriteCombo as described below.
Description
Don’s Lock and Safe Company needs a program to help with C++ assignment and to assist his customers in creating 4-number safe combinations. Don has a pencil-and-paper system and he’d like to automate the process.
Don’s system is simple. He uses a classic telephone keypad to translate characters into a single digit. Here is an image of a keypad.
Don asks his customers to give him an 8-character phrase containing no spaces, numbers, or special characters. The corresponding numbers on the phone become the digits of the generated combination.
For example, if I gave him the string “Johnston”, it would translate into this safe combination:
J ➔ 5
o 6
h 4
n 6
s 7
t 8
o 6
n 6

56467866
The safe combination for the “Johnston” code is 56-46-78-66.
There are certain restrictions on the lock combinations, due to the mechanics, lock design and dialing tolerances. Since the Underwriter’s Laboratory specify locks have a 1.25+ digit tolerance we are going to require that the combinations not have values within 4 digits of one another. That means, if a combination has two numbers within (and including) 4 numbers apart, it will be considered invalid.
For example, the combination 55-59-24-88 is invalid because 55 and 59 are within 4 digits. The combination 55-24-88-59 is also invalid.
Additionally, combinations cannot have the same number twice. The combination 58-25-94-25 is invalid because 25 appears twice in this combination.
In your implementation, you must have functions that match the following table:
Required Function Prototype (Declaration)Specification
string askForPhrase();Asks the user to enter an 8 character phrase with no digits, spaces, or special chars.
NOTE: no error checking is done in this function.
bool validatePhrase (string input, string* desc);

Passed the user’s input and performs error checking on input.
Returns a boolean value (true/false) that is true if the input is valid, or false if it is not valid.
The desc variable describes the problem with the phrase. The string pointed to by desc should be assigned to a string that tells the user what went wrong, such as “contains a space”, “contains a digit”, etc.
If the string is valid, the desc should be assigned to a string that indicates the input is valid, such as “valid”.
Required Function Prototype (Declaration)
Specification
void createCombo(
string input,
struct ComboNumbers& rCombo);
Passed a valid input string and returns the four numbers that represent the combination. The implementation should call twoCharsToInt.
bool validateCombo(
struct ComboNumbers combo,
string *desc);
Validates the combination. Returns a true/false value indicating whether the combination is valid or not.
The desc argument describes the problem with the combo, and the string it points to should be assigned before exiting the function. Example values include “duplicate numbers”, or “numbers too close together”.
BONUS:
bool WriteCombo(
struct ComboNumbers combo,
string filename);
BONUS:
Open, check, and, if successful, write a file with the validated combination.
NOTE: For full credit, your code must include the functions described.
Your information to the user should be neatly formatted in complete, brief sentences.
In your Functions.h, declare a struct with the four combo numbers. Name the struct ComboNumbers.
Your main method should:
  1. Show your header (write a separate function if you wish).
  2. Create a struct ComboNumbers instance (a variable of type struct ComboNumbers). Name it combos.
  3. Begin a do while loop.
  4. The user should be asked for their phrase.
  5. If the phrase is invalid, ask if they would like to enter another phrase or quit. If they choose to enter another phrase, go back to the beginning of the do-while loop. If they choose to quit, drop out of the do-while loop and print a goodbye statement.
  6. Create the combinartion based on the phrase
  7. Report the resultant combination and whether it is valid and usable as a safe combination.
  8. If the combination is invalid, ask if they would like to enter another phrase or quit. If they choose to enter another phrase, go back to the beginning of the do-while loop. If they choose to quit, drop out of the do-while loop and print a goodbye statement.
  9. (BONUS) Call WriteCombo, so that the user has a written record of their combination.
  10. (BONUS) Check the bool return from the WriteCombo function and report the success/failure of the file writing.
  11. When the user is finished, the code logic should drop out of the do-while loop and print a goodbye statement.
Except for your header() and askForPhrase () function ALL output is displayed to the user from main. No other functions write any information to the screen!
Source Code
#include "Functions.h"
#include
#include
#include
#include
#include
using namespace std;
// Display something about the program
void header()
{
cout << "Don's Lock and Safe Company" << endl;
cout << endl;
}
// Asks the user to enter an 8 character phrase with no digits, spaces,
// or special chars
string askForPhrase()
{
cout << "Enter an 8 character phrase with no digits, spaces, or special chars: ";
string phrase;
getline(cin, phrase);
return phrase;
}
// Perform error checking of input. Returns a boolean value if the input
// is valid ir not.
bool validatePhrase(string input, string *desc)
{
// Check everything are all characters
for (int i = 0; i < (int) input.size(); i++)
{
if (!isalpha(input[i]))
{
*desc = "The input contained a non-alphabet character.";
return false;
}
}
// Check the size
if (input.size() != 8)
{
*desc = "The input is not 8 characters.";
return false;
}
*desc = "The phrase is valid.";
return true;
}
// Get the integer representation of the 2 characters then
// concantenate them as 1
int twoCharsToInt(char char1, int char2)
{
char1 = toupper(char1);
char2 = toupper(char2);
int value1 = 0;
// Convert first char to digit
if (char1 >= 'A' && char1 <= 'C')
value1 = 2;
else if (char1 >= 'D' && char1 <= 'F')
value1 = 3;
else if (char1 >= 'G' && char1 <= 'I')
value1 = 4;
else if (char1 >= 'J' && char1 <= 'L')
value1 = 5;
else if (char1 >= 'M' && char1 <= 'O')
value1 = 6;
else if (char1 >= 'P' && char1 <= 'S')
value1 = 7;
else if (char1 >= 'T' && char1 <= 'V')
value1 = 8;
else if (char1 >= 'W' && char1 <= 'Z')
value1 = 9;
// Convert second char to digit
int value2 = 0;
if (char2 >= 'A' && char2 <= 'C')
value2 = 2;
else if (char2 >= 'D' && char2 <= 'F')
value2 = 3;
else if (char2 >= 'G' && char2 <= 'I')
value2 = 4;
else if (char2 >= 'J' && char2 <= 'L')
value2 = 5;
else if (char2 >= 'M' && char2 <= 'O')
value2 = 6;
else if (char2 >= 'P' && char2 <= 'S')
value2 = 7;
else if (char2 >= 'T' && char2 <= 'V')
value2 = 8;
else if (char2 >= 'W' && char2 <= 'Z')
value2 = 9;
// Combine the two digits and return it
return value1 * 10 + value2;
}
// Passed a valid input string and returns the four numbers that
// represent the combination
void createCombo(string input, struct ComboNumbers &rCombo)
{
rCombo.number1 = twoCharsToInt(input[0], input[1]);
rCombo.number2 = twoCharsToInt(input[2], input[3]);
rCombo.number3 = twoCharsToInt(input[4], input[5]);
rCombo.number4 = twoCharsToInt(input[6], input[7]);
}
// Validates the combination. Returns a boolean value if the combo
// is valid or not
bool validateCombo(struct ComboNumbers combo, string *desc)
{
int numbers[4] =
{
combo.number1,
combo.number2,
combo.number3,
combo.number4
};
// Validate that each numbers int the combo are 4 digits apart
// and that there are no duplicates
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 4; j++)
{
if (numbers[i] == numbers[j])
{
*desc = "Invalid combo. Duplicate numbers found.";
return false;
}
if (abs(numbers[i] - numbers[j]) <= 4)
{
*desc = "Invalid combo. Two of the numbers are found to close to each other (within 4 digit range).";
return false;
}
}
}
*desc = "The combo is valid.";
return true;
}
// Write a file with the validated combination
bool WriteCombo(struct ComboNumbers combo, string filename)
{
ofstream outFile(filename.c_str());
if (!outFile.is_open())
return false;
outFile << combo.number1 << " " << combo.number2 << " " << combo.number3 << " " << combo.number4 << endl;
outFile.close();
return true;
}