For this project, you will write a program similar to the Facebook website. This program will let you add people to a global database of "friends" and manipulate these friendships.
struct person
{
string name;
string school;
vector<person*> friends;
};
The struct contains the following information about a person on Facebook:
int main() { vector<person*> all_people; // rest of code here // very last lines should deallocate all the people in the vector (delete them) return 0; }The entire friendship network is maintained through this vector of people. All of the individual people in the network should be allocated on the heap (using
new
),
and then you will add pointers to those people to the all_people
vector. At
the end of main, you should use delete
to deallocate the memory for
all the people in the vector. Note that you will need a loop to do this, since you will
have to delete
each individual person one at a time. (This is unlike
a C++ array, where we could use delete[]
to deallocate the entire array
at once.)
person
on the heap (using new
), then use push_back
to add the
pointer to the new person to the all_people
vector. This person will initially have no friends.all_people
vector for each person's name. This should give you two pointers (one to each person).
Then add each pointer to the other person's friends
vector.people.txt
that contains an initial list
of people and their friends. See below for details of how this file is arranged.
people.txt
fileperson
or the string friend
.
person
, the next two words will be the name of
the person and their school (these are each guaranteed to be one word only).
friend
, the next two words will be two
names of people who should be added as friends in the database (friendships are reciprocal,
so if "A" is friends with "B," then "B" is also friends with "A," even though the friendship
will only be listed once in this file).
Here is a sample people.txt file.
1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 1 alice at rhodes is friends with bob at rhodes carla at sewanee dave at sewanee bob at rhodes is friends with alice at rhodes carla at sewanee erin at rhodes carla at sewanee is friends with alice at rhodes bob at rhodes dave at sewanee is friends with alice at rhodes erin at rhodes erin at rhodes is friends with dave at sewanee bob at rhodes 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 2 Who to look for? bob bob at rhodes is friends with alice at rhodes carla at sewanee erin at rhodes 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 3 Who to look for? bob bob at rhodes is friends with alice at rhodes erin at rhodes 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 4 What is this person's name? fred What is their school? hendrix 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 5 Name 1? bob Name 2? fred 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 1 alice at rhodes is friends with bob at rhodes carla at sewanee dave at sewanee bob at rhodes is friends with alice at rhodes carla at sewanee erin at rhodes fred at hendrix carla at sewanee is friends with alice at rhodes bob at rhodes dave at sewanee is friends with alice at rhodes erin at rhodes erin at rhodes is friends with dave at sewanee bob at rhodes fred at hendrix is friends with bob at rhodes 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 6 Who is changing schools? bob Change to which school? hendrix 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 1 alice at rhodes is friends with bob at hendrix carla at sewanee dave at sewanee bob at hendrix is friends with alice at rhodes carla at sewanee erin at rhodes fred at hendrix carla at sewanee is friends with alice at rhodes bob at hendrix dave at sewanee is friends with alice at rhodes erin at rhodes erin at rhodes is friends with dave at sewanee bob at hendrix fred at hendrix is friends with bob at hendrix 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 7 Name 1? bob Name 2? alice 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 1 alice at rhodes is friends with carla at sewanee dave at sewanee bob at hendrix is friends with carla at sewanee erin at rhodes fred at hendrix carla at sewanee is friends with alice at rhodes bob at hendrix dave at sewanee is friends with alice at rhodes erin at rhodes erin at rhodes is friends with dave at sewanee bob at hendrix fred at hendrix is friends with bob at hendrix 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 8 Who to remove? bob 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 1 alice at rhodes is friends with carla at sewanee dave at sewanee carla at sewanee is friends with alice at rhodes dave at sewanee is friends with alice at rhodes erin at rhodes erin at rhodes is friends with dave at sewanee fred at hendrix is friends with nobody 1. Print all people and friends. 2. Print the friends of a person. 3. Print the friends of a person who go to the same school. 4. Add a new person. 5. Add a new friendship. 6. Change a person's school. 7. Remove a friendship. 8. Remove a person (and all their friendships). 9. Quit. Enter choice: 9
friend
is a reserved word in C++, meaning you can't use it as a variable or function name (it's part of the language
itself).
The plural, friends
, is fine.