Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//Function prototype declarations
void addentry();
void delentry();
void showphonebook();
void alphabeticalsort();
void find();
void rancontact();
void delall();
typedef struct phonebook //First data structure is built for storing names and phonenumbers; not sure if this method is most efficient but it does seem practical
{
char *FirstName;
char *LastName;
char *number;
} book;
typedef struct delbook //Second data structure for deleting user data
{
char *FirstName;
char *LastName;
} del;
//Pointer and global variable initializations
book *bk;
del *dl;
int count = 0;
int delcount = 0;
int main(void)
{
int menu;
do //do while loop for my menu, always a good preference for one initialized run; helpful when troubleshooting
{
printf("\nPhone Book Application\n");
printf("1) Add Friend\n");
printf("2) Delete Friend\n");
printf("3) Display Phone Book\n");
printf("4) Alphabetically Sort\n");
printf("5) Find Name\n");
printf("6) Call Random Contact\n");
printf("7) Delete Entire Phonebook\n");
printf("8) Exit\n");
printf("\nWhat do you want to do?: ");
scanf("%d", &menu);
switch (menu)
{
case 1:
addentry();
count++;
break;
case 2:
delentry();
break;
case 3:
showphonebook();
break;
case 4:
alphabeticalsort();
break;
case 5:
find();
break;
case 6:
rancontact();
break;
case 7:
delall();
break;
case 8:
printf("\nThank you for using this program! Good Bye!"); //User friendly message for program exit
break;
default: //Thought using default case instead of while loop would be a bit neater from logic used in previous programs
printf("\nPlease enter a valid menu selection.\n");
break;
}
} while (menu != 8); //End of do while loop based of exiting criteria
//Thought I'd try some different recursive logic for reinitializing menu
//Returning memory here for efficiency
free(dl);
free(bk);
dl = NULL;
bk = NULL;
return 0;
}
//Begin Functions
//Function for adding entries into phonebook
void addentry()
{
char *lname;
if (count == 0)
{
bk = (book *) malloc ((count*100) + 100); //allocating memory with arbitrarily large number
}
else
{
bk = (book *) realloc (bk, (count*100) + 100); //reallocation of memory with arbitrarily chosen large number
}
if (bk == NULL) //Statement in the event memory is unavailable for new entries
{
printf("No memory available for allocation\n");
}
else //Here we go for adding an entry!
{
bk[count].FirstName = (char *)malloc(sizeof(char)*100);
bk[count].LastName = (char *)malloc(sizeof(char)*100);
bk[count].number = (char *)malloc(sizeof(char)*100);
//Adding some spacing here for eye relief
printf("First Name: ");
scanf("%s", bk[count].FirstName);
printf("Last Name: ");
scanf("%s", bk[count].LastName);
printf("Phone Number: ");
scanf("%s", bk[count].number);
printf("Record added to the Phone Book\n");
}
}
//Function for deleting user entries in phonebook
void delentry()
{
int i, k;
char *name;
if (delcount == 0)
{
dl = (del *) malloc ((delcount*50) + 50);
}
else
{
dl = (del *) realloc (dl, (delcount*100) + 100); //Potential memory segmentation error here with realloc; requires further investigation
}
if (dl == NULL)
{
printf("Out of memory can not Delete\n");
}
else
{
dl[delcount].FirstName = (char *) malloc(sizeof(char)*50);
dl[delcount].LastName = (char *) malloc(sizeof(char)*50);
printf("First Name: ");
scanf("%s", dl[delcount].FirstName);
printf("Last Name: ");
scanf("%s", dl[delcount].LastName);
}
for (i = 0; i < count; i++)
{
if (bk[i].FirstName == NULL && bk[i].LastName == NULL) continue;
if (strcmp(bk[i].FirstName, dl[delcount].FirstName) == 0 && strcmp(bk[i].LastName, dl[delcount].LastName) == 0)
{
printf("Record has been deleted\n");
bk[i].FirstName = NULL;
bk[i].LastName = NULL;
bk[i].number = NULL;
k = 1;
break;
}
}
if (k != 1)
{
printf("\n Entry not found.\n");
}
delcount++;
}
void showphonebook()
{
int i;
printf("\nPhone Book Entries:\n");
for (i = 0; i < count; i++)
{
if (bk[i].FirstName != NULL && bk[i].LastName != NULL)
{
printf("\n%s %s %s\n", bk[i].FirstName, bk[i].LastName, bk[i].number);
}
}
printf("\n"); //Added for spacing for eye relief between contacts being printed
}
void alphabeticalsort() //Alphabetically sorts contacts by last name just like normal phonebook programs
{
int i, k;
char *fname;
char *t1 = (char*)malloc(sizeof(char)*15);
char *t2 = (char*)malloc(sizeof(char)*15);
char *t3 = (char*)malloc(sizeof(char)*15);
/*I addmitedly had a lot of trouble on the logic in this part and I'm still not confident this is the most efficient method.
I initally realized I was only comparing and reassigning last name strings as opposed to all user data.
My way around that issue was to declare the 3 temporary strings for storage of temporary data during the sort.*/
//I also seem to run into a segmentation error when I run this section after deleting all names from phonebook and readding
for (i = 0; i < count; i++)
{
for (k = i + 1; k < count; k++)
{
if (strcmp(bk[i].LastName,bk[k].LastName) > 0) //My if statement to manually sort user data in alphabetical order by last name
{
strcpy(t1, bk[k].FirstName);
strcpy(t2, bk[k].LastName);
strcpy(t3, bk[k].number);
strcpy(bk[k].FirstName, bk[i].FirstName);
strcpy(bk[k].LastName, bk[i].LastName);
strcpy(bk[k].number,bk[i].number);
strcpy(bk[i].FirstName, t1);
strcpy(bk[i].LastName, t2);
strcpy(bk[i].number,t3);
}
}
}
for (i = 0; i < count; i++)
{
printf("\n%s %s: %s\n", bk[i].FirstName, bk[i].LastName, bk[i].number);
}
}
void find() //function to search all users in phonebook
{
int i;
int k = 0;
char first[50];
char last[50];
printf("Enter the person's first name:\n");
scanf("%s", first);
printf("Enter the person's last name:\n");
scanf("%s", last);
for (i = 0; i < count; i++)
{
if (strcmp(bk[i].FirstName, first) == 0 && strcmp(bk[i].LastName, last) == 0) //If statement for finding the person of interest
{
printf("\n%s %s's number is: %s\n", bk[i].FirstName, bk[i].LastName, bk[i].number);
k = 1;
break;
}
}
if (k != 1) //User friendly syntax here to let them know their person of interest could not be found
{
printf("This person's record does not exist\n");
}
}
void rancontact() //Function to print random contact information
{
srand(time(NULL));
int num;
num = rand()%count;
printf("%s %s %s \n", bk[num].FirstName, bk[num].LastName, bk[num].number);
}
void delall()
{
int i, menu;
printf("Are you sure you want to continue? Your phonebook will be completely deleted if you continue.\n"); //Freindly user syntax here to make sure they don't accidentally delete everything
printf("To confirm you want to continue please press 1 otherwise enter any other number\n");
printf("What is your selection:");
scanf("%d", &menu);
if (menu == 1)
{
for (i = 0; i < count; i++)
{
do { //deleting everything with null assignments
bk[i].FirstName = NULL;
bk[i].LastName = NULL;
bk[i].number = NULL;
break;
} while (i <= count);
}
printf("\nYour phonebook has been completely deleted\n"); //Friendly user message here
}
printf("\nYou will now return to the main menu.\n"); //Friendly user message
}