write a program that reads in a list of names and stores them in vector<person>. Then ask the name of the best friend for each person in the list. For each best friend, locate the Person object matching the best friend and call a setBestFriend(Person* newBestFriend) memeber function to update the bestFriend pointer for this person and the poularityCount for the friend. Finally pront out the name, best friend and poularity count for each person in the list.
this is what I have come up with so far but am now stumped!! 4 errors is the best I can do
#include<iostream>
#include<vector>
#include<iomanip>
#include<string>
using namespace std;
class Person
{
private:
string name;
Person* bestFriend;
int count;
public:
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setBestFriend(Person *p)
{
bestFriendName = b;
}
Person* getBestFriend()
{
return bestFriendName;
}
void setCount()
{
count = count + 1;
}
void display()
{
cout << "name: " << name << endl;
cout << "bestfriend: " << (*getBestFriend()).getName() << endl;
cout << "popularity: " << count << endl;
}
};
int main()
{
string yourName = "";
string bestFriendName = "";
int number = 0;
bool done = false;
vector<Person>persons;
Person myPerson[20];
cout << "Enter name (-1 to stop): "<< endl;
getline(cin, yourName);
while(yourName != "-1")
{
myPerson[number] =new Person;
myPerson[number++].setName(yourName);
cout << "Enter name (-1 to stop): " << endl;
getline(cin, yourName);
if (yourName == "-1")
{
for(int i = 0; i < number; i++)
{
cout << "Enter my best friend of" << myPerson[i].getName() << endl;
getline(cin,bestFriendName);
}
}
}
for(int i = 0; i < number; i++)
{
myPerson[i].display();
}
system("pause");
return 0;
}
the output should look like this:
Enter name (-1 to stop): Homer
Enter name (-1 to stop): Marge
Enter name (-1 to stop): Lisa
Enter name (-1 to stop): Bart
Enter name (-1 to stop): Maggie
Enter name (-1 to stop): -1
Enter best friend of Homer: Marge
Enter best friend of Marge: Homer
Enter best friend of Lisa: Marge
Enter best friend of Bart: Bart
Enter best friend of Maggie: Marge
name: Homer
best friend: Marge
popularity count: 1
name: Marge
best friend: Homer
popularity count: 3
name: Lisa
best friend: Marge
popularity count: 0
name: Bart
best friend: Bart
popularity count: 1
name: Maggie
best friend: Marge
popularity count: 0
Thanks for any help!