Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The C++ code below is a phone contacts program with 3 options: 1)Display all contacts 2)Add new contacts 3)Exit Edit the code so the program

The C++ code below is a phone contacts program with 3 options: 1)Display all contacts 2)Add new contacts 3)Exit

Edit the code so the program can also display the contacts in alphabetical order by last name, allow the user to search for a specific contact by last name, and delete a specific contact.

The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact given the contacts first or last name. 4. Delete a contact. 5. Exit program.

To represent a contact in the program, a struct named Contact with the following fields has been created:

firstName - string lastName - string phoneNumber - long long phoneType enum PhoneType

The PhoneType can be only one of the following: CELL, HOME, WORK.

When the program first loads, it reads all the saved contacts (if any) from a file named database.txt into a contacts array of size 50. DO NOT use vectors for this program. const int MAX_CONTACTS = 50; Contact contacts[MAX_CONTACTS];

Once the contacts are read from the database.txt file and loaded into the contacts array, the contacts array should be sorted by last name using either bubble sort or insertion sort.

While the program is running, the user can choose any of the 5 available options. During the program execution, if the user chooses to add or delete contacts, the contacts array should be updated and re-sorted. Any searching of the contacts array should be performed using binary search. The database.txt file will be updated automatically only when the program is about to exit. In other words, when the user selects option 5, the program first writes the contents of the Array into the database.txt file, and then exits

The format of the contents of the database.txt file should be in human readable plain text, one record per line (note the ~ separator between different entries) For example (these are the first three lines should be on database.txt):

Shawn~Seals~5124567890~0 Ted~Sheckler~2104567890~2 Chip~Chipperson~9404567890~1

For display, contacts should be printed to the console in the following format.

Chip Chipperson, Home (940)456-7890 Shawn Seals, Cell (512)456-7890 Ted Sheckler, Work (210)456-7890

// start of code

#include #include #include #include #include

using namespace std;

enum PhoneType { CELL, HOME, WORK };

struct Contact { string firstName; string lastName; long long phoneNumber; PhoneType phoneType; };

void displayContacts(Contact[], int, bool); void addContact(Contact[], int); void loadDatabase(Contact[], int); void saveToDatabase(Contact[], int);

string phoneNumberToString(long); int indexOfLastContact(Contact[], int);

static const string DATABASE_TXT_FILE = "database.txt"; static const char DELIMITER = '~';

int main() {

const int MAX_CONTACTS = 50;

Contact contacts[MAX_CONTACTS];

loadDatabase(contacts, MAX_CONTACTS);

int mainMenuChoice = 0;

while (mainMenuChoice != 3) {

cout << " Select an operation... " << "1. Display all contacts. " << "2. Add new contact. " << "3. Exit. ";

cin >> mainMenuChoice;

switch (mainMenuChoice) {

case 1: displayContacts(contacts, MAX_CONTACTS, false); break;

case 2: addContact(contacts, MAX_CONTACTS); break;

case 3: saveToDatabase(contacts, MAX_CONTACTS); break; } }

return 0; }

void displayContacts(Contact contacts[], int arraySize, bool isNumbered) {

int indexOfLast = indexOfLastContact(contacts, arraySize);

for (int i = 0; i <= indexOfLast; i++) {

Contact contact = contacts[i];

string phoneTypeString;

switch (contact.phoneType) { case CELL: phoneTypeString = "Cell"; break; case HOME: phoneTypeString = "Home"; break; case WORK: phoneTypeString = "Work"; break; }

string listNumber; if (isNumbered) { listNumber = to_string(i + 1) + ". "; } else { listNumber = ""; }

cout << endl << listNumber << contact.firstName << " " << contact.lastName << ", " << phoneTypeString << " " << phoneNumberToString(contact.phoneNumber); }

cout << endl; }

void addContact(Contact contacts[], int arraySize) {

Contact contact;

int indexOfLast = indexOfLastContact(contacts, arraySize);

cout << " First Name: "; cin >> contact.firstName;

cout << " Last Name: "; cin >> contact.lastName;

bool isValidPhoneNumber = false;

while (!isValidPhoneNumber) {

long phoneNumber; cout << " 10 Digit Phone Number (digits only): "; cin >> phoneNumber;

if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); continue; }

if (phoneNumber > 9999999999 || phoneNumber < 1000000000) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); continue; }

contact.phoneNumber = phoneNumber; isValidPhoneNumber = true; }

int phoneTypeInt = 0;

while (phoneTypeInt < 1 || phoneTypeInt > 3) {

cout << " Select phone type... " << "1. Cell " << "2. Home " << "3. Work ";

cin >> phoneTypeInt;

if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); }

switch (phoneTypeInt) { case 1: contact.phoneType = CELL; break; case 2: contact.phoneType = HOME; break; case 3: contact.phoneType = WORK; break; } }

contacts[indexOfLast + 1] = contact;

cout << " Contact added. "; }

void loadDatabase(Contact contacts[], int arraySize) {

ifstream fin; fin.open(DATABASE_TXT_FILE);

string firstName; string lastName; string phoneNumberString; string phoneTypeString;

int index = 0;

while (getline(fin, firstName, DELIMITER) && index < arraySize) {

getline(fin, lastName, DELIMITER); getline(fin, phoneNumberString, DELIMITER); getline(fin, phoneTypeString);

Contact contact;

contact.firstName = firstName; contact.lastName = lastName; contact.phoneNumber = stol(phoneNumberString);

int phoneTypeInt = stoi(phoneTypeString);

switch (phoneTypeInt) { case 0: contact.phoneType = CELL; break; case 1: contact.phoneType = HOME; break; case 2: contact.phoneType = WORK; break; }

contacts[index] = contact; index++; }

fin.close(); }

void saveToDatabase(Contact contacts[], int arraySize) {

ofstream fout; fout.open(DATABASE_TXT_FILE);

int indexOfLast = indexOfLastContact(contacts, arraySize);

for (int i = 0; i <= indexOfLast; i++) {

Contact contact = contacts[i];

fout << contact.firstName << "~" << contact.lastName << "~" << contact.phoneNumber << "~" << contact.phoneType << endl; }

fout.close(); }

string phoneNumberToString(long phoneNumberLong) {

string phoneNumberString = to_string(phoneNumberLong);

string areaCode = phoneNumberString.substr(0, 3); string prefix = phoneNumberString.substr(3, 3); string suffix = phoneNumberString.substr(6, 4);

return "(" + areaCode + ")" + prefix + "-" + suffix; }

int indexOfLastContact(Contact contacts[], int arraySize){

for (int i = 0; i < arraySize; i++) { Contact contact = contacts[i]; if (contact.firstName.compare("") == 0) { return i - 1; } } } //end of code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions