Thread: Need a hand with a Phonebook program

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Need a hand with a Phonebook program

    Hi guys, I'm just starting to learn C and I've hit a wall with my current task. Any pointers or nudges in the right direction will be greatly appreciated.

    Here's what I have to do:
    "Write a program that uses an array of structures to hold contact information for your friends. The program should allow the user to enter as many friends as the user wants. Create functions to add or delete entries in the phone book and to print valid phone book entries. Do not display phone book entries that are invalid or NULL (0). You can assume that all people have unique names. Make sure you allocate and free memory as necessary."

    Aaaaaaaaaaand here's what I have thus far:

    Code:
    #include <stdio.h>
    #include <string.h>
    int i;
    int number_of_entries;
    typedef struct{
            char first [15];
            char last [15];
            char phone [15];
            }phonebook;
    
    main (){
    phonebook entry[50];
    int response;
    
    printf("\n\tPhonebook\n\n");
    printf("(1)Add Friend\n");
    printf("(2)Delete Friend\n");
    printf("(3)Show Phone Book\n");
    printf("Enter selection: ");
    scanf("%d", &response);
    
    switch (response){
    case 1:
         number_of_entries++;
         printf("Enter First Name: ");
         scanf("%s", entry[i].first);
         printf("Enter Last Name: ");
         scanf("%s", entry[i].last);
         printf("Enter Phone Number: ");
         scanf("%s", entry[i].phone);
         return 0;
         break;
    
    case 2:
    case 3:
         for(i; i<number_of_entries; i++){
         printf("%s, %s, %s", entry[i].last, entry[i].first, entry[i].phone);  }
         return 0;
         break; 
                     }
    system("pause");
    return 0;
    }
    I'm sure there are errors abound, so go easy on me. When it doesn't output random characters (my guess is thats its pointing to a random address), it simply prints out blank values with commas between them. I'm not really sure where to go from here :\

    Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, since it says you have to have an unlimited number, then you probably want a linked list (there's a tutorial on the topic, plus a million forum posts). Also, since they said you want to allocate and free memory (that means, don't just use an array of 50). Also, if you want to ask for multiple entries, then yo need to wrap the whole thing in a loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Definitely change it over a linked list, with dynamic memory use. That's clearly what the assignment is about.

    For proper printing, (and a tip), I would leave the link to nodes with erased friends, in place. Just '\0' out their name. Then when you go to print out the phonebook, don't print out any node in your linked list that has no name.

    The '\0' also should indicate that the entire node is available for a new data set, when needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM