Arrays, Vectors, & Pointers

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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

using namespace 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.
Last edited on
I forgot to also include the .h file and the other .cpp file

this is the .h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef PERSON_H
#define PERSON_H
#include <string>

using namespace std;

class Person
{
     string name;
     Person * best_friend;
     int popularity;
public:
     Person(): name(""), best_friend(0), popularity(0) {}
     Person(string n): name(n), best_friend(0), popularity(0) {}
     string get_name();
     string get_best_friend();
     int get_popularity();
     void set_best_friend(Person *);
};

#endif 


this is the other .cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Person.h"

string Person::get_name()
{
     return name;
}

string Person::get_best_friend()
{
     if (best_friend != 0) // check for null pointer
          return best_friend->name;
     else return "";
}

int Person::get_popularity()
{
     return popularity;
}

void Person::set_best_friend(Person * bf)
{
     best_friend = bf;
     (bf->popularity)++;
}
Topic archived. No new replies allowed.