Question
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. Here's my current code: //Colin Mazzella Lab 7 Phonebook// #include #include #include #include struct phonebook { char fname[20]; char mname[20]; char lname[20]; char pnum[20]; }; void addfriend(struct phonebook *pEntries, int count); void deletefriend(struct phonebook *pEntries, int count); void exposephonebook(struct phonebook *pEntries, int count); void friendcall(struct phonebook *pEntries, int i); void sortentries(struct phonebook *pEntries, int count); int namefinder(struct phonebook *pEntries, int count, char *name); int main () { struct phonebook *pEntries; pEntries = (struct phonebook *)malloc(sizeof(struct phonebook)); int choice; srand(time(NULL)); int count = 0, index = 0;; do { printf("Phone Book Application "); printf("1: Add a friend "); printf("2: Delete the friend "); printf("3: Show the phonebook "); printf("4: Delete the phonebook "); printf("5: Call your pal "); printf("6: Leave the chat "); printf("What's your plans at this moment? "); scanf("%d", &choice); switch (choice) { case 1: addfriend(pEntries, count); count++; sortentries(pEntries, count); printf(" "); break; case 2: deletefriend(pEntries, count); count--; printf("Conglaturations, you have deleted someone important in your life! "); printf(" "); break; case 3: exposephonebook(pEntries, count); printf(" "); break; case 4: count = 0; printf("You have deleted everyone important in your life! Have a nice day! "); printf(" "); break; case 5: index = rand() % count; friendcall(pEntries, index); printf(" "); break; case 6: return 0; default: printf("Plug in your desirable option: "); printf(" "); } } while (1); return 0; } void addfriend(struct phonebook *pEntries, int count) { printf("What is your first name? "); scanf("%s", pEntries[count].fname); printf("What is your middle name? "); scanf("%s", pEntries[count].mname); printf("What is your last name? "); scanf("%s", pEntries[count].lname); printf("Plug in your phone number: "); scanf("%s", pEntries[count].pnum); printf("Complete! "); } void deletefriend(struct phonebook *pEntries, int count) { char name[20]; int i, index; printf("Enter the name you want to see no more: "); printf("List the name off: "); scanf("%s", name); index = namefinder(pEntries, count, name); for (i = index; i < count - 1; i++) { pEntries[i] = pEntries[i + 1]; } } void exposephonebook(struct phonebook *pEntries, int count) { int i = 0; printf("phone book entries: "); for(i = 0; i < count; i++); { printf("%s %s %s %s ", pEntries[i].fname, pEntries[i].mname, pEntries[i].lname, pEntries[i].pnum); } } void friendcall(struct phonebook *pEntries, int i) { printf("%s %s %s %s ", pEntries[1].fname, pEntries[i].mname, pEntries[i].lname, pEntries[i].pnum); } void sortentries(struct phonebook *pEntries, int count) { int i, j; char briefname[20]; for (i = 0; i < count - 1; i++) { for (j = i + 1; j < count; j++) { if (strcmp(pEntries[i].fname, pEntries[j].fname) > 0) { strcpy(briefname, pEntries[i].fname); strcpy(pEntries[i].fname, pEntries[j].fname); strcpy(pEntries[j].fname, briefname); } } } } int namefinder(struct phonebook *pEntries, int count, char *name) { int index = -1, i = 0; for(i = 0; i < count; i++) { if (strcmp(pEntries[i].fname, name) == 0) { index = 1; break; } } return index; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started