I am trying to create a phonebook program that allows me to add contacts, remove contacts, and displays the whole phonebook, but I added in some debugging printf statements to see what was going wrong and I saw that my variable for bookSize was never increasing.
I am unsure how I can fix it. I can create contacts but each new one will replace the old one.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct //Contact structure
{
char fname[20], lname[20];
int phoneNum;
} contact;
int addContact(contact*, contact, int*); //Add a contact
int removeContact(contact*, contact, int*); //Remove a contact
void showBook(contact*, int*); //Show the phonebook
int main()
{
//Variables
int menu = 0, phoneNum, userIn;
contact *friends = malloc(1 * sizeof(contact));
int bookSize = 1;
char fname[20], lname[20];
do
{
printf("-=-=-=-=-=-=-=-=-=-=Digital Phonebook=-=-=-=-=-=-=-=-=-=-\n");
printf("Menu: \n");
printf("1: Add Contact\n");
printf("2: Remove Contact\n");
printf("3: Show Contact List\n");
printf("4: Exit Menu\n");
scanf("%d", &userIn); //Menu select
if (userIn == 1) //Add Contact
{
contact newContact;
printf("Enter the first name: ");
scanf("%s", &newContact.fname);
printf("Enter the last name: ");
scanf("%s", &newContact.lname);
printf("Enter the phone number: ");
scanf("%d", &newContact.phoneNum);
addContact(friends, newContact, &bookSize);
}
else if (userIn == 2) //Remove contact
{
contact oldContact;
printf("First name of the contact you wish to delete: ");
scanf("%s", &oldContact.fname);
printf("Enter the last name: ");
scanf("%s", &oldContact.lname);
removeContact(friends, oldContact, &bookSize);
}
else if (userIn == 3) //Show phone book
{
showBook(friends, &bookSize);
}
else if (userIn == 4) //Exit menu
{
menu = 1;
break;
}
} while (menu == 0);
return 0;
}
int addContact(contact *friends, contact newContact, int *bookSize)
{
contact *temp;
int sizeOfBook = *bookSize;
printf("\n%d\n", *bookSize); //Debug
*bookSize++;
temp = realloc(friends, sizeOfBook * sizeof(friends));
if (temp == NULL)
printf("Allocation failed: Out of memory.\n");
else
{
friends = temp;
strcpy(friends[sizeOfBook-1].fname, newContact.fname);
strcpy(friends[sizeOfBook-1].lname, newContact.lname);
friends[sizeOfBook-1].phoneNum = newContact.phoneNum;
}
printf("\n%d\n", *bookSize); //Debug
}
int removeContact(contact *friends, contact oldContact, int *bookSize)
{
int i=0;
int temp = bookSize;
char fname[20],lname[20];
if(bookSize==0)
{
printf("List is empty\n\n");
return 0;
}
while(i<temp)
{
if(strcmp(friends[i].fname,fname) == 0 && strcmp(friends[i].lname,lname) == 0)
{
int y = i;
if(temp == 1)
temp = 0;
while(y < temp - 1)
{
friends[y] = friends[y + 1];
y++;
}
temp--;
printf("Record deleted from the phone book\n\n");
*bookSize--;
break;
}
i++;
}
if(i == temp)
printf("Record Not In the List\n\n");
return 0;
}
void showBook(contact *friends, int *bookSize)
{
int i=0;
printf("\n%d\n", *bookSize); //Debug
while(i < *bookSize)
{
printf("%d) %s %s %d \n\n", i+1, friends[i].fname, friends[i].lname, friends[i].phoneNum);
i++;
}
}