Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add additional functionality to your phonebook program from lab# 6. Make it possible for users to: 1) Alphabetically sort the list of entries by name

Add additional functionality to your phonebook program from lab# 6. Make it possible for users to:

1) Alphabetically sort the list of entries by name (first or last).

2) Find a phone number for a given name.

3) Randomly select a friend from the phonebook for you to call.

4) Delete everyone from the phonebook at the same time.

This is my existing code for lab 6, can someone add the additional functions please.

#include #include #include

//Declarations typedef struct entry { char fName[15]; char lName[15]; char phone[15]; }entry;

void addContact(entry*,int*); void delContact(entry*,int*); void showBook(entry*,int*);

//Main Function main() { int selection = 0; int entryNum = 0; entry *phoneBook; phoneBook = (entry*)malloc(sizeof(entry)); do { printf(" Phonebook Menu "); printf("\t(1)Add Contact "); printf("\t(2)Delete Contact "); printf("\t(3)Show Phonebook "); printf("\t(0)Exit "); printf("\tPlease Choose an Option: "); scanf("%d",&selection); switch(selection) { case 1: { //Adding Contact printf("\tNow Adding Contact "); entryNum++; addContact(phoneBook,&entryNum); break; } case 2: { //Deleting Contact printf("\tNow Deleting Contact "); delContact(phoneBook,&entryNum); break; } case 3: { //Showing Phonebook printf("\tNow Showing Phonebook "); printf("\t\tCONTACTS "); showBook(phoneBook,&entryNum); break; } case 0: { //Exit printf("\tThank You, Goodbye!"); free(phoneBook); break; } default: { //Invalid Option printf("\tNot A Valid Option! "); printf("\tPlease Try Again! "); break; } } } while (selection!=0); } void addContact(entry*phoneBook,int * entryNum) { if (*entryNum==0) { phoneBook = (entry*)realloc(phoneBook, (sizeof(entry)*2)); } else if (*entryNum>0) { phoneBook = (entry*)realloc(phoneBook, (sizeof(entry)*(*entryNum))+sizeof(entry)); } if(phoneBook!=NULL) { printf("Entry Number:%d ",*entryNum); printf(" First Name:"); scanf("%s",phoneBook[*entryNum].fName); printf(" Last Name:"); scanf("%s",phoneBook[*entryNum].lName); printf(" Phone Number (Format: xxx-xxxx):"); scanf("%s",phoneBook[*entryNum].phone); } else { printf(" NOT ENOUGH MEMORY!"); } } void delContact(entry*phoneBook,int * entryNum) { int num; int delNum; char tempFName[15]={'\0'}; char tempLName[15]={'\0'}; char tempPhone[15]={'\0'}; int selection=0; printf(" First Name:"); scanf("%s",tempFName); printf(" Last Name:"); scanf("%s",tempLName); for(num=0;num<=*entryNum+1;num++) { if (strcmp(tempFName,phoneBook[num].fName)==0&&strcmp(tempLName,phoneBook[num].lName)==0) { //Deleting contact delNum=num; printf(" Delete Entry Number:%d ",num); printf("First Name:\t%s ",phoneBook[num].fName); printf("Last Name:\t%s ",phoneBook[num].lName); printf("Phone Number:\t%s ",phoneBook[num].phone); printf("\tIS THIS CORRECT? "); printf("\t\t(1)Yes "); printf("\t\t(2)No "); scanf("%d",&selection); switch(selection) { case 1: { //Shifting all entries up after an entry is deleted for(num=delNum+1;num<=*entryNum+1;num++) { strcpy(phoneBook[num-1].fName,phoneBook[num].fName); strcpy(phoneBook[num-1].lName,phoneBook[num].lName); strcpy(phoneBook[num-1].phone,phoneBook[num].phone); } *entryNum=*entryNum-1; printf("\tContact Deleted! "); if (*entryNum!=0) { phoneBook = (entry*)realloc(phoneBook, (sizeof(entry)*(*entryNum+1))); } else if (entryNum>0) { phoneBook = (entry*)realloc(phoneBook, 2*(sizeof(entry))); } break; } case 2: { printf("Deletion Cancelled!"); break; } } break; } else if (num>*entryNum) { printf("No matching entry found!"); } } } //Showing Phonebook void showBook(entry*phoneBook,int* entryNum) { int num; for (num=1;num<=*entryNum;num++) { printf(" Entry Number:\t%d ",num); printf("\tFirst Name:\t%s ",phoneBook[num].fName); printf("\tLast Name:\t%s ",phoneBook[num].lName); printf("\tPhone Number:\t%s ",phoneBook[num].phone); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions