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

Can someone write an Address Book program with the following guidelines? It also

ID: 3846500 • Letter: C

Question

Can someone write an Address Book program with the following guidelines? It also needs comments throughout so it is easy to follow for someone else.

Guidelines:

Fully modularize your program

Use pass by reference parameters wherever appropriate

You must use record (structs) stored in an array or vector

Your program must be able to save the vector of input data to a file and reopen that file later.

Your program must be menu driven

The menu shall have following functionality:

Enter a single new record (same as example)

Enter many records one-after-the-other option (not in the example)

Edit and Delete records

Search for and display one record (part of this functionality is required for edit and delete suggesting that “search” is a function used by more than one process)

Display a listing of all records (truncate appropriately so that all important information appears on one line)

Sort by name or id of record

Sort by one other field of your choice

Save to file

Open file

Quit

Explanation / Answer

C++ Address book program code as specified in the question

AddressBook.cpp

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct addressBook
{
   int id;
   string firstName;
   string lastName;
   int phnNumber;
};
int co=1;
void add(vector<addressBook> &address)
{
   struct addressBook ad;
   cout<<"Enter the first name: ";
   cin.ignore();
   getline(cin,ad.firstName);
   cout<<"Enter the last name: ";
   getline(cin,ad.lastName);
   cout<<"Enter the phone number: ";
   cin>>ad.phnNumber;
   ad.id=co;
   address.push_back(ad);
       co++;
}
void openFile(vector<addressBook> &address)
{
   struct addressBook ad;
   ifstream in;
   string line;
   in.open("inp.txt");
   while(getline(in,line))
   {
       istringstream is(line);
       is>>ad.id;
       is>>ad.firstName;
       is>>ad.lastName;
       is>>ad.phnNumber;
       address.push_back(ad);
       co++;
   }
   in.close();
}
void writeFile(vector<addressBook> &address)
{
   ofstream ou;
   ou.open("in.txt");
   for(int j=0;j<address.size();j++)
   {
       ou<<address.at(j).id;
       ou<<" ";
       ou<<address.at(j).firstName;
       ou<<" ";
       ou<<address.at(j).lastName;
       ou<<" ";
       ou<<address.at(j).phnNumber;
       ou<<" ";
   }
}
void display(vector<addressBook> &address)
{
   for(int j=0;j<address.size();j++)
   {
       cout<<address.at(j).id;
       cout<<" ";
       cout<<address.at(j).firstName;
       cout<<" ";
       cout<<address.at(j).lastName;
       cout<<" ";
       cout<<address.at(j).phnNumber;
       cout<<" ";
   }
}
void search(vector<addressBook> &address)
{
   string name;
   cout<<"Enter the first name to be searched: ";
   cin.ignore();
   getline(cin,name);
   int ind;
   bool found=false;
   for(int j=0;j<address.size();j++)
   {
       if(name==address.at(j).firstName)
       {
           found=true;
           ind=j;
           break;
       }
   }
   if(found)
   {
       cout<<"Details of person :"<<endl;
       cout<<address.at(ind).id;
       cout<<" ";
       cout<<address.at(ind).firstName;
       cout<<" ";
       cout<<address.at(ind).lastName;
       cout<<" ";
       cout<<address.at(ind).phnNumber;
       cout<<" ";
   }
   else
   {
       cout<<"Person is not found "<<endl;
   }
}
bool compareByID(const addressBook &a1, const addressBook &a2)
{
    return a1.id < a2.id;
}
bool compareByName(const addressBook &a1, const addressBook &a2)
{
    return a1.firstName < a2.firstName;
}
void sortID(vector<addressBook> &address)
{
   std::sort(address.begin(), address.end(), compareByID);
}
void sortName(vector<addressBook> &address)
{
   std::sort(address.begin(), address.end(), compareByName);
}
void deleteRecord(vector<addressBook> &address)
{
   string name;
   cout<<"Enter the first name to be deleted: ";
   cin.ignore();
   getline(cin,name);
   for(int j=0;j<address.size();j++)
   {
       if(name==address.at(j).firstName)
       {
           address.erase (address.begin()+j);
       }
   }
}
int main()
{
   vector<addressBook> address;
   int ch=1;
   while(ch!=9)
   {
       cout<<"1: Enter single new record"<<endl;
       cout<<"2: Search record"<<endl;
       cout<<"3: Dispaly record"<<endl;
       cout<<"4: Sort by ID"<<endl;
       cout<<"5: Delete record"<<endl;
       cout<<"6: Open file"<<endl;
       cout<<"7: Write file"<<endl;
       cout<<"8: Sort by Name"<<endl;
       cout<<"8: Exit"<<endl;
       cout<<"Enter your choice"<<endl;
       cin>>ch;
       switch(ch)
       {
       case 1:add(address);
           break;
       case 2:search(address);
           break;
       case 3: display(address);
           break;
       case 4:sortID(address);
           break;
       case 5: deleteRecord(address);
           break;
       case 6: openFile(address);
           break;
       case 7:writeFile(address);
           break;
       case 8:sortName(address);
           break;
       case 9: break;
       }
   }
   system("pause");
   return 0;
}

Output:

1: Enter single new record
2: Search record
3: Dispaly record
4: Sort by ID
5: Delete record
6: Open file
7: Write file
8: Sort by Name
8: Exit
Enter your choice
6
1: Enter single new record
2: Search record
3: Dispaly record
4: Sort by ID
5: Delete record
6: Open file
7: Write file
8: Sort by Name
8: Exit
Enter your choice
3
1 Drisya Mathew 4455
2 Visal Vijayan 5599
3 Edwin Paul 6699
1: Enter single new record
2: Search record
3: Dispaly record
4: Sort by ID
5: Delete record
6: Open file
7: Write file
8: Sort by Name
8: Exit
Enter your choice
8
1: Enter single new record
2: Search record
3: Dispaly record
4: Sort by ID
5: Delete record
6: Open file
7: Write file
8: Sort by Name
8: Exit
Enter your choice
3
1 Drisya Mathew 4455
3 Edwin Paul 6699
2 Visal Vijayan 5599
1: Enter single new record
2: Search record
3: Dispaly record
4: Sort by ID
5: Delete record
6: Open file
7: Write file
8: Sort by Name
8: Exit
Enter your choice

Input File:

1 Drisya Mathew 4455
2 Visal Vijayan   5599
3 Edwin Paul 6699

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