#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//typedef struct to define what's in the phonebook
typedef struct PhoneBookContacts
{
    char Name[20];
    char Surname[20];
    char PhoneNumber[20];
} phone;

//Function prototypes
void AddEntry (phone[]);
void DeleteEntry (phone[]);
void PrintEntry (phone[]);
void Sort (phone[]);
int counter = 0; //Global counter variable used to keep track of number of contacts

//Begin main function
int main (void)
{
    phone phonebook[20]; //Phonebook instance
    char userChoice; //Variable to use to select menu choice

    while (userChoice != 'Q') {
        printf ("***************\n");
        printf ("Please enter a command:\n");
        printf("'A': Add an entry\n");
        printf("'D': Delete an entry\n");
        printf("'S': Sort entries\n");
        printf("'P': Print the phonebook\n");
        printf("'Q': Quit\n");
        printf ("***************\n");

        scanf("%s", &userChoice);  //Stores menu choice into variable userChoice

        // Add Contact
        if (userChoice == 'A')
            AddEntry(phonebook);

        //Remove Contact
        if (userChoice == 'D')
            DeleteEntry (phonebook);

        //Print Contacts
        if (userChoice == 'P')
            PrintEntry(phonebook);

        //Sort Contacts
        if (userChoice == 'S')
            Sort(phonebook);

        //Quit
        if (userChoice == 'Q') {
            printf("Phonebook will now quit.");
            return 0;
        }
    }
}

//Function Definition to Add Contacts to the Phonebook
void AddEntry (phone phonebook[]) {
    counter++; //global counter increase

    printf("\nFirst Name: ");
    scanf("%s", phonebook[counter-1].Name); //counter-1 b/c arrays start at 0

    printf("Last Name: ");
    scanf("%s", phonebook[counter-1].Surname);

    printf("Phone Number (XXX-XXX-XXXX): ");
    scanf("%s", phonebook[counter-1].PhoneNumber);

    printf("\n%s added to phonebook\n", phonebook[counter-1].Name); //tell user friend added
}

void DeleteEntry (phone phonebook[])
{
    int x = 0;
    char deleteName[20];  // Temp string to compare to existing phonebook
    char deleteSurname[20];  //temp string
    char nullStr[20] = {"\0"};  // empty string to remove phonenumber

    printf("\nEnter name: ");
    scanf("%s", deleteName); //place into temp string
    printf("Enter Surname: ");
    scanf("%s", deleteSurname); //place into temp string

    for (x = 0; x < counter; x++)
    {
        if (strcmp(deleteName, phonebook[x].Name) == 0) //compare deleteName to phonebook.Name
        {
            for (x = 0; x < counter; x++)
            {
                if (strcmp(deleteSurname, phonebook[x].Surname) == 0) //If deleteSurname matches phonebook.Surname
                {
                    strcpy(phonebook[x].Name, nullStr); //Put null into Name
                    strcpy(phonebook[x].Surname, nullStr); //Null into Surname
                    strcpy(phonebook[x].PhoneNumber, nullStr); //Null into PhoneNumber
                    printf("Contact removed from phonebook.\n");
                    counter--;
                    break;
                }
            }

        }
        else printf("Invalid entry--try again.\n");
    }
}

// Function def to print contacts
void PrintEntry (phone phonebook[]) {
    int x = 0;
    printf("\nPhonebook entries:\n");

    for ( x = 0; x < counter; x++) {
        printf("\n(%d)\n", x+1); //Show contact number
        printf("Name: %s %s\n", phonebook[x].Name, phonebook[x].Surname); //Name
        printf("Number: %s\n", phonebook[x].PhoneNumber); //Number
    }
}

void Sort (phone phonebook[]) {
    phone temp;
    int i;  int j;

    for (i=0; i<19; i++) {
        for (j=i+1; j<19; j++) {
            if (strcmp(phonebook[i].Surname, phonebook[j].Surname) > 0) {
                temp=phonebook[i];
                phonebook[i]=phonebook[j];
                phonebook[j]=temp;
            }
        }
    }
}

Somehow when I try to delete a certain record it mess up with the other records that are saved :S
Need Help...plx!

Let's pretend that the phonebook has 3 entries: e0, e1 and e2. counter is 3. Then you delete e0. Counter becomes 2. Now when you try to print them, you printing e0 (which is zeroed out) and e1, instead of desired e1 and e2.

There are numerous ways out. You may sort the phonebook after deletion. You may copy the last valid entry on top of the deleted one (instead of clobbering it). You may drop the counter at all in favor of some validity flag. Etc.

Let's pretend that the phonebook has 3 entries: e0, e1 and e2. counter is 3. Then you delete e0. Counter becomes 2. Now when you try to print them, you printing e0 (which is zeroed out) and e1, instead of desired e1 and e2.

There are numerous ways out. You may sort the phonebook after deletion. You may copy the last valid entry on top of the deleted one (instead of clobbering it). You may drop the counter at all in favor of some validity flag. Etc.

I dont know, can you help with some example on that code :o

You don't know what? I just described how your code works.

There is few mistakes in your program:

What's happen if you tried to add more than 20 entries?

What's happen when you have two (or more) entries with the same Name value and you try to delete one of them?

So I suggest you four tips:

-- try to use some "#define table_size" to know how many elements you can put in you table.

-- to know if a table entrie is empty, you might check if it contain a null string (this can be usefull to help you to make the same program without your "counter" variable).

-- according to the previous tips, you might initialise all your entries with null strings at the begin of your program.

-- instead of puting new entries at the end of the table, put them at the fisrt free entrie (first entrie that contain a null string) and then you can short all the list.

Few days ago you start this thread:


"Hi Programmers,

Please help me, i was wondering on how to create a simple phone book program using struct array to insert a new contact, update an existing contact, delete an existing contact and to display the contact informations"

And now you ask us to debug an example writen by someone else


Are you trying to use us to make your homework?

Ah I see he exactly copied the code from the link I provided as an example in the other thread yet he didn't even bother to check the comments about that code...

My intention was just to show him just an example of how a phone book program could possibly look and work like... was I wrong to show him that :(

I do hope the OP makes his own phonebook program instead of using exactly this code from that link and he's just using this to learn

I think we give him engough clues to do the job, I hope he will not open a third thread to find people to finish the job...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.