Thread: a phone book program

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    a phone book program

    hi all, ive moved on to the next thing and its killing me, i dont even know really how to begin. the section is all about making a main function and writing other .h files and using them in the main function. basically its a phone book using structures and pointer arrays. the ability to add more information and allocate the necessary memory. also to later be able to edit the information stored and write it to a file. this is the little that im pretty sure is not right at all. thanks for the help ahead of time, im gonna need it with this one.

    Program Info:


    1. Create a phone book program that allows users to enter names and phone numbers of friends and acquaintances. Create a structure to hold contact information and use calloc to reserve the first memory segment. The user should be able to add phone book entries through a menu in a loop. Use the realloc function to add contiguous memory segments to the original memory block when a user adds a new phone book entry.
    Your structure should hold first name, last name, email address, and phone number.

    Hint: Your main program should create the structure in a header file called “main.h”.

    main.h should look like the following:

    typedef struct contact {
    char first[20];
    char last[20];
    char email[20];
    char phone[10];
    } c;


    Your main.c program should create a dynamic array of these structures and then dynamically allocate memory as necessary. The main.c program will contain the menu. Your individual functions will ask the users for the appropriate values.

    You will need to create an external function called add with the following signature. Create this in the header file process.h

    void add( c*, int ); where c* is the array of structures, and int is the number of the current address to be added to the book.

    2. Create the ability to edit contact information for a person (Add it to your menu). Ask the user for the first name, last name of the contact to edit. Search through your array and find the item. If the person is not in the list, then print a message to the user saying that. If the person is in the list, ask them for the new email address and phonenumber and update the structure accordingly.

    Add the following prototype to the file process.h

    Void edit( c*, int );
    Where c* is the array of structures, and int is the current index to be edited.
    Edit will modify the user information in the array of structures.


    3. Add the ability (another menu option) to save your contact information to a file. The data should be stored as name emailaddress phonenumber






    My Code So Far
    the main.c program
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include "main.h"
    #include "process.h"
    
    int main() {
        int entries, i;
        c *entry;
        *ptrC = c;
        c *newc;
        int choice;
        
        c = (int *) calloc (1, sizeof(int));
        
            if (c == (int *) NULL)
        {
              printf("\n Failed to allocate grades in array\n");
              exit(1);
        }
        
        while (choice != 5); {
        printf("Welcome to the Phonebook\n\n");      
        printf("1)Add entries to phone book.\n");
        printf("2)To edit entries.\n");
        printf("3)Print Phonebook.\n");
        scanf("%d", &choice);
        
    
                     if (choice = 1){
                                printf("Enter the number of phone book entries:");
                                scanf("%d", &entries); 
                                add (c*, entries);
                                }
                     if (choice = 2) {
                                edit(c*, i);
                                }
                     if (choice = 3)
                                {
                                    //havent gotten to yet
                                    }
                                    }
                     system("PAUSE");
                     
                     return 0;



    the main.h
    Code:
    typedef struct contact {
        char first[20];
         char last[20];
        char email[20];
       char phone[10];
                            } c;
     
     
     void add(c*, int entries){
    
                                       
                   
                      newc = realloc (c, entries * sizeof(int));
                      
                      if (newc == NULL) {
                               printf("out of memory.");
                                         }
                      else 
                             c = newc;
    
        
                                 for( i = 0; i < entries; i++ ) {  
                                   printf("Enter first name:\n");
                                     scanf("%s", &c[i].first);
    
                                     printf("Enter last name:\n");
                                     scanf("%s", &c[i].last);
    
                                     printf("Enter email:\n");
                                     scanf("%s", &c[i].email);
          
                                     printf("Enter phone:\n");
                                     scanf("%s", &c[i].phone);
                                                               }
                                 }
                     
                     free(c);
                     free(newc);


    the process.h

    Code:
    void add(c*, int);
    void edit(c*, int);

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why are you using realloc() when you really want malloc()?

    Or perhaps I should say, why are you not taking into account the number of entries already in the phone book when you are adding new entries.

    Code:
        c = (int *) calloc (1, sizeof(int));
        
            if (c == (int *) NULL)
    This code won't compile. "c" is a type - very badly named, to add insult to injury.

    You also shouldn't cast the result of malloc, and you should not cast NULL to a different type either - it is compatible with any type of pointer already. If you MUST cast it, at least cast it to the correct type that you want.

    Note that using scanf() to read strings is unsafe. Imagine that someone enters a longer e-mail address than twenty characters. [my work e-mail address is 25 characters long, my private e-mail is 21 characters, my gmail account is about 25 too]. It will then overwrite the next field in the struct, and there will be no end-marker for that string once the next field is filled in. Use fgets() instead.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. You've got code for the function add in main.h that does not really belong in a header file. It also looks like some of it is outside of any block of code (the two calls to free), unless that's a typo.

    2. In general, your indentation needs work.

    3. Where is ptrC declared?
    Code:
    int main() {
        int entries, i;
        c *entry;
        *ptrC = c;
    4. You've got an apparent scope issue. You have a variable, newc, that is declared in main but referenced in function add... unless void add(c*, int entries){ is meant to be void add(c* newc, int entries){ (a variable name is missing from this function).

    5. This bit c = newc; in function add is not legal, as matsp has mentioned, c is a type and you can't assign something to a type.

    6. You are calling your functions wrong:
    Code:
    add (c*, entries);
    }
    if (choice = 2) {
        edit(c*, i);
    Last edited by hk_mp5kpdw; 09-19-2007 at 06:34 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. strtok() a phone number
    By Sure in forum C Programming
    Replies: 3
    Last Post: 06-27-2005, 06:46 PM
  4. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  5. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM