Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
typedef struct pbEntry
{
char fName[25];
char lName[25];
char pNumber[25];
} person;
void addFriend (int*, int, person*); // Add function prototype
void deleteFriend (int*, int, person*); // Delete function prototype
void printBook (int*, int, person*); // Print function prototype
int compare (const void *a, const void *b); // Compare function prototype
void findFriend (int*, int, person*); // Find function prototype
void deleteAll (int*, int, person*); // Delete function prototype
main()
{
char lName[25], fName[25], pNumber[25];
int iResponse = 0;
int i;
int counter;
person contacts[50];
person* pContacts;
pContacts = (person*) calloc(0, sizeof(person));
srand (time (NULL));
int ilName;
ilName = rand () % counter;
do {
printf ("\n\t\t\tPhone Book\n");
printf ("\n\t1)\tAdd friend\n");
printf ("\n\t2)\tDelete friend\n");
printf ("\n\t3)\tShow phone book\n");
printf ("\n\t4)\tSort entries alphabetically\n");
printf ("\n\t5)\tFind friend\n");
printf ("\n\t6)\tRandom friend\n");
printf ("\n\t7)\tDelete all entries\n");
printf ("\n\t8)\tExit\n");
printf ("\n\n\tPlease, enter your choice (1 - 8): \n");
scanf ("%d", &iResponse);
switch (iResponse) {
case 1:
pContacts = realloc (pContacts, counter * sizeof (person));
addFriend (&counter, i, contacts);
break;
case 2:
deleteFriend (&counter, i, contacts);
break;
case 3:
printBook (&counter, i, contacts);
break;
case 4:
qsort (contacts, counter, sizeof (person), compare);
for(i = 0; i < counter; ++i) {
printf("\n\t%s %s %s\n", contacts[i].lName, contacts[i].fName, contacts[i].pNumber);
} //end for loop
break;
case 5:
findFriend (&counter, i, contacts);
break;
case 6:
printf("\n\t%s %s %s\n", contacts[ilName].lName, contacts[ilName].fName, contacts[ilName].pNumber);
break;
case 7:
deleteAll (&counter, i, contacts);
break;
case 8:
system ("CLS");
printf ("\n\n\t\tGood bye!\n");
getch ();
break;
default:
printf ("\n\tInvalid choice.\n");
getch ();
break;
} // end switch
} // end loop
while (iResponse != 8);
} // end main
/*Add entry function*/
void addFriend (int *counter, int i, person *contacts) // function defenition
{
(*counter)++;
printf ("\n\tPlease, enter the last name: \n");
scanf("%s", contacts [*counter - 1].lName);
printf ("\n\tPlease, enter the first name: \n");
scanf("%s", contacts [*counter - 1].fName);
printf ("\n\tPlease, enter the phone number: \n");
scanf("%s", contacts [*counter - 1].pNumber);
printf("\n\t%s %s was added to the phone book.\n\n", contacts [*counter - 1].lName, contacts [*counter - 1].fName);
} // end function
/*Delete entry function*/
void deleteFriend (int *counter, int i, person *contacts) // function definition
{
char lNameDel[25];
char fNameDel[25];
char nullString[25] = {"\0"};
printf ("\n\tPlease, enter the last name: \n");
scanf("%s", lNameDel);
printf ("\n\tPlease, enter the first name: \n");
scanf("%s", fNameDel);
for (i = 0; i < *counter; i++)
if (strcmp(lNameDel, contacts[i].lName) == 0) break;
if (i != *counter) {
strcpy(contacts[i].lName, nullString);
strcpy(contacts[i].fName, nullString);
strcpy(contacts[i].pNumber, nullString);
printf("\n\t%s %s record has been deleted from the phone book.\n", lNameDel, fNameDel);
} // end if
else
printf ("\n\t%s %s record does not exist in the Phone Book.\n", lNameDel, fNameDel);
getch ();
} // end function
/*Print Book function*/
void printBook (int *counter, int i, person *contacts) // function definition
{
char nullString [25] = {"\0"};
if (*counter > 0) {
printf("\n\tPhone Book entries:\n\n");
for(i = 0; i<*counter; i++) {
if (strcmp(nullString, contacts[i].fName) != 0) {
printf("%s %s %s\n", contacts[i].lName, contacts[i].fName, contacts[i].pNumber);
} // end if
} // end for loop
} // end if
else {
printf("\n\tThe phone book is empty.\n\n");
} // end else
} // end function
/*Compare function for qsort*/
int compare (const void *a, const void *b) // function definition
{
person *ia = (person *)a;
person *ib = (person *)b;
return strcmp (ia -> lName, ib -> lName);
} // end function
/*Find entry function*/
void findFriend (int *counter, int i, person *contacts) // function definition
{
char lName[25];
char fName[25];
printf ("\n\tPlease, enter the last name: \n");
scanf("%s", lName);
printf ("\n\tPlease, enter the first name: \n");
scanf("%s", fName);
for (i = 0; i < *counter; i++)
if (strcmp(lName, contacts[i].lName) == 0 && strcmp(fName, contacts[i].fName) == 0) break;
if (i != *counter)
printf ("\n\t%s %s phone number is %s\n", lName, fName, contacts[i].pNumber);
else
printf ("\n\t%s %s record does not exist in the Phone Book.\n", lName, fName);
getch ();
} // end function
/*Delete all function*/
void deleteAll (int *counter, int i, person *contacts) // function definition
{
char nullString[25] = {"\0"};
for (i = 0; i < *counter; i++) {
if (strlen(contacts[i].lName) != 0) {
strcpy(contacts[i].lName, nullString);
strcpy(contacts[i].fName, nullString);
strcpy(contacts[i].pNumber, nullString);
} // end if
} // end loop
printf("\n\tThe Phone Book is empty.\n");
} // end function