C++ You will design an online contact list to keep track of names and phone numb
ID: 3865845 • Letter: C
Question
C++
You will design an online contact list to keep track of names and phone numbers.
Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables.
Add the following operations to your program:
Add a new contact. Ask the user to enter the name and up to 3 phone numbers.
Delete a contact by having the user enter the name.
Search the list by name and print the name and phone numbers for that one person.
Print all contacts and phone numbers for each contact.
Create a program to test your class. The program should have a menu like this one.
Add a contact
Delete a contact
Print all contacts
Search for a contact
Exit
Here is how your node will look: (This is going to be a struct) The struct will contain a string, an array of 3 elements and the pointer to the next node.
Name
2145556678
8179992222
2143555555
Phone Numbers
Pointer to Next Node
Test:
Take a screen shot or screen shots testing your program. Enter two contacts, print all contacts, search for a contact, delete a contact, print all contacts again so you can see the deleted contact is gone, search for the deleted contact.
Source Code: waitlist.cpp, waitlist.h and GSerranoLab8.cpp
2145556678
8179992222
2143555555
Explanation / Answer
Given below is the code for the question. Please do rate if it helped. Thank you.
contactlist.h
#ifndef contactlist_h
#define contactlist_h
#include <iostream>
using namespace std;
typedef struct Node
{
string name;
string phones[3];
int numPhones; // the number of phone numbers for this contact
struct Node * next;
}ContactNode;
// a list of contacts stored in alphabetical order of name
class ContactList
{
Node* head;
public:
ContactList();
void addContact(string name, string *phones, int numPhones);
void findContact(string name);
void printAll();
bool deleteContact(string name);
~ContactList();
};
#endif /* contactlist_h */
contactlist.cpp
#include "contactlist.h"
#include <iomanip>
using namespace std;
ContactList::ContactList()
{
head = NULL;
}
void ContactList::addContact(string name, string *phones, int numPhones)
{
ContactNode *c = new ContactNode();
c->name = name;
for(int i = 0; i < numPhones; i++)
c->phones[i] = phones[i];
c->numPhones = numPhones;
c->next = NULL;
if(head == NULL)
head = c;
else
{
ContactNode *curr = head, *prev = NULL;
while(curr != NULL)
{
if(name > curr->name)
{
prev = curr;
curr = curr->next;
}
else
break;
}
if(prev == NULL) //insertion before head node
{
c->next = head;
head = c;
}
else
{
c->next = curr;
prev->next = c;
}
}
}
void ContactList::findContact(string name)
{
ContactNode *curr = head;
while(curr != NULL)
{
if(curr->name == name)
break;
curr = curr->next;
}
if(curr == NULL)
cout << name << " not found." << endl;
else
{
cout << "Found " << name << endl;
cout << " " << curr->name ;
for(int i = 0; i < curr->numPhones; i++)
cout << " " << curr->phones[i];
cout << endl;
}
cout << endl;
}
void ContactList::printAll()
{
ContactNode *curr = head;
cout << setw(20) << left << "Name" << setw(15) << "Phone 1" << setw(15) << "Phone 2" << setw(15) << "Phone 3" << endl;
while(curr != NULL)
{
cout << setw(20) << left << curr->name;
for(int i = 0; i < curr->numPhones; i++)
cout << setw(15) << curr->phones[i];
curr = curr->next;
cout << endl;
}
cout << endl;
}
bool ContactList::deleteContact(string name)
{
ContactNode *curr = head, *prev = NULL;
while(curr != NULL)
{
if(curr->name == name)
break;
prev = curr;
curr = curr->next;
}
if(curr == NULL) //not found
return false;
else
{
if(prev == NULL) //delete head node
head = head->next;
else
prev->next =curr->next;
delete curr;
return true;
}
}
ContactList::~ContactList()
{
ContactNode *curr = head, * next;
while(curr != NULL)
{
next = curr->next;
delete curr;
curr = next;
}
}
contactmain.cpp
#include <iostream>
#include "contactlist.h"
using namespace std;
int main()
{
int choice = 0;
string phones[3], name;
int numPhones;
ContactList list;
while(choice != 5)
{
cout << "1. Add contact" << endl;
cout << "2. Delete contact" << endl;
cout << "3. Print all" << endl;
cout << "4. Search contact" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter name: ";
cin >> name;
cout << "How many phones? " ;
cin >> numPhones;
for(int i = 0; i < numPhones; i++)
{
cout << "Enter Phone " << (i+1) << ": ";
cin >> phones[i];
}
list.addContact(name, phones, numPhones);
cout << "Contact added successfully." << endl << endl;
break;
case 2:
cout <<"Enter name of contact to delete: ";
cin >> name;
if(list.deleteContact(name))
cout << "Deleted contact " << name << endl << endl;
else
cout << "Not found contact " << name << endl << endl;
break;
case 3:
cout << "List of contacts " << endl;
list.printAll();
break;
case 4:
cout <<"Enter name of contact to search: ";
cin >> name;
list.findContact(name);
break;
case 5:
break;
default:
cout << "Invalid choice. " << endl;
}
}
cout << "Thank you for using contact list program." << endl;
return 0;
}
output
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 1
Enter name: John
How many phones? 2
Enter Phone 1: 1111111
Enter Phone 2: 2222222
Contact added successfully.
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 3
List of contacts
Name Phone 1 Phone 2 Phone 3
John 1111111 2222222
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 1
Enter name: Bob
How many phones? 3
Enter Phone 1: 3333333
Enter Phone 2: 4444444
Enter Phone 3: 5555555
Contact added successfully.
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 3
List of contacts
Name Phone 1 Phone 2 Phone 3
Bob 3333333 4444444 5555555
John 1111111 2222222
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 1
Enter name: Michael
How many phones? 1
Enter Phone 1: 6666666
Contact added successfully.
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 3
List of contacts
Name Phone 1 Phone 2 Phone 3
Bob 3333333 4444444 5555555
John 1111111 2222222
Michael 6666666
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 4
Enter name of contact to search: Alice
Alice not found.
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 4
Enter name of contact to search: Bob
Found Bob
Bob 3333333 4444444 5555555
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 4
Enter name of contact to search: Michael
Found Michael
Michael 6666666
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 1
Enter name: Alice
How many phones? 2
Enter Phone 1: 7777777
Enter Phone 2: 8888888
Contact added successfully.
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 3
List of contacts
Name Phone 1 Phone 2 Phone 3
Alice 7777777 8888888
Bob 3333333 4444444 5555555
John 1111111 2222222
Michael 6666666
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 2
Enter name of contact to delete: Jane
Not found contact Jane
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 2
Enter name of contact to delete: John
Deleted contact John
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 3
List of contacts
Name Phone 1 Phone 2 Phone 3
Alice 7777777 8888888
Bob 3333333 4444444 5555555
Michael 6666666
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 4
Enter name of contact to search: John
John not found.
1. Add contact
2. Delete contact
3. Print all
4. Search contact
5. Exit
Enter your choice: 5
Thank you for using contact list program.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.