The following program allows the user to enter a list of names (ending this process by entering -1) and then the user enters the best friend of each person. The output lists the person, their best friend, and the popularity count of the person.
#include <iostream>
#include <iomanip> //for setw
#include <fstream> // for ifstream
#include <string> // for string
#include <vector> // for vector
#include "Person.h" // for use of the .h file
usingnamespace std;
main()
{
string name;
vector<Person *> people; // vector of points to objects of type Person
Person * person_pointer;
cout << "Please, enter the names (end the program by entering -1): " << endl;
cin >> name;
while (name != "-1") {
int i;
for (i = 0; i < people.size(); i++)
if (people[i]->get_name()==name)
break;
if (i==people.size()) {
person_pointer = new Person(name);
people.push_back(person_pointer);
cout << "Please, enter another name (end the program with -1): " << endl;
cin >> name;
} else {
cout << name << " has already been entered. Please, enter another name." << endl;
cin >> name;
}
}
int i, j;
// for each person prompt for a best friend name
for (i = 0; i < people.size(); i++) {
cout << "Who is " << people[i] ->get_name() << "'s best friend? ";
cin >> name;
// search for best friend
for (j = 0; j < people.size(); j++)
if (people[j] ->get_name() == name)
break;
if (j < people.size()) // found a best friend
people[i] ->set_best_friend(people[j]);
else
cout << "Couldn't find best friend " << name << endl;
}
// list of name, best friend, and popularity count
for (i = 0; i < people.size(); i++) {
person_pointer = people[i];
cout << left << setw(10) << person_pointer->get_name();
cout << left << setw(10) << person_pointer->get_best_friend();
cout << right << setw(2) << person_pointer->get_popularity() << endl;
}
// clean up
for (i = 0; i < people.size(); i++)
delete people[i];
}
The issue that I'm having is that I need the program to prompt the user for when a best friend is entered that doesn't exist. For example, when I run the person list as the following...
Jon
Jan
Kim
Taylor
...then I run their best friends...
Kim
Sam
Jon
Kim
Sam will not appear on the list. I need it to tell the user that they need to list another name that exists from the person list.