Given the three following files: AddressBook.cpp #include #include #include \"Ad
ID: 3684771 • Letter: G
Question
Given the three following files:
AddressBook.cpp
#include
#include
#include "AddressBook.h"
using namespace std;
void OpenFile(string&, ifstream&);
void MainMenu(ifstream&, string);
void AddEntry(ifstream&, string);
void PrintAddressBook(ifstream&);
void PrintRecord(entryType);
void PrintBriefRecord(entryType);
void ReadRecord(entryType&);
void WriteRecord(ofstream&, entryType);
void SearchMenu(ifstream&);
void SearchFirstName(ifstream&);
void SearchLastName(ifstream&);
void SearchAddress(ifstream&);
void SearchPhone(ifstream&);
bool GetRecord(ifstream&, entryType&);
void GetName(ifstream&, NameType&);
void GetPhone(ifstream&, PhoneType&);
void GetAddress(ifstream&, AddressType&);
void GetState(ifstream&, AddressType&);
string GetTitle(Title);
string NormalizeString(string);
void TrimSpaces(string&);
void PurgeLines(ifstream&, int);
int main()
{
entryType userRecord;
string filename;
ifstream inData;
char searchOption;
OpenFile(filename, inData);
MainMenu(inData, filename);
return 0;
}
void OpenFile(string& filename, ifstream& inData)
{
do {
cout << "Enter file name to open: ";
cin >> filename;
inData.open(filename.c_str());
if (!inData)
cout << "File not found!" << endl;
} while (!inData);
}
void MainMenu(ifstream& inData, string filename)
{
char menuOption;
do {
cout << endl;
cout << "Select an option..." << endl;
cout << "(A)dd entry, (P)rint, (S)earch, e(X)it: ";
cin >> menuOption;
menuOption = toupper(menuOption);
switch (menuOption) {
case 'A':
AddEntry(inData, filename);
break;
case 'P':
PrintAddressBook(inData);
break;
case 'S':
SearchMenu(inData);
break;
case 'X':
break;
default:
cout << "Invalid option selected!" << endl;
break;
}
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
} while (menuOption != 'X');
}
void AddEntry(ifstream& inData, string filename)
{
entryType userRecord;
ofstream outData;
ReadRecord(userRecord);
inData.close();
outData.open(filename.c_str(), fstream::app);
WriteRecord(outData, userRecord);
outData.close();
inData.open(filename.c_str());
}
void PrintAddressBook(ifstream& inData)
{
entryType userRecord;
char choice;
cout << "Do you want to print a brief list? (Y/N)";
cin >> choice;
choice = toupper(choice);
if (choice == 'N') {
// Loop through all records in the file
while (GetRecord(inData, userRecord)){
PrintRecord(userRecord);
}
}
else {
while (GetRecord(inData, userRecord)){
PrintBriefRecord(userRecord);
}
}
}
void SearchMenu(ifstream& inData)
{
char searchOption;
do {
cout << endl;
cout << "Enter how you want to search... " << endl;
cout << "(F)irst name, (L)ast name, (A)ddress, (P)hone, e(X)it: ";
cin >> searchOption;
searchOption = toupper(searchOption);
switch (searchOption) {
case 'F':
SearchFirstName(inData);
break;
case 'L':
SearchLastName(inData);
break;
case 'A':
SearchAddress(inData);
break;
case 'P':
SearchPhone(inData);
break;
case 'X':
break;
default:
cout << "Invalid option selected!" << endl;
break;
}
} while (searchOption != 'X');
}
// Searches passed file stream for a first name read from the user
void SearchFirstName(ifstream& inData)
{
string searchName;
entryType userRecord;
string normalSearchName, normalFirstName;
char choice;
bool found = false;
cout << "Enter first name to search for: ";
cin >> searchName;
normalSearchName = NormalizeString(searchName); // Convert name to all uppercase
// Loop through all records in the file
while (GetRecord(inData, userRecord)){
normalFirstName = NormalizeString(userRecord.name.firstName); // Convert retrieved string to all uppercase
if (normalFirstName == normalSearchName) { // Requested name matches
PrintRecord(userRecord);
cout << "Is this the correct entry? (Y/N)";
cin >> choice;
choice = toupper(choice);
cout << endl;
if (choice == 'Y') {
found = true;
break;
}
}
}
// Matching name was found before the end of the file
if (inData && !found){
cout << "Record found: " << endl;
PrintRecord(userRecord);
cout << endl;
}
else if (!found) // End of file. Name not found.
{
cout << searchName << " not found!" << endl << endl;
}
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
}
// Not implemented. (Similar to SearchFirstName)
void SearchLastName(ifstream& inData)
{
}
// Not implemented. (Similar to SearchFirstName)
void SearchAddress(ifstream& inData)
{
}
// Not implemented. (Similar to SearchFirstName)
void SearchPhone(ifstream& inData)
{
}
void GetName(ifstream& inData, NameType& userName)
{
getline(inData, userName.firstName, ',');
TrimSpaces(userName.firstName);
getline(inData, userName.lastName, ',');
TrimSpaces(userName.lastName);
}
void GetPhone(ifstream& inData, PhoneType& userPhone)
{
inData >> userPhone.areaCode;
inData >> userPhone.prefix;
inData >> userPhone.number;
}
void GetAddress(ifstream& inData, AddressType& userAddress)
{
getline(inData, userAddress.street, ',');
// TrimSpaces(address);
getline(inData, userAddress.city, ',');
GetState(inData, userAddress);
getline(inData, userAddress.zip, ' ');
}
void GetState(ifstream& inData, AddressType& userAddress)
{
getline(inData, userAddress.state, ',');
}
// Read all elements of an address book entry from a given file
// Return true if successful, false otherwise (Based on stream state)
bool GetRecord(ifstream& inData, entryType& userRecord)
{
userRecord.name.title = Mr;
GetName(inData, userRecord.name);
GetPhone(inData, userRecord.phone);
inData.ignore(100, ' ');
GetAddress(inData, userRecord.address);
return inData;
}
// Given all the elements of an address book entry, print the results
void PrintRecord(entryType userRecord)
{
cout << "Name: " << GetTitle(userRecord.name.title) << ". "
<< userRecord.name.firstName << " " << userRecord.name.lastName << endl;
cout << "Phone: " << "(" << userRecord.phone.areaCode << ") "
<< userRecord.phone.prefix << "-"
<< userRecord.phone.number << endl;
cout << "Address: " << userRecord.address.street
<< userRecord.address.city
<< userRecord.address.state
<< userRecord.address.zip << endl;
}
// Given all the elements of an address book entry, print the results
void PrintBriefRecord(entryType userRecord)
{
cout << "Name: " << GetTitle(userRecord.name.title) << ". "
<< userRecord.name.firstName << " " << userRecord.name.lastName << endl;
cout << "Phone: " << userRecord.phone.areaCode
<< userRecord.phone.prefix
<< userRecord.phone.number << endl;
}
string GetTitle(Title title)
{
switch (title) {
case Mr:
return "Mr";
break;
case Mrs:
return "Mrs";
break;
case Ms:
return "Ms";
break;
case Dr:
return "Dr";
break;
default:
return "Invalid";
}
}
void ReadRecord(entryType& userRecord)
{
cout << "Enter first and last name: ";
cin >> userRecord.name.firstName >> userRecord.name.lastName;
userRecord.phone.areaCode = 999;
userRecord.phone.prefix = 555;
userRecord.phone.number = 1212;
userRecord.address.street = "555 Mockingbird Ln";
userRecord.address.city = "Nowhere";
userRecord.address.state = "NotHere";
userRecord.address.zip = "00000";
}
void WriteRecord(ofstream& outData, entryType userRecord)
{
outData << userRecord.name.firstName << ","
<< userRecord.name.lastName << ",";
outData << userRecord.phone.areaCode << " "
<< userRecord.phone.prefix << " "
<< userRecord.phone.number << endl;
outData << userRecord.address.street << ", "
<< userRecord.address.city << ", "
<< userRecord.address.state << ", "
<< userRecord.address.zip << endl;
}
// Convert passed string to all upper case and return result
string NormalizeString(string str)
{
string nString = str;
int i;
for (i = 0; i < str.size(); i++)
{
nString[i] = toupper(str[i]);
}
return nString;
}
// Remove any leading or trailing spaces from given string
void TrimSpaces(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);
}
// **Not used**
// Read and discard a specified number of lines from a given file
void PurgeLines(ifstream& inData, int numLines)
{
string buffer;
for (int i = 0; i < numLines; i++)
getline(inData, buffer, ' ');
}
.
.
.
AddressBook.h
enum Title {Mr, Mrs, Ms, Dr};
struct NameType {
Title title;
std::string firstName;
char middleInitial;
std::string lastName;
};
struct AddressType {
std::string street;
std::string city;
std::string state;
std::string zip;
};
struct PhoneType {
int areaCode;
int prefix;
int number;
};
struct entryType {
NameType name;
AddressType address;
PhoneType phone;
};
.
.
test.txt
Mickey, Mouse, 123 555 1212
1 Disney Lane, Orlando, FL, 12345
Donald, Duck, 123 555 1213
1313 Webfoot Walk, Duckburg, CA, 00000
.
.
.
QUESTION
1. Modify the address book program so that it stores the address book entries in an array and adding the following features:
a. Store Address Book entries in an array.
b. Allow the user to add, delete, and display entries without having to re-read the file from disk.
Explanation / Answer
#include <iostream>
#include<string>
#include<windows.h>
using namespace std;
void gotoxy(int x, int y) //move the cursor to x y coordinate
{
HANDLE hConsoleOutput;
COORD Cursor = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
class Node
{ public:
string name,email,address,off_address ;
int phone_no,cell_no;
Node *next;
};
char y;
void Link_List_Input(Node *&head, Node*&tail) {
Node *current = new Node;
cout << " Enter following contents : ";
cout<<"NAME : ";
cin >> current->name;
cout<<"PHONE NUMBER : ";
cin >> current->phone_no;
cout<<"CELL NUMBER : ";
cin >> current->cell_no;
cout<<"EMAIL : ";
cin >> current->email;
cout<<"ADDRESS : ";
cin >> current->address;
cout<<"OFFICE ADDRESS : ";
cin >> current->off_address;
if (head == NULL >> tail == NULL)
{
head = current;
tail = current; tail->next=NULL;
} else {
current->next = head;head = current;
}
}
void Print_Link_List(Node *&head, Node*&tail)
{
if (head == NULL >> tail == NULL)
{
cout << "n List is empty";
return;
}
Node *current = head;
int count = 0;
do
{
cout << "n Node Number = " << ++count << " Has Content : "
<<current->name<<endl
<<current->phone_no<<endl
<<current->cell_no<<endl
<<current->email<<endl
<<current->address<<endl
<<current->off_address<<endl;
current = current->next;
}
while (current != 0); }
void DeleteAll(Node *&head,Node *&tail)
{
system("cls");
gotoxy(30,10);
cout<<"Erasing Complete Data :";
for(int i=0;i<16;i++)
{
gotoxy(8+i,14);
cout<<"ntttt ________________";
gotoxy(32,16);
cout<<char(179);
gotoxy(33+i,16);
cout<<char(178);
gotoxy(23+i,16);
cout<<"ntttt ================";
gotoxy(49,16);
cout<<char(179);
Sleep(100);
}
system("cls");
cout<<"nnnnnttttNO DATA LEFT :";
Sleep(1000);
system("cls");
cout<<"nnnnnntttReturning to Menu ";
for(int i=0;i<7;i++)
{
Sleep(600);
cout<<".";}
system("cls");
for (int j=0;head!=NULL;j++)
{
Node *ptr=head;
head=head->next;
delete ptr ;
}
return ;
}
void Search_In_List(Node *&head,Node *&tail)
{
Node *current = head;
string Name;
cout<<"Enter the string here ";
cin>>Name;
while (current!=NULL)
{
if (current->name.compare(0,Name.length(),Name)==0)
{
cout<<"Matching results :n";
cout<<"NAME: "<<current->name<<endl<<"PHONE NUMBER : "<<current->phone_no<<endl<<"CELL NUMBER : "<<current->cell_no<<endl
<<"EMAIL: "<<current->email<<endl<<"ADRESS : "<<current->address<<endl<<"Office Adress :"<<current->off_address<<endl;
current=current->next;
}
else
{
cout<<"Moving Next :";
current=current->next;
}
}
}
int Delete_Node_Contents(Node*&head)
{
string name;
cout<<"Enter the name for deleting : ";
cin>>name;
Node *current,*previous;
current=head;
if (head==NULL)
{
cout<<"list empty not possible"<<endl;
}
else
{
if(head->name.compare(0,name.length(),name)==0) //deleting head
{
Node *delhead=head;
cout<<"Node Contains :n"<<head->name<<endl<<head->phone_no<<endl<<head->cell_no<<endl<<endl<<head->email<<endl<<head->address<<endl<<endl<<head->off_address;
cout<<"Press Y to delete n OR Press any other key for menue ";
cin>>y;
if (char(98)==y||char(121)==y)
{
head = head->next;delete delhead;
cout<<"required data has been deleted";return 0;
}
else {cout<<"Next attempt :";
return Delete_Node_Contents(head->next);}
}
else
{
while (head->name.compare(0,name.length(),name)<=0 || current->next!=NULL)
{
if(current->next==NULL)
{
cout<<"not found";
return -1;
}
else
{
previous=current;
current=current->next;
}
}
previous->next=current->next;
delete current;
cout<<"required data has been deleted"<<endl;}
}
return 0;
}
int main()
{
Node *head, *tail;
head = NULL;
tail = NULL;
int choice = 0;
while (1) { Sleep(2000);system("cls");
cout << "n ***************** MAIN MENU *****************" << endl
<< " Press 1. Enter new entry " << endl
<< " Press 2. view all entry " << endl
<< " Press 3. Delete an entry " << endl
<< " Press 4. Search the entry " << endl
<< " Press 5. delete all entries " << endl
<< " Press 6: To Exit"
<< "n --------------------------------------------- " << endl
<< "Your Choice :";
cin >> choice;
switch (choice)
{
case 1:
Link_List_Input(head, tail);
cout<<"nnttSaving Data ";
{Sleep(1000);
cout<<" .";}
break;
case 2:
Print_Link_List(head,tail);
break;
case 3:
Delete_Node_Contents(head);
break;
case 4:
Search_In_List(head,tail);
break;
case 5:
DeleteAll(head,tail);
break;
case 6:
cout << " n Exiting .... " << endl;
return 0;
default:
cout << "n Invalid Option, Please use any of the listed options"
<< endl;
}
}
}
#include <iostream>
#include<string>
#include<windows.h>
using namespace std;
void gotoxy(int x, int y) //move the cursor to x y coordinate
{
HANDLE hConsoleOutput;
COORD Cursor = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
class Node
{ public:
string name,email,address,off_address ;
int phone_no,cell_no;
Node *next;
};
char y;
void Link_List_Input(Node *&head, Node*&tail) {
Node *current = new Node;
cout << " Enter following contents : ";
cout<<"NAME : ";
cin >> current->name;
cout<<"PHONE NUMBER : ";
cin >> current->phone_no;
cout<<"CELL NUMBER : ";
cin >> current->cell_no;
cout<<"EMAIL : ";
cin >> current->email;
cout<<"ADDRESS : ";
cin >> current->address;
cout<<"OFFICE ADDRESS : ";
cin >> current->off_address;
if (head == NULL >> tail == NULL)
{
head = current;
tail = current; tail->next=NULL;
} else {
current->next = head;head = current;
}
}
void Print_Link_List(Node *&head, Node*&tail)
{
if (head == NULL >> tail == NULL)
{
cout << "n List is empty";
return;
}
Node *current = head;
int count = 0;
do
{
cout << "n Node Number = " << ++count << " Has Content : "
<<current->name<<endl
<<current->phone_no<<endl
<<current->cell_no<<endl
<<current->email<<endl
<<current->address<<endl
<<current->off_address<<endl;
current = current->next;
}
while (current != 0); }
void DeleteAll(Node *&head,Node *&tail)
{
system("cls");
gotoxy(30,10);
cout<<"Erasing Complete Data :";
for(int i=0;i<16;i++)
{
gotoxy(8+i,14);
cout<<"ntttt ________________";
gotoxy(32,16);
cout<<char(179);
gotoxy(33+i,16);
cout<<char(178);
gotoxy(23+i,16);
cout<<"ntttt ================";
gotoxy(49,16);
cout<<char(179);
Sleep(100);
}
system("cls");
cout<<"nnnnnttttNO DATA LEFT :";
Sleep(1000);
system("cls");
cout<<"nnnnnntttReturning to Menu ";
for(int i=0;i<7;i++)
{
Sleep(600);
cout<<".";}
system("cls");
for (int j=0;head!=NULL;j++)
{
Node *ptr=head;
head=head->next;
delete ptr ;
}
return ;
}
void Search_In_List(Node *&head,Node *&tail)
{
Node *current = head;
string Name;
cout<<"Enter the string here ";
cin>>Name;
while (current!=NULL)
{
if (current->name.compare(0,Name.length(),Name)==0)
{
cout<<"Matching results :n";
cout<<"NAME: "<<current->name<<endl<<"PHONE NUMBER : "<<current->phone_no<<endl<<"CELL NUMBER : "<<current->cell_no<<endl
<<"EMAIL: "<<current->email<<endl<<"ADRESS : "<<current->address<<endl<<"Office Adress :"<<current->off_address<<endl;
current=current->next;
}
else
{
cout<<"Moving Next :";
current=current->next;
}
}
}
int Delete_Node_Contents(Node*&head)
{
string name;
cout<<"Enter the name for deleting : ";
cin>>name;
Node *current,*previous;
current=head;
if (head==NULL)
{
cout<<"list empty not possible"<<endl;
}
else
{
if(head->name.compare(0,name.length(),name)==0) //deleting head
{
Node *delhead=head;
cout<<"Node Contains :n"<<head->name<<endl<<head->phone_no<<endl<<head->cell_no<<endl<<endl<<head->email<<endl<<head->address<<endl<<endl<<head->off_address;
cout<<"Press Y to delete n OR Press any other key for menue ";
cin>>y;
if (char(98)==y||char(121)==y)
{
head = head->next;delete delhead;
cout<<"required data has been deleted";return 0;
}
else {cout<<"Next attempt :";
return Delete_Node_Contents(head->next);}
}
else
{
while (head->name.compare(0,name.length(),name)<=0 || current->next!=NULL)
{
if(current->next==NULL)
{
cout<<"not found";
return -1;
}
else
{
previous=current;
current=current->next;
}
}
previous->next=current->next;
delete current;
cout<<"required data has been deleted"<<endl;}
}
return 0;
}
int main()
{
Node *head, *tail;
head = NULL;
tail = NULL;
int choice = 0;
while (1) { Sleep(2000);system("cls");
cout << "n ***************** MAIN MENU *****************" << endl
<< " Press 1. Enter new entry " << endl
<< " Press 2. view all entry " << endl
<< " Press 3. Delete an entry " << endl
<< " Press 4. Search the entry " << endl
<< " Press 5. delete all entries " << endl
<< " Press 6: To Exit"
<< "n --------------------------------------------- " << endl
<< "Your Choice :";
cin >> choice;
switch (choice)
{
case 1:
Link_List_Input(head, tail);
cout<<"nnttSaving Data ";
{Sleep(1000);
cout<<" .";}
break;
case 2:
Print_Link_List(head,tail);
break;
case 3:
Delete_Node_Contents(head);
break;
case 4:
Search_In_List(head,tail);
break;
case 5:
DeleteAll(head,tail);
break;
case 6:
cout << " n Exiting .... " << endl;
return 0;
default:
cout << "n Invalid Option, Please use any of the listed options"
<< endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.