Instructions
Objective
Write a C++ assignment program to create friends finding system.
Requirements and Specifications
Source Code
FRIEND FINDER
#include "FriendFinder.h"
FriendFinder::FriendFinder()
{
// default cosntructor
}
void FriendFinder::GetDetails()
{
string first_name, last_name;
int age, id;
cout << "What is your first name? ";
getline(cin, first_name);
cout << "What is your last name? ";
getline(cin, last_name);
bool valid_input = false;
// user a while loop here in case the user enters an invalid value
while (!valid_input)
{
cout << "What is your age? ";
cin >> age;
if (age > 0)
valid_input = true;
}
cin.ignore();
valid_input = false;
// user a while loop here in case the user enters an invalid value
while (!valid_input)
{
cout << "What is your ID? ";
cin >> id;
if (id > 0)
valid_input = true;
}
cin.ignore();
// Now, update person
m_me.SetDetails(first_name, last_name, age, id);
}
void FriendFinder::DisplayMenu(int &choice)
{
bool valid_input = false;
// use a while loop so program keeps asking to user until he/she enters a valid value
while (!valid_input)
{
// Function to display main menu
cout << "1. Display Entire Organization" << endl;
cout << "2. Display Friend List" << endl;
cout << "3. Search for a Friend" << endl;
cout << "4. Remove Friend" << endl;
cout << "5. Exit" << endl;
cin >> choice;
if (choice >= 1 && choice <= 5)
valid_input = true;
}
}
void FriendFinder::Start()
{
// First, create the person
GetDetails();
// build orgaization
m_organization.SetName();
m_organization.LoadRoster();
int choice;
bool running = true; // true until player enters the "Exit" option
cout << "************************" << endl;
cout << " Welcome to " << m_organization.GetName() << " Friend Finder" << endl;
cout << "************************" << endl;
cout << endl;
int user_input;
while (running)
{
DisplayMenu(choice);
if (choice == 1) // Display Entire Organization
{
m_me.DisplayDetails();
cout << endl;
}
else if (choice == 2) // Display Friend List
{
m_me.DisplayFriends();
}
else if (choice == 3) // search for a friend
{
m_organization.DisplayRoster();
Person* person = m_organization.GetPerson();
if (person != NULL)
{
m_me.AddFriend(person);
}
}
else if (choice == 4) // Remove Friend
{
// First, display friends
m_me.DisplayFriends();
if (m_me.GetFriendsCount() > 0)
{
bool valid_input = false;
while (!valid_input)
{
cout << "Who would you like to remove? ";
cin >> user_input;
if (user_input >= 1 && user_input <= m_me.GetFriendsCount())
{
// Get Friend
Person* p = m_me.GetFriends()[user_input];
m_me.RemoveFriend(p);
}
else
cout << "There is no person in your Friends List with that index." << endl;
}
}
}
else if (choice == 5)
running = false;
}
// program ended. Show farewell message
cout << "Thank you for using " << m_organization.GetName() << " Friend Finder!" << endl;
}
ORGANIZATION
#include "Organization.h"
Organization::Organization()
{
// default constructor
m_numRoster = 0;
}
void Organization::SetName()
{
// ask user for organization name
cout << "What is the name of your organization? ";
getline(cin, m_name);
}
void Organization::LoadRoster()
{
// ask user for file name
cout << "Please enter file name: ";
getline(cin, m_fileName);
// Now, read file
// First, read the input file
ifstream input_file;
input_file.open(m_fileName, ios::in);
// get data
string line;
size_t found;
string first_name, last_name;
int age, ID;
while (getline(input_file, line))
{
//cout << line << endl;
// split line so we get each field
// each line has only 4 fields
found = line.find(",");
first_name = line.substr(0, found);
line = line.substr(found + 1, line.length());
found = line.find(",");
last_name = line.substr(0, found);
line = line.substr(found + 1, line.length());
found = line.find(",");
age = stoi(line.substr(0, found));
line = line.substr(found + 1, line.length());
ID = stoi(line);
// Build person
Person p(first_name, last_name, age, ID);
// add person to roster
m_roster[m_numRoster] = p;
m_numRoster++;
}
input_file.close();
cout << m_numRoster << " people loaded into the roster" << endl;
}
void Organization::DisplayRoster()
{
// check if there is people in roster
cout << "*****" << GetName() << "*****" << endl;
if (m_numRoster > 0)
{
// display all people in roster
int count = 1;
for (int i = 0; i < m_numRoster; i++)
{
cout << count << ". " << m_roster[i] << endl;
count++;
}
}
else
cout << "There is no people in the organization." << endl;
}
Person* Organization::GetPerson()
{
DisplayRoster();
// check if m_roster is populated
if (m_numRoster > 0)
{
// now, ask number of person
int index;
cout << "Who would you like to friend?" << endl;
while (true)
{
cin >> index;
if (index >= 1 && index <= m_numRoster) // valid
{
Person* p = (m_roster + index-1);
return p;
}
}
}
else
return NULL; // return null
}
PERSON
#include "Person.h"
Person::Person()
{
// constructor without parameters
m_friendCount = 0;
}
Person::Person(string first_name, string last_name, int age, int ID)
{
m_friendCount = 0;
m_fName = first_name;
m_lName = last_name;
m_age = age;
m_ID = ID;
}
void Person::AddFriend(Person* person)
{
m_friends[m_friendCount] = person;
m_friendCount++;
cout << "You are now friends with " << person->m_fName << "!" << endl;
}
void Person::RemoveFriend(Person* person)
{
// First, iterate through array to check if the person exists
for (int i = 0; i < m_friendCount; i++)
{
Person *p = m_friends[i];
if (p->m_ID == person->m_ID)
{
// Iterate from element i to the end and move the remaining elements in the array to the left
for (int j = i + 1; j < m_friendCount; j++)
{
m_friends[i] = m_friends[j];
}
// remove the last element from the array
m_friendCount--;
break;
}
}
}
bool Person::CheckID(int id)
{
for (int i = 0; i < m_friendCount; i++)
{
if (m_friends[i]->m_ID== id)
{
return true;
}
}
return false;
}
void Person::DisplayFriends()
{
if (m_friendCount < 1)
cout << "You don't have any friends yet" << endl;
else if (m_friendCount <= 10)
{
cout << "Friend List for " << m_fName << endl;
int count = 1;
for (int i = 0; i < m_friendCount; i++)
{
Person p =*m_friends[i];
cout << count << ". " << p << endl;
count++;
}
}
else if (m_friendCount > 10) // display only the first 4 and last 4
{
cout << "Friend List for " << m_fName << endl;
int count = 1;
for (int i = 0; i < 4; i++)
{
Person p = *m_friends[i];
cout << count << ". " << p << endl;
count++;
}
cout << ".\n.\n.\n";
count = m_friendCount - 4;
for (int i = m_friendCount - 4; i < m_friendCount; i++)
{
Person p = *m_friends[i];
cout << count << ". " << p << endl;
count++;
}
}
}
void Person::DisplayDetails()
{
cout << "You are: " << m_fName << " " << m_lName << " (" << m_age << " yrs) " << m_ID << endl;
}
void Person::SetDetails(string first_name, string last_name, int age, int id)
{
m_fName = first_name;
m_lName = last_name;
m_age = age;
m_ID = id;
}
Related Samples
On ProgrammingHomeworkHelp.com, students can access a variety of related samples for C++ assignments. Our website offers expertly crafted examples that cover diverse C++ topics, helping students understand key concepts and coding techniques. Whether you need help with object-oriented programming, data structures, or algorithms, our C++ samples provide practical insights and solutions to guide you through your assignments. We are dedicated to supporting students with high-quality, tailored assistance to ensure academic success in their C++ coursework.
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++