Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ programming. Create a Phone Book program that contains first name, last name

ID: 656078 • Letter: C

Question

C++ programming.

Create a Phone Book program that contains first name, last name and phone number. It must be menu driven and allow the user to:

1. Create entries

2. Read a list of entries (display a list of entries) that are selectable from a range or in its entirety.

Example: names beginning with H through K

The list of entries displayed must be sorted.

3. Update/modify an entry from the menu and choose a selected attribute or all attributes.

Example: Change first name, or change first name, last name and phone number.

4. Delete a single entry, a range of entries, or delete all entries.

5. Save all entries to a file after each update and before exiting the program.

6. The list should be sorted each time a change is made so display is accurate.

Note: Use of the Standard Template Library and Struct is STRONGLY encouraged.

Explanation / Answer

Complete Program:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

class Contact
{
public:
Contact();
void setFirstName(string first);
void setLastName(string last);
void setPhoneNumber(string phone);
string getFirstName();
string getLastName();
string getPhoneNumber();
void printContact();

private:
string fName;
string lName;
string number;
};

Contact::Contact()
{
fName = "";
lName = "";
number = "";
}

void Contact::setFirstName(string first)
{
fName = first;
}

void Contact::setLastName(string last)
{
lName = last;
}

void Contact::setPhoneNumber(string phone)
{
number = phone;
}

void Contact::printContact()
{
cout << fName << " " << lName << "   " << number << endl;
}

string Contact::getFirstName()
{
return fName;
}

string Contact::getLastName()
{
return lName;
}

string Contact::getPhoneNumber()
{
return number;
}


class PhoneBook
{
public:
PhoneBook();
void addContact(Contact cont);
void deleteContact(Contact cont);
void updateContact(Contact cont1, Contact cont2);
void saveContactsToFile(string filename);
void printAllContacts();
void sortContacts();
bool isFind(Contact cont);

private:
vector<Contact> contacts;
};

PhoneBook::PhoneBook()
{}

void PhoneBook::addContact(Contact cont)
{
if(!isFind(cont))
  contacts.push_back(cont);
else
  cout << "This contact is already entered!" << endl;
}

void PhoneBook::deleteContact(Contact cont)
{
if(isFind(cont))
{
  vector<Contact> temp;

  for(vector<Contact>::iterator itr = contacts.begin(); itr != contacts.end(); itr++)
  {
   Contact current = *itr;

   if(strcmp(current.getFirstName().c_str(), cont.getFirstName().c_str()) != 0
    || strcmp(current.getLastName().c_str(), cont.getLastName().c_str()) != 0
    || strcmp(current.getPhoneNumber().c_str(), cont.getPhoneNumber().c_str()) != 0)
   {
    temp.push_back(current);
   }
  }

  contacts = temp;
}
else
  cout << "This contact is not found in the phone book!" << endl;
}

void PhoneBook::updateContact(Contact cont1, Contact cont2)
{
if(isFind(cont1))
{
  for(vector<Contact>::iterator itr = contacts.begin(); itr != contacts.end(); itr++)
  {
   Contact current = *itr;

   if(strcmp(current.getFirstName().c_str(), cont1.getFirstName().c_str()) == 0
   && strcmp(current.getLastName().c_str(), cont1.getLastName().c_str()) == 0
   && strcmp(current.getPhoneNumber().c_str(), cont1.getPhoneNumber().c_str()) == 0)
   {
    (*itr).setFirstName(cont2.getFirstName());
    (*itr).setLastName(cont2.getLastName());
    (*itr).setPhoneNumber(cont2.getPhoneNumber());
    return;
   }
  }
}
else
  cout << "This old contact is not found in the phone book!" << endl;
}

void PhoneBook::saveContactsToFile(string filename)
{
ofstream outfile;
outfile.open(filename);

for(vector<Contact>::iterator itr = contacts.begin(); itr != contacts.end(); itr++)
{
  outfile << (*itr).getFirstName() << " " << (*itr).getLastName() << "   " << (*itr).getPhoneNumber() << endl;
}

outfile.close();
}

