C++ Address book the program should allow user to search by name, address or pho
ID: 3817450 • Letter: C
Question
C++ Address book the program should allow user to search by name, address or phone number. Add entry and delete an entry. Prompts user for filename and reads it into an array. This is my code so far. DO NOT USE vectors or nodes. Try to Use the function prototypes I created.
File
Bob King 123 456 8900
101 main street Orlando Fl 12345
Susan Smith 454 676 8888
102 Market Drive Nashville TN 76733
Header File
#include<string>
using namespace std;
enum Title {Mr, Mrs, Ms, Dr, NA};
struct NameType {
Title title;
string firstName;
string lastName;
};
struct AddressType {
string street;
string city;
string state;
string zip;
};
struct PhoneType {
int areaCode;
int prefix;
int number;
};
struct entryType {
NameType name;
AddressType address;
PhoneType phone;
};
const int MAX_RECORDS = 50;
struct addressBookType {
entryType record[MAX_RECORDS];
int numEntries;
};
#include "AddressBook.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
// Pre:
// Post:
void OpenFile(string& filename, ifstream& inData);
// Pre:
// Post:
void MainMenu(ifstream& inData, string filename);
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);
void DeleteEntry(ifstream&);
string NormalizeString(string);
void TrimSpaces(string&);
void PurgeLines(ifstream&, int);
entryType bookArray[MAX_RECORDS];
int bookCount = 0;
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;
for(int i=0; i<MAX_RECORDS;i++)
{
inData >> bookArray[i].name.firstName;
inData>>bookArray[i].name.lastName;
}
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, (D)elete 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 'D':
DeleteEntry(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);
cout << endl;
}
}
else {
while (GetRecord(inData, userRecord)){
PrintBriefRecord(userRecord);
cout << endl;
}
}
}
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)
{
entryType userRecord;
string searchName;
string normalSearchName, normalFirstName;
char choice;
bool found = false;
cout << "Enter first name to search for: ";
cin >> searchName;
for (int i=0;i<MAX_RECORDS;i++)
{
normalFirstName = NormalizeString(bookArray[i].name.firstName); // Convert retrieved string to all uppercase
if (normalFirstName == normalSearchName) { // Requested name matches
cout<<bookArray[i].name.firstName;
cout<<bookArray;
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
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
}
Explanation / Answer
#include <iostream>
#include<string>
#include<windows.h>
utilizing namespace sexually transmitted disease;
void gotoxy(int x, int y) /move the cursor to x y facilitate
{
HANDLE hConsoleOutput;
COORD Cursor = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
class Node
{ open:
string name,email,address,off_address ;
int phone_no,cell_no;
Hub *next;
};
scorch y;
void Link_List_Input(Node *&head, Node*&tail) {
Hub *current = new Node;
cout << " Enter taking after substance : ";
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;
in the event that (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)
{
in the event that (head == NULL >> tail == NULL)
{
cout << "n List is vacant";
return;
}
Hub *current = head;
int check = 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++)
{
Hub *ptr=head;
head=head->next;
erase ptr ;
}
return ;
}
void Search_In_List(Node *&head,Node *&tail)
{
Hub *current = head;
string Name;
cout<<"Enter the string here ";
cin>>Name;
while (current!=NULL)
{
on the off chance that (current->name.compare(0,Name.length(),Name)==0)
{
cout<<"Matching comes about :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 erasing : ";
cin>>name;
Hub *current,*previous;
current=head;
in the event that (head==NULL)
{
cout<<"list empty not possible"<<endl;
}
else
{
if(head->name.compare(0,name.length(),name)==0) /erasing head
{
Hub *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 erase n OR Press some other key for menue ";
cin>>y;
in the event that (char(98)==y||char(121)==y)
{
head = head->next;delete delhead;
cout<<"required information has been deleted";return 0;
}
else {cout<<"Next endeavor :";
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;
}
}
past >next=current->next;
erase current;
cout<<"required information has been deleted"<<endl;}
}
return 0;
}
int principle()
{
Hub *head, *tail;
head = NULL;
tail = NULL;
int decision = 0;
while (1) { Sleep(2000);system("cls");
cout << "n ***************** MAIN MENU *****************" << endl
<< " Press 1. Enter new passage " << endl
<< " Press 2. see all passage " << endl
<< " Press 3. Erase a passage " << endl
<< " Press 4. Look the section " << endl
<< " Press 5. erase all passages " << endl
<< " Press 6: To Exit"
<< "n - - - " << endl
<< "Your Choice :";
cin >> decision;
switch (decision)
{
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 utilize any of the recorded alternatives"
<< endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.