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 "Person.h"
int menu(); int find(vector
//start of main() int main() { // Container for holding all Person objects. vector
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<
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
void writeToFile(ofstream& out, vector
#ifndef PERSON_H_ #define PERSON_H_
#include
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
Get step-by-step solutions from verified subject matter experts
