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

Write a C++ program to keep track of business contacts. Specification Create a B

ID: 3913574 • Letter: W

Question

Write a C++ program to keep track of business contacts.

Specification

Create a BusinessContact class with the following fields:

firstName:String

lastName:String

phoneNumber:String

emailAddress:String

company:String

Store the records in memory in an array (not a vector).

Store the records on disk in the text file contacts.txt.

Create a menu that allows for the following operations:

Add a Contact

Delete a Contact

View a Contact

Display the Contact List

Automatically read the text file and store the records in an array when the program begins.

Automatically write the records to the file when the program ends.

Explanation / Answer

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

//Class definition

class BusinessContact {

                //private data members

                string firstName;

                string lastName;

                string phoneNumber;

                string emailAddress;

                string company;

public:

                //member functions prototype

                //getter functions

                string getFirstName();

                string getLastName();

                string getPhoneNumber();

                string getEmailAddress();

                string getCompany();

               

                //setter functions

                void setFirstName(string fnm);

                void setLastName(string lnm);

                void setPhoneNumber(string phone);

                void setEmailAddress(string email);

                void setCompany(string c);

                //Overloaded friend functions

                friend ostream &operator<<( ostream &output, const BusinessContact &tbc );

                friend istream &operator>>( istream &input, BusinessContact &tbc );

};

// Definition of BusinessContact class memeber functions

string BusinessContact::getFirstName() {

                return(firstName);

}

string BusinessContact::getLastName() {

                return(lastName);

}

string BusinessContact::getPhoneNumber() {

                return(phoneNumber);

}

string BusinessContact::getEmailAddress() {

                return(emailAddress);

}

string BusinessContact::getCompany() {

                return(company);

}

void BusinessContact::setFirstName(string fnm) {

                firstName = fnm;

}

void BusinessContact::setLastName(string lnm) {

                lastName = lnm;

}

void BusinessContact::setPhoneNumber(string phone) {

                phoneNumber = phone;

}

void BusinessContact::setEmailAddress(string email) {

                emailAddress = email;

}

void BusinessContact::setCompany(string c) {

                company = c;

}

// Overloaded inseration operator function

//Will be used writing data into txt file

ostream &operator<<( ostream &output, const BusinessContact &tbc ) {

                output << tbc.firstName<<" "<<tbc.lastName<<" "<<tbc.phoneNumber<<" "<<tbc.emailAddress<<" "<<tbc.company<<endl;

                return output;  

}

// Overloaded extraction operator function

//Will be used raeding data from txt file

istream &operator>>( istream &input, BusinessContact &tbc ) {

                input >>tbc.firstName>>tbc.lastName>>tbc.phoneNumber>>tbc.emailAddress>>tbc.company;

                return input;     

}

int main() {

                BusinessContact bc[100]; // array to hold the contact details

                fstream file; // File stream

                string fnm, lnm, phone, email, c;

                int i, j, choice, count = 0; // count variable will keep track of number of contact

               

                file.open("contacts.txt", ios::in); //Open file for reading

                if(!file)

                              cout<<"File does not exist... New file will be created"<<endl;

                //Reading content of file till end of file

                if(file) {

                                while(!file.eof()) {

                                file>>bc[count]; // Reading contacts from txt file

                                count++;

                }

                }

                file.close(); //Close file

                do {

                                //displaying the Menu on screen

                                cout<<"1. Add a Contact"<<endl;

                                cout<<"2. Delete a Contact"<<endl;

                                cout<<"3. View a Contact"<<endl;

                                cout<<"4. Display the Contact List"<<endl;

                                cout<<"5. Exit"<<endl;

                                cout<<"Enter your choice:";

                                cin>>choice; //Input choice

                                switch(choice) {

                                                case 1 : // To add contact in array

                                                                                cout<<"Enter the First Name : ";

                                                                                cin>>fnm; // taking input from user

                                                                                bc[count].setFirstName(fnm); // store it into array

                                                                               

                                                                                cout<<"Enter the Last Name : ";

                                                                                cin>>lnm;

                                                                                bc[count].setLastName(lnm);

                                                                               

                                                                                cout<<"Enter the Phone Number : ";

                                                                                cin>>phone;

                                                                                bc[count].setPhoneNumber(phone);

                                                                               

                                                                                cout<<"Enter the E-mail Address : ";

                                                                                cin>>email;

                                                                                bc[count].setEmailAddress(email);

                                                                               

                                                                                cout<<"Enter the Company Name : ";

                                                                                cin>>c;

                                                                                bc[count].setCompany(c);

                                                                               

                                                                                count++;

                                                                               

                                                                                break;

                                                case 2 : //To delete contact from array

                                                                                cout<<"Enter the phone-number of contact to be deleted : ";

                                                                                cin>>phone;

                                                                                for (i=0; i<count; i++)

                                                                                                if (bc[i].getPhoneNumber() == phone)

                                                                                                                break;

                                                                                                // If x found in array

                                                                                                if (i < count) {

                                                                                                // reduce size of array and move all

                                                                                                // elements on space ahead

                                                                                                count = count - 1;

                                                                                                for (int j=i; j<count; j++)

                                                                                                                bc[j] = bc[j+1];

                                                                                                }

                                                                                                else

                                                                                                                cout<<"Phone number not found in list of contacts"<<endl;

                                                                                break;

                                                case 3 : // To view a particular contact

                                                                                cout<<"Enter the phone-number of contact to be viewed : ";

                                                                                cin>>phone;

                                                                                for (i=0; i<count; i++)

                                                                                                if (bc[i].getPhoneNumber() == phone) {

                                                                                                                cout<<left<<setw(15)<<"First Name"<<left<<setw(15)<<"Last Name"<<left<<setw(15)<<"Phone-No"<<left<<setw(15)<<"E-mail Address"<<left<<setw(15)<<"Company"<<endl;

                                                                                                                cout<<left<<setw(15)<<bc[i].getFirstName()<<left<<setw(15)<<bc[i].getLastName()<<left<<setw(15)<<bc[i].getPhoneNumber()<<left<<setw(15)<<bc[i].getEmailAddress()<<left<<setw(15)<<bc[i].getCompany()<<endl;

                                                                                                                break;

                                                                                                }

                                                                                                if (i == count)

                                                                                                                cout<<"Phone number not found in list of contacts"<<endl;

                                                                                                break;

                                                case 4 : // To display the all contacts of array

                                                                                cout<<left<<setw(15)<<"First Name"<<left<<setw(15)<<"Last Name"<<left<<setw(15)<<"Phone-No"<<left<<setw(15)<<"E-mail Address"<<left<<setw(15)<<"Company"<<endl;

                                                                                for (i=0; i<count; i++)

                                                                                cout<<left<<setw(15)<<bc[i].getFirstName()<<left<<setw(15)<<bc[i].getLastName()<<left<<setw(15)<<bc[i].getPhoneNumber()<<left<<setw(15)<<bc[i].getEmailAddress()<<left<<setw(15)<<bc[i].getCompany()<<endl;

                                                                                break;

                                                                                }

                }while(choice!=5); // loop will be executed till the option 5 is selected

                //Write content of array into txt file

                file.open("contacts.txt", ios::out); //Open file for writing

                if(!file) {

                                cout<<"File cannot be opened for reading"<<endl;

                                return 0;

                }

                for (i=0; i<count; i++)

                                file<<bc[i]; //Writing data into txt file

                file.close(); // close file

                return 1;

}

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