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

MAIN.CPP #include <iostream> #include <string> #include \"libpb.hpp\" int main (

ID: 3717559 • Letter: M

Question

MAIN.CPP

#include <iostream>
#include <string>
#include "libpb.hpp"

int main () {
char cont;
string find_name, str;
phone_book pb;
personal_info person;

cout << " ********************************************* ";
cout << " Start with entering new contacts! ";
cout << " ********************************************* ";
cout << " Would you like to enter a new contact (Y/N): ";

while(pb.get_num_people() < 20) {
cin >> cont;

if (cont == 'Y') {
cout << "Enter a first name: ";
cin >> str;
person.setfirst(str);
cout << "Enter " << person.getfirst() << "'s last name: ";
cin >> str;
person.setlast(str);
cout <<"Enter " << person.getfirst() <<"'s phone number: ";
cin >> str;
person.setphone(str);
pb.add_person(person);}
else if (cont == 'N') break;
else if (cont == ' ') continue;
else cout <<"Error: User entered '" << cont <<"'. Must enter either 'Y' or 'N'" << endl;
  
cout << endl << "Would you like to enter a new name (Y/N): ";

}

//search phone book by first name and print persons

cout << " ********************************************* ";
cout << " Now You can search for names! ";
cout << " ********************************************* ";
cout << " Would you like to search for a name (Y/N)? ";

while(1){
cin >> cont;
  
if (cont == 'Y'){
cout << "Enter a person's name to search for: ";
cin >> find_name;
pb.search_pb(find_name);}
else if (cont == 'N') break;
else if (cont == ' ') continue;
else cout << "Error: User entered '" <<cont <<"'. Must enter either 'Y' or 'N'" << endl;
  
cout << endl << "Would you like to search for a name (Y/N)? ";

}
return 0;
}

LIBPB.C

#include "libpb.h"
#include <string.h>
#include <stdio.h>

void add_person(struct phone_book * pb, struct personal_info person){
pb->people[pb->num_people] = person;
pb->num_people++;
return;
}

void search_pb(struct phone_book pb, char find_name[]){

int num_found = 0;

for(int i = 0; i < pb.num_people; i++) {
if(!strcmp(find_name,pb.people[i].first)) {
printf(" Name: %s %s Phone: %s ", pb.people[i].first, pb.people[i].last, pb.people[i].phone);
num_found++;
}
}
if(num_found == 0) {
printf("No entries with that name. ");
}

return;
}

LIBPB.H

#ifndef __LIBPB_H__
#define __LIBPB_H__

struct personal_info {
char first[25];
char last[25];
char phone[15];
};

struct phone_book {
struct personal_info people[20];
int num_people;
};

void add_person(struct phone_book * pb, struct personal_info person);
void search_pb(struct phone_book pb, char find_name[]);

#endif

Must convert libpb.c and libpb.h to compatible c++ code to work with main.cpp

Replace all of the C code in libpb.c and libpb.h (provided in the supplementary folder) with C+ code. Rename libpb.c to libpb.cpp and libpb.h to libpb.hpp. Convert the personalinfo structure into a class with three private data member strings for the first name, last name, and phone num ber. To access private members of a class, there are typically public get and set functions. Get/set member function prototypes for the data members of the class personalinfo are shown below (to be added into your libpb.hpp file). There is a get and a set for each private data member. A set function will set the private member equal to the functions input. A get function will return the private member's value. This is typically good practice for things that should not be accessed directly (think of them as hidden from the end user).

Explanation / Answer

/*libpb.hpp*/

class personal_info{

   private:
      string first;
      string last;
      string phone;

   public:
      void setfirst(string a);
      void setlast(string a);
      void setPhone(string a);
      string getfirst();
      string getlast();
      string getphone();
};

class phone_book {
    private:
        vector<persona_info> list;
    public:
        void add_person(personal_info p);
        void search_pb(string a);
        int get_num_people();
};   

/*libpb.cpp */

#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

#include "lipb.hpp"

void personal_info::setfirst(string a){
          first = a;
}
void personal_info::setlast(string a){
          last = a;
      }
void personal_info::setPhone(string a){
          phone = a;
}
string personal_info::getfirst(){
          return first;
}
string personal_info::getlast(){
          return last;
}
string personal_info::getphone(){
          return phone;
}

void phone_book::add_person(personal_info p){
     list.push_back(p);
}
void phone_book::search_pb(string a){
     int found = 0;
     for (int i = 0; i<list.size(); i++){
         if (list[i].getfirst() == a){
              cout << list[i].getfirst() << " " << list[i].getlast() << list[i].getphone() << endll;
              found = 1;
         }
     }
     if (found == 0)
        cout << "Not found ";  
}
int phone_book::get_num_people(){
     return list.size();
}