void PhoneBook::printAllContacts()
{
for(vector<Contact>::iterator itr = contacts.begin(); itr != contacts.end(); itr++)
  (*itr).printContact();
}

void PhoneBook::sortContacts()
{
for(int i = 0; i < contacts.size() - 1; i++)
{
  int minPos = i;

  for(int j = i + 1; j < contacts.size(); j++)
  {
   string str1 = contacts[j].getFirstName() + " " + contacts[j].getLastName() + " " + contacts[j].getPhoneNumber();
   string str2 = contacts[minPos].getFirstName() + " " + contacts[minPos].getLastName() + " " + contacts[minPos].getPhoneNumber();

   if(strcmp(str1.c_str(), str2.c_str()) < 0)
   {
    minPos = j;
   }
  }

  if(minPos != i)
  {
   Contact temp = contacts[minPos];
   contacts[minPos] = contacts[i];
   contacts[i] = temp;
  }
}
}

bool PhoneBook::isFind(Contact cont)
{
for(vector<Contact>::iterator itr = contacts.begin(); itr != contacts.end(); itr++)
{
  Contact current = *itr;

  if(strcmp(current.getFirstName().c_str(), cont.getFirstName().c_str()) == 0
   && strcmp(current.getLastName().c_str(), cont.getLastName().c_str()) == 0
   && strcmp(current.getPhoneNumber().c_str(), cont.getPhoneNumber().c_str()) == 0)
  {
   return true;
  }
}

return false;
}


int main()
{
PhoneBook book;
Contact cont, cont2;
int choice, option;
string first, last, pnum;

do
{
  cout << " ---MENU---" << endl;
  cout << "1. Create" << endl;
  cout << "2. Display" << endl;
  cout << "3. Update" << endl;
  cout << "4. Delete" << endl;
  cout << "5. Save" << endl;
  cout << "6. Sort" << endl;
  cout << "0. Exit" << endl;
  cout << "Enter your choice: ";
  cin >> choice;

  switch(choice)
  {
  case 1:
   cout << " Enter the first name: ";
   cin >> first;
   cout << "Enter the last name: ";
   cin >> last;
   cout << "Enter the phone number: ";
   cin >> pnum;

   cont.setFirstName(first);
   cont.setLastName(last);
   cont.setPhoneNumber(pnum);

   book.addContact(cont);
   break;

  case 2:
   book.printAllContacts();
   break;

  case 3:
   cout << " Enter the old first name: ";
   cin >> first;
   cout << "Enter the old last name: ";
   cin >> last;
   cout << "Enter the old phone number: ";
   cin >> pnum;

   cont.setFirstName(first);
   cont.setLastName(last);
   cont.setPhoneNumber(pnum);

   cout << " Enter the new first name: ";
   cin >> first;
   cout << "Enter the new last name: ";
   cin >> last;
   cout << "Enter the new phone number: ";
   cin >> pnum;

   cont2.setFirstName(first);
   cont2.setLastName(last);
   cont2.setPhoneNumber(pnum);

   book.updateContact(cont, cont2);   
   break;

  case 4:
   cout << " Enter the first name: ";
   cin >> first;
   cout << "Enter the last name: ";
   cin >> last;
   cout << "Enter the phone number: ";
   cin >> pnum;

   cont.setFirstName(first);
   cont.setLastName(last);
   cont.setPhoneNumber(pnum);

   book.deleteContact(cont);
   break;

  case 5:
   book.saveContactsToFile("contacts.txt");
   break;

  case 6:
   book.sortContacts();
   break;

  case 0:
   cout << "Thank you!" << endl;
   break;

  default:
   cout << "Invalid Choice!" << endl;
  }
}while(choice != 0);

system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote