I've been messing with this program for days now and I just can't get the features to work properly. When I try to delete entries it locks up and though I can add one person, when I attempt to add a second it locks up. I'm thinking the problem is related to memory and/or pointers, though I'm really not sure. I'd appreciate it if anyone could help me troubleshoot this.
This was written in Bloodshed and the program requirements are:
*User can enter as many entries as desired
*Use an array of structures to hold contact info and create functions to add, delete and print entries.
*Do not display invalid or NULL entries; you can assume all people have unique names.
*Allocate and free memory as necessary
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Function prototypes
void Add();
void Delete();
void Display();
//Structure for Phonebook list
typedef struct PhoneBookList {
const char *FirstName;
const char *LastName;
const char *PhoneNumber;
} list;
//Structure for delete
typedef struct DeleteEntry {
const char *FirstName;
const char *LastName;
} take;
//Pointers to structures
list *lt;
take *tk ;
//Global variables
int count = 0;
int delCount = 0;
main()
{
//Variable declarations
int iSelection;
do { //Print menu to screen, ask user for selection
printf("\n\nPhone Book:\n\n");
printf("1) Add friend\n");
printf("2) Delete friend\n");
printf("3) Display phone book\n");
printf("4) Exit\n");
printf("What do you want to do? ");
scanf("%d", &iSelection);
switch (iSelection) {
case 1: //Add entry
Add();
break;
case 2: //Delete entry
Delete();
break;
case 3: //Display all entries
Display();
break;
case 4: //Break loop and close program
break;
default: //User entered an invalid number
printf("\nYou entered an invalid selection. Try again.\n\n\n");
system("pause");
} //End Switch
} while (iSelection != 4); //End Do While loop
//Return used memory
tk = NULL;
lt = NULL;
free(tk);
free(lt);
} //End Main
//Function definitions
//Add an entry
void Add()
{
if (count == 0)
{
lt = (list *) malloc ((count*200) + 30);
}
else
{
lt = (list *) realloc (lt, (count*1) + 1);
}
if (lt == NULL)
{
printf("You cannot add more memory");
}
else
{
lt[count].FirstName;
lt[count].LastName;
lt[count].PhoneNumber = (char*) malloc(sizeof(char)*200);
printf("\nEnter their First Name: ");
scanf("%s", lt[count].FirstName);
printf("\nEnter their Last Name: ");
scanf("%s", lt[count].LastName);
printf("\nEnter their Phone Number (no spaces or dashes): ");
scanf("%d", <[count].PhoneNumber);
printf("\nContact added");
}
count++;
system("pause");
}
//Delete an entry
void Delete()
{
int i;
int q = 0;
char *userName;
if (delCount == 0)
{
tk = (take *) malloc ((delCount*200) + 200);
}
else
{
tk = (take *) realloc (tk, (delCount*1) + 1);
}
if (tk == NULL)
{
printf("This cannot be deleted (out of memory");
}
else
{
tk[delCount].FirstName = (char *) malloc(sizeof(char)*20);
tk[delCount].LastName = (char *) malloc(sizeof(char)*20);
printf("\nEnter their First Name: ");
scanf("%s", tk[delCount].FirstName);
printf("\nEnter their Last Name: ");
scanf("%s", tk[delCount].LastName);
}
for (i = 0; i < count; i++)
{
if (lt[i].FirstName == NULL || lt[i].LastName == NULL) continue;
if (strcmp(lt[i].FirstName, tk[delCount].FirstName) == 0 || strcmp(lt[i].LastName, tk[delCount].LastName) == 0)
{
printf("\n%s %s has been deleted", lt[i].FirstName, lt[i].LastName);
lt[i].FirstName = NULL;
lt[i].LastName = NULL;
q = 1;
break;
}
if (q != 1)
{
printf("\nThis person is not in the Phonebook");
}
} //End for loop
delCount++;
system("pause");
}//End function
//Display all phonebook entries
void Display()
{
int i;
printf("\nYour contacts:\n");
for (i = 0; i < count; i++)
{
if (lt[i].FirstName != NULL || lt[i].LastName != NULL)
{
printf("\n%s %s: %d\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
}
}//End for loop
system("pause");
}//End function