Write an application in C++ to maintain a list of friends. Write the program int
ID: 3906250 • Letter: W
Question
Write an application in C++ to maintain a list of friends.
Write the program into 3 files, main, header and cpp.
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
Explanation / Answer
//Friends.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Friend
{
string firstName, lastName, cellPhone;
public:
Friend();
void set_fname(string fname);
void set_lname(string lname);
void set_cellPh(string ph);
string get_fname() const;
string get_lname() const;
string get_cellPh() const;
void display();
};
-------------------------------------
//Friends.cpp
#include"Friend.h"
Friend::Friend()
{
firstName = "";
lastName = "";
cellPhone = "";
}
void Friend::set_fname(string fname)
{
firstName = fname;
}
void Friend::set_lname(string lname)
{
lastName = lname;
}
void Friend::set_cellPh(string ph)
{
cellPhone = ph;
}
string Friend::get_fname() const
{
return firstName;
}
string Friend::get_lname() const
{
return lastName;
}
string Friend::get_cellPh() const
{
return cellPhone;
}
void Friend::display()
{
cout << firstName << " " << lastName << " " << cellPhone << endl;
}
----------------------------------
//Main.cpp
#include"Friend.h"
//Set MAX number of friends 100
#define MAX 100
int main()
{
//declare array of Friend objects of size MAX
Friend friends[MAX];
int choice,count=0;
string fname, lname, ph;
do
{
cout << "1. Add a Friend 2. Remove a Friend 4. Display all Friends 4.Exit ";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter first name: ";
cin >> fname;
cout << "Enter last name: ";
cin >> lname;
cout << "Enter cell phone number: ";
cin >> ph;
friends[count].set_fname(fname);
friends[count].set_lname(lname);
friends[count].set_cellPh(ph);
count++;
break;
case 2:
int index;
cout << "Enter the first name of a friend to remove from friends list: ";
cin >> fname;
cout << "Enter the last name of a friend to remove from friends list: ";
cin >> lname;
//search and get index of the friend in friends array
for (int i = 0; i < count; i++)
{
if (fname == friends[i].get_fname())
{
if (lname == friends[i].get_lname())
{
index = i;
break;
}
else
{
cout << "There is no match with last name" << endl;
}
}
}
//now move the array elements to right at index where friend found
for (int i = index; i < count-1; i++)
{
friends[i].set_fname(friends[i + 1].get_fname());
friends[i].set_lname(friends[i + 1].get_lname());
friends[i].set_cellPh (friends[i + 1].get_cellPh());
}
//reduce count of friends in array after removing a friend
--count;
break;
case 3:
for (int i = 0; i < count; i++)
{
friends[i].display();
}
break;
case 4:
cout << "Exiting..." << endl;
default:
cout << "Invalid choice" << endl;
}
} while (choice != 4);
}
/*output: tested all the conditions
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 1
Enter first name: Mary
Enter last name: Ral
Enter cell phone number: 8976543987
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 1
Enter first name: James
Enter last name: Smith
Enter cell phone number: 6784532108
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 1
Enter first name: Siman
Enter last name: Bond
Enter cell phone number: 5634219087
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 1
Enter first name: Jona
Enter last name: Johns
Enter cell phone number: 1087965434
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 3
Mary Ral 8976543987
James Smith 6784532108
Siman Bond 5634219087
Jona Johns 1087965434
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 2
Enter the first name of a friend to remove from friends list: James
Enter the last name of a friend to remove from friends list: Smith
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 3
Mary Ral 8976543987
Siman Bond 5634219087
Jona Johns 1087965434
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 2
Enter the first name of a friend to remove from friends list: Mary
Enter the last name of a friend to remove from friends list: Ral
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 3
Siman Bond 5634219087
Jona Johns 1087965434
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 2
Enter the first name of a friend to remove from friends list: Jona
Enter the last name of a friend to remove from friends list: Johns
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 3
Siman Bond 5634219087
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 1
Enter first name: Mary
Enter last name: Ral
Enter cell phone number: 9875643219
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 3
Siman Bond 5634219087
Mary Ral 9875643219
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 5
Invalid choice
1. Add a Friend
2. Remove a Friend
4. Display all Friends
4.Exit
Enter your choice: 4
Exiting...
Invalid choice
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.