Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the program to prevent duplicate names being inserted in the vector. Modi

ID: 3719282 • Letter: M

Question

Modify the program to prevent duplicate names being inserted in the vector.

Modify the program to prompt the user for another name if best friend is not found.

Person.h

#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

Person.cpp


#include "Person.h"

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

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

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

void Person::set_best_friend(Person * bf)
{
    best_friend = bf;
    (bf->popularity)++;
}

Main.cpp

#include <iostream>

#include <iomanip>    // for setw

#include <fstream>    // for ifstream

#include <string>

#include <vector>

#include "Person.h"

using namespace std;

main()

{

    string name;

    vector<Person *> people;

    Person * person_pointer;

    ifstream in;

    in.open("names");

    if (!in.is_open()) {

       cout<<"Couldn't open 'names' file. ";

       return 1;

    }

    in>>name;

    while (!in.eof()) {

       person_pointer = new Person(name);

       people.push_back(person_pointer);

       in>>name;

    }

    in.close();

    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++)

       delete people[i];

}

Explanation / Answer

only main() is modified so pasting only main code for both functionality.

main()
{
    string name;

    vector<Person *> people;

    Person * person_pointer;

    ifstream in;

    in.open("names");

    if (!in.is_open()) {
       cout<<"Couldn't open 'names' file. ";
       return 1;
    }

    in>>name;

    while (!in.eof()) {

        bool alreadyInList = false; //flag to set when entry found in vector

        for(int i=0; i< people.size(); i++) {
            if (people[i]->get_name() == name) {
                alreadyInList = true; //entry found in vector so its a duplicate entry skip this value
            }
        }
        if (alreadyInList) {
            cout << name << " already exist so skipping... " << endl;
        }
        else {
            person_pointer = new Person(name);
            people.push_back(person_pointer);
        }
        in>>name;
    }

    in.close();

    int i, j;

    for(i=0; i< people.size(); i++) {

        bool friendFoundInList = false; //flag to see if frient is already in vector or not

        while(!friendFoundInList) { //loop till friend is not found in vector
            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]);
                friendFoundInList = true; //friend foud in vector exit from loop, no need to ask new name again
            }
            else
                cout<< "Couldn't find best friend " << name << ". Please Re-enter name.. " << endl;
        }
    }

    for (i=0; i<people.size(); i++)
       delete people[i];
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote