Question: I need help making this c++ program search by by partial name and return likely matches. For example if John Doe, John Deer, and John

I need help making this c++ program search by by partial name and return likely matches. For example if "John Doe, John Deer, and John Oliver" are in the address book, and the user types in "John" for the search option, it should return all 3. _________MAIN.CPP__________

#include #include #include #include using namespace std;

#include "Person.h"

int menu(); int find(vector, string); //function to update the data to file every time void writeToFile(ofstream& , vector);

//start of main() int main() { // Container for holding all Person objects. vector people; ofstream out;

while(true) { int choice = menu(); PersonDetails p; Person *person; string name; int index; switch(choice) { case 1: getline(cin,name); cout<<"Enter name: "; getline(cin,p.name); cout<<" Enter phone: "; getline(cin,p.phone ); cout<<" Enter address: "; getline(cin,p.address); cout<<" Enter email: "; getline(cin,p.email); cout<<" Enter website: "; getline(cin,p.website); person = new Person(p); people.push_back(person); writeToFile(out, people); break; case 2: getline(cin,name); cout<<" Enter name to search: "; getline(cin,name); index = find(people, name); if(index<0) cout<<" Person not found "; else cout<get_name()<< "Found"; break; case 3: getline(cin,name); cout<<" Enter name to remove: "; getline(cin,name); index = find(people, name); if(index<0) cout<<" Person not found "; else people.erase(people.begin() + index); writeToFile(out, people); break; case 4: exit(0); } } system("pause"); return 0; }

int menu() { int choice; //add a person to the text file where names/addresses/etc. are stored cout << " 1. Add Person" << endl; //search the text file for a name and it will return all of the person's info cout << "2. Search Directory" << endl; //removes a person's entry from the text file along with all of their personal data cout << "3. Remove Person" << endl; //exits from the program cout << "4. Exit" << endl; cout<<" Enter your choice: "; cin>>choice; return choice; }

// Finds a person and returns it index. int find(vector pv1, string name) { for (unsigned int i = 0; i < pv1.size(); ++i) { if (pv1.at(i)->get_name() == name) { return i; } } return -1; }

void writeToFile(ofstream& out, vector people) { out.open("information.txt"); for(int i=0; i_name<<" "<_phone<<" "<_address<<" "<_email; out<<" "<_website<PERSON.H_______________

#ifndef PERSON_H_ #define PERSON_H_

#include using namespace std;

struct PersonDetails { string name, phone, address, email, website; };

class Person { public: string _name; string _phone; string _address; string _email; string _website; Person(); Person(PersonDetails p1); virtual ~Person();

string get_name();

friend ostream & operator << (ostream &, Person &); };

#endif /* PERSON_H_ */ ____________PERSON.CPP_____________

#include "Person.h" #include

Person::Person() { // Init all _name = ""; _phone = ""; _address = ""; _email = ""; _website = ""; }

Person::Person(PersonDetails pd1) { _name = pd1.name; _phone = pd1.phone; _address = pd1.address; _email = pd1.email; _website = pd1.website; }

Person::~Person(){}

string Person::get_name() { return this->_name; }

ostream & operator << (ostream &os, Person &p1) { os << p1._name; return os; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!