Classes, Vectors, and Pointers

I am having a little trouble with getting my code to work. the part i am having trouble with is to prevent duplicate names being inserted in the vector. this is where i am having a trouble. i have gotten my program to catch when you enter a duplicate but when you are entering a name after entering a first one it does not read it. for instance when i start it, it begins with "Enter the names, terminate with -1: " then i am able to enter a name then it goes "Enter another name, end with -1: " and if you enter a name it just goes down a space and you have to enter the name again for it to register. can someone point me in the general direction of my problem? thanks.

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
#include <iostream>
#include <iomanip>    
#include <string>
#include <vector>

#include "Person.h"

using namespace std;

main()
{
    string name;
    vector<Person *> people; 
    Person * person_pointer;

    cout << "Enter the names, terminate with -1: ";
    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 << "Enter another name, end with -1: "; 
        cin >> name;
	} else 
	cout << name << " " << "is a duplicate, enter another name" <<endl;
	cin >> name;
    }

    int i, j;

    
    for (i = 0; i < people.size(); i++) {
        cout << "Who is " << people[i]->get_name() << "'s best friend? ";
        cin >> name;

        
        for (j = 0; j < people.size(); j++)
            if (people[j]->get_name() == name)
                break;
        if (j < people.size()) 
            people[i]->set_best_friend(people[j]);
        else
            cout << "Couldn't find best friend " << name << endl;
    }

    
    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;
    }


    for (i = 0; i < people.size(); i++)
        delete people[i];
}
If you want line 30 to be part of the else part you'll have to use { }.
LOL i always miss the smallest of things. thanks man. much appreciated.
Topic archived. No new replies allowed.