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!

There are 2 -3 compile time errors in your code..

#include<iostream>
#include<vector>
#include<iomanip>
#include<string>
using namespace std;
class Person
{
private:
string name;

// Make this bestFriendName. You have used bestFriendName in the function definitions 

Person* bestFriend;
int count;
public:
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setBestFriend(Person *p)
{
// This should be p not b
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")
{
// No need for this line. Memory is already allotted when you create the array of objects
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;
}

This should clear your compile time bugs... But there are logical bugs in your code. When I ran your code I got a seg fault.

commented: big help +0

There are 2 -3 compile time errors in your code..

This should clear your compile time bugs... But there are logical bugs in your code. When I ran your code I got a seg fault.

Thank you for the help, I managed to work thorugh and get the program to compile. I'm getting a strange error now.."ACCESS VIOLATION(SEGMENTATION FAULT) RAISED IN YOUR PROGRAM"..I'm guessing that it was the segment error you referred to? It gets locked up when it starts to display (or tries to) the best friend of each name. It's giving me a bazaar number. Is that a memory address issue?

Why are you declaring a vector of Persons as well as an array of Person ? You need to have one not both
The way you are storing the best friends name is wrong. As of now you have a string called best friend in the main function. The getline call stores the input in this string. I dont think this is what you wanted. Because of this the display function gives a seg fault

got it all sorted out...thanks for the help!

got it all sorted out...thanks for the help!

You are welcome .... Mark this thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.