Hi I need some help on this C++ lab.
I need to get user input for names and then search for names matching that input. I can't seems to figure out the issue in my code. I feel like it is somewhere between these lines
Person searchPerson(searchLastName, searchFirstName, 0);
Person* sPerson = &searchPerson;
// get count of such persons
int cntPersons = persSet.count(sPerson);
cout << "Number of people with this name= " << cntPersons;
I need to output the names matching the inputted names by the user but it keeps returning 0. I need some fresh eyes on this because I feel like it is a simple fix that I just can't see. Full code posted below
#pragma warning (disable:4786) // for set (Microsoft only)
#include <iostream>
#include <set>
#include <iterator>
#include <string>
#include <functional>
using namespace std;
class Person
{
private:
string lastN, firstN;
long phoneNum;
public:
Person()
{
lastN = "blank";
firstN = "blank";
phoneNum = 0;
}
Person(string lastName, string firstName, long number)
{
lastN = lastName;
firstN = firstName;
phoneNum = number;
}
//operator == for person class
friend bool operator==(const Person& p1, const Person& p2)
{
return (p1.lastN == p2.lastN &&
p1.firstN == p2.firstN) ? true : false;
}
friend bool operator<(const Person& p1, const Person& p2)
{
if (p1.lastN == p2.lastN)
return (p1.firstN < p2.firstN) ? true : false;
return (p1.lastN < p2.lastN) ? true : false;
}
void display() const //display person's data
{
cout << endl << lastN << ",\t" << firstN;
cout << "\t\tPhone: " << phoneNum;
}
};
int main()
{
string searchLastName, searchFirstName;
Person* ptrP1 = new Person("KuangThu", "Bruce", 4157300);
Person* ptrP2 = new Person("Deauville", "William", 8435150);
Person* ptrP3 = new Person("Wellington", "John", 9207404);
Person* ptrP4 = new Person("Bartoski", "Peter", 6946473);
Person* ptrP5 = new Person("Fredericks", "Roger", 7049982);
Person* ptrP6 = new Person("McDonald", "Stacey", 7764987);
Person* ptrP7 = new Person("KuangThu", "Bruce", 4157300);
Person* ptrP8 = new Person("Deauville", "William", 8435150);
// multiset of persons
multiset<Person*, less<Person*> > persSet;
// iterator to a multiset of persons
multiset<Person*, less<Person*> >::iterator iter;
persSet.insert(ptrP1); // put persons in multiset
persSet.insert(ptrP2);
persSet.insert(ptrP3);
persSet.insert(ptrP4);
persSet.insert(ptrP5);
persSet.insert(ptrP6);
persSet.insert(ptrP7);
persSet.insert(ptrP8);
cout << "Number of Entries= " << persSet.size();
iter=persSet.begin(); //display contents of multiset
while (iter != persSet.end())
{
(*iter++)->display();
}
cout << endl<<"Enter the last name of person to search for: ";
cin >> searchLastName;
cout << endl<<"Enter their first name: ";
cin >> searchFirstName;
//creating a search for a person with this name
Person searchPerson(searchLastName, searchFirstName, 0);
Person* sPerson = &searchPerson;
// get count of such persons
int cntPersons = persSet.count(sPerson);
cout << "Number of people with this name= " << cntPersons;
//output all the matches to the name
iter = persSet.lower_bound(sPerson);
while (iter != persSet.upper_bound(sPerson))
{
(*iter++)->display();
}
cout << endl;
return 0;
}