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

C++ Language: Write a program that will manage a simple address book. The progra

ID: 3801171 • Letter: C

Question

C++ Language: Write a program that will manage a simple address book. The program should allow you to view and search existing entires as well as add new entries. The data should be stored in a file structured so that a person's first name, last name, and phone number are stored on one line separated by commas and the next line has their address. Additional requirements: The program should allow the user to make repeated requests until they choose to exit The program should find multiple matches if necessary The program should have options to print out both a full list and a list that just includes Name and Phone Number The program should use at least two struct types and one enumerated type

Explanation / Answer

#include <iostream>
#include <fstream>

using namespace std;

void openFile(string, ifstream&);
bool readFile(ifstream&, string&, string&, string&, string&);
void printInfo(string, string, string, string);
void searchFirst(ifstream&);
void searchLast(ifstream&);
void searchAddress(ifstream&);
void searchPhone(ifstream&);
string uppercaseString(string);
void extraSpaces(string&);


int main()
{
string firstName, lastName, address, phone, filename;
ifstream inData;
char searchMethod;

openFile(filename, inData);

do {
cout << "What do you want to search by?" << endl
<< "(F)irst name, (L)ast name, (A)ddress, (P)hone, or e(X)it?: ";

cin >> searchMethod;
searchMethod = toupper(searchMethod);

switch (searchMethod)
{
case 'F':
searchFirst(inData);
break;
case 'L':
searchLast(inData);
break;
case 'A':
searchAddress(inData);
break;
case 'P':
searchPhone(inData);
break;
case 'X':
break;
default:
cout << "Invalid input!" << endl;
break;
}

} while (searchMethod != 'X');

return 0;
}


void openFile(string filename, ifstream& inData)
{
do {
cout << "Enter the name of the file you want to open: ";
cin >> filename;

inData.open(filename.c_str());

if (!inData)
cout << "File not found try again!" << endl;

} while (!inData);
}


void searchFirst(ifstream& inData)
{
string searchName, firstName, lastName, phone, address,
upperSearch, upperFirstName;

cout << "Enter the first name of the person: ";
cin >> searchName;
cout << endl;

upperSearch = uppercaseString(searchName);

while (readFile(inData, firstName, lastName, phone, address))
{
upperFirstName = uppercaseString(firstName);

if (upperFirstName == upperSearch)
break;
}

if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchName << " not found!" << endl << endl;
}

inData.clear();
inData.seekg(0);
}


void searchLast(ifstream& inData)
{
string searchName, firstName, lastName, phone, address,
upperSearch, upperLastName;

cout << "Enter the last name of the person: ";
cin >> searchName;
cout << endl;

upperSearch = uppercaseString(searchName);

while (readFile(inData, firstName, lastName, phone, address))
{
upperLastName = uppercaseString(lastName);

if (upperLastName == upperSearch)
break;
}

if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchName << " not found!" << endl << endl;
}

inData.clear();
inData.seekg(0);
}


void searchAddress(ifstream& inData)
{
string searchAdd, firstName, lastName, phone, address,
upperSearch, upperAdd;

cout << "Enter the address of the person: ";
cin >> searchAdd;
cout << endl;

upperSearch = uppercaseString(searchAdd);

while (readFile(inData, firstName, lastName, phone, address))
{
upperAdd = uppercaseString(address);

if (upperAdd == upperSearch)
break;
}

if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchAdd << " not found!" << endl << endl;
}

inData.clear();
inData.seekg(0);
}


void searchPhone(ifstream& inData)
{
string searchNum, firstName, lastName, phone, address,
upperSearch, upperNum;

cout << "Enter the phone number of the person: ";
cin >> searchNum;
cout << endl;

upperSearch = uppercaseString(searchNum);

while (readFile(inData, firstName, lastName, phone, address))
{
upperNum = uppercaseString(phone);

if (upperNum == upperSearch)
break;
}

if (inData)
{
cout << "Person found: " << endl;
printInfo(firstName, lastName, phone, address);
}
else
{
cout << searchNum << " not found!" << endl << endl;
}

inData.clear();
inData.seekg(0);
}


bool readFile(ifstream& inData, string& firstName, string& lastName, string& phone, string& address)
{
getline(inData, firstName, ',');
extraSpaces(firstName);

getline(inData, lastName, ',');
extraSpaces(lastName);

getline(inData, phone, ' ');
extraSpaces(phone);

getline(inData, address, ' ');
extraSpaces(address);

return inData;
}


void printInfo(string firstName, string lastName, string phone, string address)
{
cout << "Name: " << firstName << " " << lastName << endl;
cout << "Phone: " << phone << endl;
cout << "Address: " << address << endl << endl;
}


string uppercaseString(string str)
{
string nString = str;
int i;

for (i = 0; i < str.size(); i++)
{
nString[i] = toupper(str[i]);
}

return nString;
}


void extraSpaces(string& str)
{
int index;

index = str.find_first_not_of(" ");
str.erase(0, index);

index = str.find_last_not_of(" ");
if (string::npos != index)
str.erase(index + 1);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote