Thread: tell me the errors of this simple phone book ?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    24

    tell me the errors of this simple phone book ?

    this simple phonebook programme has some errors.its not compile.can someone correct the code?

    Code:
    /*
    Description:
    A program utilizing data structures to create, delete, and display phone book entries.
    User can add, delete, and print the phone book entries.
    */
    
    #include <stdio.h>     // Standard C input & output library
    #include <stdlib.h>    // mallac, callac, and free functions
    #include <string.h>		// String functions (string copy, string compare)
    
    
    typedef struct PhoneBookEntry         // Declaration of phone book structure
    {
         char FirstName[21];
         char LastName[21];
         int PhoneNum;
    } person; 
    
    // FUNCTION PROTOTYPES:
    void Add (int*, int, person*);	// Function to add an entry
    void Del (int*, int, person*);	// Function to delete an entry
    void Print (int*, int, person*);	// Function to print the phone book
    
    
    int main() {             // Main driving function
    
        int select=0; // Integer variable for user input of phone book operation
        int contact_ctr=0; // Counter to track number of contacts
        int i=0; // Index for printing "for" loop
    	
        
        person contacts[50];   // Declare an array of phone book entries
        person* pcontacts;  // Declare a pointer for array Entries
        pcontacts = (person*) calloc(0, sizeof(person));    // Set pointer to allocated/initialized block of memory, size of person struct
        if (pcontacts == NULL)
           {printf("Out of memory!  Aborting...");
           return 1;}
        else  {}    // Do nothing!
    
        
        do {  // Print menu to screen, ask user for selection...
            printf("\n\nPhone Book Application:\n\n");
            printf("1) Add friend\n");
            printf("2) Delete friend\n");
            printf("3) Show phone book\n");
            printf("4) Exit\n");
            printf("What do you want to do? ");
            scanf("%d", &select);    // Store user input in variable "select"
            getchar();               // Clear the input buffer
       
            switch (select)
            {
                   case 1:     // User wants to add an entry
    				    pcontacts = realloc(pcontacts, contact_ctr * sizeof(person));     // Reallocate memory for additional contact
    					Add (&contact_ctr, i, contacts);
                        break;
                   case 2:    // User wants to delete an entry
    					Del (&contact_ctr, i, contacts);
                        break;
                   case 3:    // User wants to print the phone book
    					Print (&contact_ctr, i, contacts);
    					break;
                   case 4:    // User wants to quit, break the switch and exit to loop
                        break;
                   default:   // User entered a value other than 1,2,3, or 4.
                        printf("You entered an invalid selection.  Try again.\n\n\n"); 
                        break;
            }  // End switch statement
            }  // End do loop
            
       while (select!=4);
       
       printf("\nThis phone book will now close.  Goodbye!\n");
       // Pause program until user inputs any character
       getchar();
       // Return statement
       return 0;
    }      // End main
    
    
    void Add (int *contact_ctr, int i, person *contacts)
    {
    	(*contact_ctr)++;
    	// Need to add realloc failsafe, if NULL
        printf("\nFirst name: ");
        scanf("%s", &contacts[*contact_ctr-1].FirstName);
        printf("Last name: ");
        scanf("%s",  &contacts[*contact_ctr-1].LastName);
        printf("Phone number (7-digit, no dash): ");
        scanf("%d",  &contacts[*contact_ctr-1].PhoneNum);
        printf("Record added to the phone book\n\n");
    }	// End function Add
    
    void Del (int *contact_ctr, int i, person *contacts)
    {
    	char delTempf[21];	// Temporary string for deletion of an entry - first name
    	char delTempl[21];	// Temporary string for deletion of any array - last name
    	char nullStr[21] = {"\0"};	// Null string for use in deleting entries
    
    	printf("\nFirst name: ");
    	scanf("%s", &delTempf);
        printf("Last name: ");
    	scanf("%s", &delTempl);
    	// compare strings, find entry or return entry not found
    	for (i=0; i<*contact_ctr; i++)
    	{
    		if (strcmp(delTempf, contacts[i].FirstName) == 0)
    		{
    			strcpy(contacts[i].FirstName, nullStr);
    			strcpy(contacts[i].LastName, nullStr);
    			strcpy(contacts[i].PhoneNum, nullStr);
    			free(&contacts[i]);
    			(*contact_ctr)--;	// Contact deleted, update the contact total counter
    			break;
    		}
    		else
    		{
    			printf("\nEntry not found.\n\n");				
    		}
    	}	// End for loop
    
        printf("Record deleted from the phone book\n\n");
    }	// End function Del
    
    void Print (int *contact_ctr, int i, person *contacts)
    {
    	char nullStr[21] = {"\0"};	// Null string
    	if (*contact_ctr > 0)	// If there are entries, print them
    	{
    		printf("\n\nPhone Book Entries:\n\n");
    		for(i=0; i<*contact_ctr; i++)
    		{
    			if (strcmp(nullStr, contacts[i].FirstName) != 0)	// If this index is NOT null, print it
    			{
    				printf("%s %s %d\n", contacts[i].FirstName, contacts[i].LastName, contacts[i].PhoneNum);
    			}
    			else {}	// Do nothing (skip this index because no entry exists)
    		} // end for loop
    	}
    	else	// Else the book is empty, tell the user
    	{
    		printf("\n\nThe phone book is currently empty.\n\n");
    	}
    }	// End function Print

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to get more engaged in your program, Blogchama. You must have tried to compile your program, so you tell us, exactly what ARE the errors?

    What did you do to try to fix them?

    Sounds like a homework "dump" onto a forum, to me.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    24
    Quote Originally Posted by Adak View Post
    You need to get more engaged in your program, Blogchama. You must have tried to compile your program, so you tell us, exactly what ARE the errors?

    What did you do to try to fix them?

    Sounds like a homework "dump" onto a forum, to me.
    i try to fix them,but have't a clear idea about the erorors,thats why asked for a help.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > person* pcontacts; // Declare a pointer for array Entries
    > pcontacts = (person*) calloc(0, sizeof(person)); // Set pointer to allocated/initialized block of memory, size of person struct
    The first thing to do is DELETE all your use of calloc / realloc / malloc / free.
    It's wrong to begin with (likely to crash when you run it), and completely unnecessary.

    > person contacts[50];
    This is already fully allocated for your program as it stands.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by blogchama View Post
    this simple phonebook programme has some errors.its not compile.can someone correct the code?
    We don't correct code, we correct the person that wrote the code
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    More to ponder about: SourceForge.net: Scanf woes - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. phone book directory 2
    By arangawawa in forum C Programming
    Replies: 4
    Last Post: 08-01-2003, 05:32 PM
  3. 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
  4. hangman game, copied staight from book.. 31 errors?!?!?!?!
    By Blizzarddog in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2002, 12:20 AM
  5. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM