-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmw_7_1.cpp
98 lines (75 loc) · 1.86 KB
/
hmw_7_1.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Homework 7_1
Arian Owji
604649619
*/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Person {
public:
Person(string aName) {
name = aName;
bestFriend = nullptr;
counter = 0;
}
void setBestFriend(Person* aBestFriend);
void printPerson() const;
string getName() const;
private:
string name;
Person* bestFriend;
int counter;
};
void Person::setBestFriend(Person* aBestFriend) {
bestFriend = aBestFriend;
aBestFriend->counter += 1;
}
void Person::printPerson() const {
if (bestFriend != nullptr) {
std::cout << name << " | " << bestFriend->getName() << " | " << counter << endl;
}
else {
std::cout << name << " | " << "NONE" << " | " << counter << endl;
}
}
string Person::getName() const {
return name;
}
int main() {
char response = 'y';
vector<Person*> people;
while (response == 'y') {
string inputName;
std::cout << "Name: ";
getline(cin, inputName);
Person* newPerson = new Person(inputName);
people.push_back(newPerson);
//delete newPerson; If not commented this line would cause a crash, although I thought we were supposed to delete?
//Is the new class member not stored in the vector already so we are free to delete the memory?
std::cout << endl;
std::cout << "Continue <y/n>? ";
std::cin >> response;
cin.clear();
cin.ignore(1000, '\n');
std::cout << endl;
}
std::cout << "Enter the name of best friends: " << endl << endl;
for (int i = 0; i < people.size(); i++) {
string friendName;
std::cout << "Best friend of " << people[i]->getName() << ": ";
getline(cin, friendName);
for (int j = 0; j < people.size(); j++) {
if (people[j]->getName() == friendName) {
people[i]->setBestFriend(people[j]);
}
}
}
std::cout << endl << "Information:" << endl;
for (int i = 0; i < people.size(); i++) {
people[i]->printPerson();
}
return 0;
}