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

five crown program part 2 based on original program For this assignment you will

ID: 3864310 • Letter: F

Question

five crown program part 2 based on original program For this assignment you will modify your phone book program 1. Prompt the user and determine if they want to pre-populate their phone book vector from a file. o If so, the program will open the file specified and read in the series of names and numbers stored in the file. 2. After loading the names and numbers into the phone book vector (as specified by the user), prompt the user to determine if they want to add any additional names/numbers o If so, allow the user to add as many new names/numbers as desired. 3. Display your final phone list in a formatted table. 4. Save all the entries in your phone book vector to an output file (in a format that you can use for input in step 1). Extra Credit: 1. Overload the operator for the phone book object 2. Implement the vector sort method to ensure the phone book is sorted by either name or phone number prior to being saved in the output file. Submit your source code files (.cpp/ .h), your output file, and multiple screenshots demonstrating your program works.

Explanation / Answer

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

/* Declaring phonebook structure */
struct phoneBook {
string name;
int number;
string email;
};

int main(){
   // declaring vector of phoneBook
   vector<phoneBook> book;
   string fileName;
   // reading file
   cout<<"Enter file name to read contacts: ";
   cin>>fileName;
  
   std::string myline;
   std::ifstream infile(fileName);
   while (std::getline(infile, myline))
   {
       std::istringstream iss(myline);
       string name;
       int number;
       string email;
       if (!(iss >> name >> number >> email)) { break; }
       //pushing into vector
       book.push_back (name,number,email);
   }
  
   //reading extra contacts from user
   while(true){
       int choice;
       cout<<"Do you want to add extra contact then type 1 and 2 to exit : ";
       cin>>choice;
       if(choice == 1){
           string name;
           int number;
           string email;
           cout<<"Enter name: ";
           cin>>name;
           cout<<"Enter number: ";
           cin>>number;
           cout<<"Enter email: ";
           cin>>email;
           book.push_back (name,number,email);
       }
       else{
           break;
       }
   }

   // printing phone book
   cout<<"Contacts are here: "<<endl;
   for(int i=0;i<book.size();i++) {
       cout<<book[i].name<<" "<<book[i].number<<" "<<book[i].email<<endl;
   }
   return 0;
}