Design and implement a C++ program to maintain a list of contacts. Requirements:
ID: 654387 • Letter: D
Question
Design and implement a C++ program to maintain a list of contacts.
Requirements:
1. Contacts have a name, a phone number, and an email address. A contact is uniquely identified by name.
2. Have options for the user to:
a. Enter a contact, by entering the name, phone number, and email address. If the name is already in the list, display an error message.
b. Remove a contact, by entering the name. If the name is not in the list, display an error message.
c. Find a contact, by entering the name. If found, display the contact phone number and email address, otherwise display an error message.
d. List all contacts (name, phone number, and email address), in order by name. 3. Use console input-output.
4. Create a class to hold the contact list, with interface methods for each of the above options.
a. Use a linked-list of structures.
b. Maintain the list in order by name.
c. Use sequential search to search for the contacts.
Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//STRUCTURE FOR PHONE BOOK ENTRIES
typedef struct phone {
char fname[10];
char lname[10];
char addr[30];
char phonum[15];
char email[30];
}emp;
//INITIALIZING FUNCTIONS
void add(emp *, int);
void print(emp *, int);
void remove(emp *, int);
void call(emp *, int);
//MAIN FUNCTION
main()
{
int e; //amount of entries in phone book
int sel=0; // user input
emp emp1[20]={''};
printf("How many entries do you want in your phone book? ");
scanf("%d", &e);
fflush(stdin);
while(sel!=5)
{
printf(" Select what you want to do to the phone book: "); //shows user different options and allows user to enter input
printf("1: Add an entry ");
printf("2: Delete an entry ");
printf("3: Print all entries ");
printf("4: Randomly select a friend to call ");
printf("5: Exit program ");
printf(" Enter number: ");
scanf("%d", &sel);
fflush(stdin);
switch(sel) //switch that executes proper function depending on user input
{
case 1:
add(emp1, e);
break;
case 2:
remove(emp1, e);
break;
case 3:
print(emp1, e);
break;
case 4:
call(emp1, e);
break;
default:
sel=5;
printf(" You are leaving the program! ");
}
}
system("PAUSE");
return 0;
}
//FUNCTION TO ADD ENTRY
void add(emp * emp1, int n)
{
char fname[10]; //declaring variables for this function
char lname[10];
char addr[30];
char phonumm[15];
char email[30];
int i = 0; //test for if element is filled later on in this functin. 0 means no and i means yes.
char t[3]={''}; //established to allow strcmp to compare to a null value
printf(" You are entering a new phone book entry! "); //user assigning value to each variable
printf("Enter first name: ");
scanf("%s", &fname);
fflush(stdin);
printf(" Enter last name: ");
scanf("%s", &lname);
fflush(stdin);
printf(" Enter address: ");
scanf("%s", &addr);
fflush(stdin);
printf(" Enter phone number: ");
scanf("%s", &phonumm);
fflush(stdin);
printf(" Enter email: ");
scanf("%s", &email);
fflush(stdin);
for(int c = 0; c<n && i==0; c++) //goes through whole array
{
if(strcmp(emp1[c].fname, t)!=0) //makes sure it enters data only if element is empty
{
strcpy(emp1[c].fname, fname); //copies user input into necessary part of structure array
strcpy(emp1[c].lname, lname);
strcpy(emp1[c].addr, addr);
strcpy(emp1[c].phonum, phonumm);
strcpy(emp1[c].email, email);
i = 1;
}
}
}
//FUNCTION THAT PRINTS OUT ADDRESSES IN FUNCTIONS
void print(emp * emp1, int n)
{
char t[3]={''}; //established so it can be used to compare each element to 'null'
printf(" You are printing out your phonebook!");
for(int x=0, e=1; x < n; x++) //for loop to print out correct number of entries. goes through whole array.
{
if(strcmp(emp1[x].fname, t)!=NULL) //makes sure it skips over empty arrays
{
printf(" Entry #%d", e);
printf(" First Name: %s", emp1[x].fname);
printf(" Last Name: %s", emp1[x].lname);
printf(" Address: %s", emp1[x].addr);
printf(" Phone Number: %s", emp1[x].phonum);
printf(" E-mail: %s", emp1[x].email);
e=e+1;
}
}
}
//FUNCTION THAT ALLOWS USER TO REMOVE AN ENTRY
void remove(emp * emp1, int n)
{
char out[10]; //initializes variables
char t[3]={''};
int c;
printf(" You can now remove a name from the phonebook!"); //allows user to input name to remove
printf(" Enter the first name of the entry you'd like to remove: ");
scanf("%s", &out);
fflush(stdin);
for(int x=0; x<n; x++) //for loop that goes through all the structures
{
if(strcmp(out, emp1[x].fname)==NULL) //frees up space if user input matches the array
{
strcpy(emp1[x].fname, t);
strcpy(emp1[x].lname, t);
strcpy(emp1[x].addr, t);
strcpy(emp1[x].phonum, t);
strcpy(emp1[x].email, t);
}
}
for(c=0; c<n; c++); //for loop to shrink array
{
if(strcmp(emp1[c].fname, t)==NULL) //if element matches null, replaces element with the next function up
{
strcpy(emp1[c].fname, emp1[c+1].fname);
strcpy(emp1[c].addr, emp1[c+1].addr);
strcpy(emp1[c].phonum, emp1[c+1].phonum);
strcpy(emp1[c].email, emp1[c+1].email);
}
}
}
Edit & Run
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.