Write an application to maintain a list of friends. I need assistance with my co
ID: 675469 • Letter: W
Question
Write an application to maintain a list of friends.
I need assistance with my code since I'm a little lost as to how I should correctly write my code.
NOTE: PLEASE USE C++ CODE ONLY.
Specifications
Create a simple Friend class with, as a minimum, the following:
firstName, lastName and cellPhone fields
appropriate constructors
get/set functions
display() function - prints a friend's name and cell phone number
Create an array of Friends.
Write a program to manage your list of friends.
Run the program from a menu with the following options:
Add a Friend
Remove a Friend
Display all Friends
Exit
#include <iostream>
using namespace std;
class Friend {
public:
Friend();
Friend(string, string, string);
string GetfirstName()
{
return firstName;
}
void SetfirstName(string val)
{
firstName = val;
}
string GetLastName()
{
return lastName;
}
void setlastName(string val )
{
lastName = val;
}
};
Friend::Friend(): firstName(""), lastName(""), cellPhone("")
{
}
Friend::Friend(string fn, string ln, string cp): firstName(fn), lastName(ln), cellPhone(cp)
{
}
void Friend::display()
{
cout << firstName <<
<< lastName <<
<< cellPhone << endl;
}
const size_t SZ = 10;
void mAddFriend(array < Friend, SZ>&, int&);
void mRemoveFriend(array < Friend, SZ>&, int&);
void mDisplayFriends(const array<Friend, SZ>&, int&);
void sortMyFriendsList(array<Friend, SZ>&, int&);
int main()
{
int num = 0;
array<Friend, SZ> myFriends = {};
makeSomeFriend(myFriends, num):
cout << "num = " << num << endl;
char selection;
do{
system("cls");
cout << "Welcome to Friend List Manager "
<< "1) Add a Friend "
<< "2) "
switch(selection)
}
return 0;
}
void mAddFriend()
{
string firstName, lastName, cellPhone;
system("cls");
cout << "Add a Friend "
cout << "First Name: ";
cin >> firstName;
cout << "Last name: ";
cin >> lastName;
cout << "Cell Phone: ";
cin >> cellPhone;
friends[n++]= Friend(firstName, lastName, cellPhone);
}
void mRemoveFriend()
{
int lineNumber;
system("cls");
cout << "Remove a Friend: ";
mDisplayFriends(friends, n);
cout << "Enter line number of friend to remove: ";
cin >> lineNumber;
for(int i = lineNumber-1; i < n-1; i++)
{
friends[i] = friends[i+1];
}
n--;
cout << endl;
mDisplayFriends(friends , n);
system("PAUSE");
}
void mDisplayFriends(array<Friend SZ>&, int&)
{
sortMyFriendsList(friends, n);
for(int i = 0; i < n; i++)
{
cout << i + 1 << "";
friends[i].display();
}
cout << endl;
}
void makeSomeFriends(const array<Friend, SZ> &friends, int&n)
{
friends[n++]= Friend("Jack", "Smith", "111-111-111");
friends[n++] = Friend("Jill", "Jones", "222-222-2222");
friends[n++] = Friend(""
}
void sortMyFriendsList(const array<Friend, SZ > &friends, int&n)
{
int minIndex;
Friend otherFriend;
for(int i = 0; i<n-1; i++)
{ minIndex = i;
otherFriend = friends[minIndex];
for(int j = i+1; j < n; j++ )
{
if(friends[j].GetlastName()<otherFriend.GetlastName())
{
minIndex = j;
otherFriend = friends[j];
}
}
friends[minIndex] = friends[i];
friends[j] = otherFriend;
}
}
Explanation / Answer
#include <iostream>
#include<conio.h>
using namespace std;
class friend1
{
public:
string firstName;
string lastName;
int cellPhone;
friend1()
{
firstName="";
lastName="";
cellPhone=0;
}
friend1(string f, string l, int p)
{
firstName=f;
lastName=l;
cellPhone=p;
}
string GetfirstName()
{
return firstName;
}
void SetfirstName(string val)
{
firstName = val;
}
string GetLastName()
{
return lastName;
}
void setlastName(string val )
{
lastName = val;
}
void display()
{
cout << firstName << ": " << lastName << ": " << cellPhone << endl;
}
};
int main()
{
int choice, p=0;
char more;
friend1 f[20];
cout<< " 1. Add a friend" << endl;
cout<< " 2. Remove a friend" << endl;
cout<< " 3. Display all friends" << endl;
cout<< " 4. Exit" << endl;
cout<< " Enter your choice" << endl;
cin>>choice;
if(choice==1)
{
do
{
cout<<"Enter the first name :";
cin>>f[p].firstName;
cout<<"Enter the last name :";
cin>>f[p].lastName;
cout<<"Enter the phone number :";
cin>>f[p].cellPhone;
p++;
cout<<"Do you want to add more friends";
cin>>more;
}
while(more=='y');
}
else if (choice==3)
{
for(int i=0; i<p;i++)
{
f[i].display();
}
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.