Code:
/*
Description:
A program utilizing data structures to create, delete, and display phone book entries.
User can add, delete, and print the phone book entries.
*/
#include <stdio.h> // Standard C input & output library
#include <stdlib.h> // mallac, callac, and free functions
#include <string.h> // String functions (string copy, string compare)
typedef struct PhoneBookEntry // Declaration of phone book structure
{
char FirstName[21];
char LastName[21];
int PhoneNum;
} person;
// FUNCTION PROTOTYPES:
void Add (int*, int, person*); // Function to add an entry
void Del (int*, int, person*); // Function to delete an entry
void Print (int*, int, person*); // Function to print the phone book
int main() { // Main driving function
int select=0; // Integer variable for user input of phone book operation
int contact_ctr=0; // Counter to track number of contacts
int i=0; // Index for printing "for" loop
person contacts[50]; // Declare an array of phone book entries
person* pcontacts; // Declare a pointer for array Entries
pcontacts = (person*) calloc(0, sizeof(person)); // Set pointer to allocated/initialized block of memory, size of person struct
if (pcontacts == NULL)
{printf("Out of memory! Aborting...");
return 1;}
else {} // Do nothing!
do { // Print menu to screen, ask user for selection...
printf("\n\nPhone Book Application:\n\n");
printf("1) Add friend\n");
printf("2) Delete friend\n");
printf("3) Show phone book\n");
printf("4) Exit\n");
printf("What do you want to do? ");
scanf("%d", &select); // Store user input in variable "select"
getchar(); // Clear the input buffer
switch (select)
{
case 1: // User wants to add an entry
pcontacts = realloc(pcontacts, contact_ctr * sizeof(person)); // Reallocate memory for additional contact
Add (&contact_ctr, i, contacts);
break;
case 2: // User wants to delete an entry
Del (&contact_ctr, i, contacts);
break;
case 3: // User wants to print the phone book
Print (&contact_ctr, i, contacts);
break;
case 4: // User wants to quit, break the switch and exit to loop
break;
default: // User entered a value other than 1,2,3, or 4.
printf("You entered an invalid selection. Try again.\n\n\n");
break;
} // End switch statement
} // End do loop
while (select!=4);
printf("\nThis phone book will now close. Goodbye!\n");
// Pause program until user inputs any character
getchar();
// Return statement
return 0;
} // End main
void Add (int *contact_ctr, int i, person *contacts)
{
(*contact_ctr)++;
// Need to add realloc failsafe, if NULL
printf("\nFirst name: ");
scanf("%s", &contacts[*contact_ctr-1].FirstName);
printf("Last name: ");
scanf("%s", &contacts[*contact_ctr-1].LastName);
printf("Phone number (7-digit, no dash): ");
scanf("%d", &contacts[*contact_ctr-1].PhoneNum);
printf("Record added to the phone book\n\n");
} // End function Add
void Del (int *contact_ctr, int i, person *contacts)
{
char delTempf[21]; // Temporary string for deletion of an entry - first name
char delTempl[21]; // Temporary string for deletion of any array - last name
char nullStr[21] = {"\0"}; // Null string for use in deleting entries
printf("\nFirst name: ");
scanf("%s", &delTempf);
printf("Last name: ");
scanf("%s", &delTempl);
// compare strings, find entry or return entry not found
for (i=0; i<*contact_ctr; i++)
{
if (strcmp(delTempf, contacts[i].FirstName) == 0)
{
strcpy(contacts[i].FirstName, nullStr);
strcpy(contacts[i].LastName, nullStr);
strcpy(contacts[i].PhoneNum, nullStr);
free(&contacts[i]);
(*contact_ctr)--; // Contact deleted, update the contact total counter
break;
}
else
{
printf("\nEntry not found.\n\n");
}
} // End for loop
printf("Record deleted from the phone book\n\n");
} // End function Del
void Print (int *contact_ctr, int i, person *contacts)
{
char nullStr[21] = {"\0"}; // Null string
if (*contact_ctr > 0) // If there are entries, print them
{
printf("\n\nPhone Book Entries:\n\n");
for(i=0; i<*contact_ctr; i++)
{
if (strcmp(nullStr, contacts[i].FirstName) != 0) // If this index is NOT null, print it
{
printf("%s %s %d\n", contacts[i].FirstName, contacts[i].LastName, contacts[i].PhoneNum);
}
else {} // Do nothing (skip this index because no entry exists)
} // end for loop
}
else // Else the book is empty, tell the user
{
printf("\n\nThe phone book is currently empty.\n\n");
}
} // End function Print