Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add additional functionality to your phonebook program from lab# 6. Make it poss

ID: 3538875 • Letter: A

Question

Add additional functionality to your phonebook program from lab# 6. Make it possible for users to:

1) Alphabetically sort the list of entries by name (first or last).

2) Find a phone number for a given name.

3) Randomly select a friend from the phonebook for you to call.

4) Delete everyone from the phonebook at the same time.


THIS IS MY CURRENT CODE


#include <stdio.h>

#include <string.h>


#define BUFSIZE 1024 /* pick an extra large size, to ensure? large enough ... */

#define MENU "Contact Menu ... "

" (1) Add Contact "

" (2) Delete Contact "

" (3) Show All Contacts "

" (4) Exit ... "

"What would you like to do? "


const int BEGIN_SIZE = 8; /* pick begin size to minimize calls to realloc ... */


int SIZE = 0; /* Global variable used to keep track of number of Contacts */

int CAP = 0; /* Global variable used to keep track of capacity for Contacts */



  

typedef struct myContact

{

char* firstName;

char* lastName;

char* phoneNumber; /* num of char's for phoneNumber is 10 + terminal 0 */

} Contact ;


Contact* addContact( Contact* );

void deleteContact( Contact* );

void showAllContacts( const Contact* );


char* newCopy( const char* s )

{

char* p = malloc( strlen(s) + 1 );

if( p == NULL )

{

printf( " ERROR! malloc failed in 'newCopy' ... " );

printf( " Press 'Enter' to exit ... " );

fflush( stdout );

getchar();

exit(1);

}

/* else ... if reach here ... memory was allocated ... */

strcpy( p, s );

return p; /* return address to new memory ... */

}


char* getString( const char* msg, int maxSize, int minSize )

{

char *p, buffer[BUFSIZE];

int len;

  

for( ; ; ) /* loop forever ... until break ... */

{

printf( msg );

fflush( stdin );

fgets( buffer, BUFSIZE, stdin );

p = strchr( buffer, ' ' );

if( p != NULL ) *p = 0; /* eliminate ' ' char at end of C string */

  

len = strlen(buffer);

if( len <= maxSize && len >= minSize ) break;

/* else... */

printf( " ERROR! Input must have length in range %d..%d ",

minSize, maxSize );

}


/* when reach here ... a good len was input ... */

return newCopy(buffer);

}




int main( void )

{   

int i, iSelection = 0, numGood;

Contact* phonebook = NULL;


while( iSelection != 4 )

{

printf( MENU );

fflush( stdout );

numGood = scanf( "%d", &iSelection );

while( getchar() != ' ' ) ; /* flush stdin ... */

  

if( numGood != 1 )

{

printf( " Entry ERROR! Please enter an integer in range 1..4 " );

continue; /* goto top of while loop right now ... */

}


if( iSelection == 1 )

{

phonebook = addContact(phonebook); /* update address of new memory */

if( phonebook == NULL )

{

printf( " Error getting new memory in addContact ... "

"Press 'Enter' to exit ... " );

fflush( stdout );

getchar();

return -1;

}

}

else if( iSelection == 2 ) deleteContact(phonebook);

else if( iSelection == 3 ) showAllContacts(phonebook);

else if( iSelection == 4 )

{

printf( " Will exit now ... " );

for( i = SIZE-1; i >= 0; --i )

{

free( phonebook[i].phoneNumber );

free( phonebook[i].lastName );

free( phonebook[i].firstName );

}

free(phonebook);

}

else printf( " %d NOT implemented yet ... ", iSelection );

}

return 0;

}



Contact* addContact( Contact* phonebook )

{

void* p;

if( SIZE == CAP )

{

if( CAP != 0 ) CAP += CAP; /* double capacity ... */

else CAP = BEGIN_SIZE;

  

p = realloc( phonebook, sizeof(Contact)*CAP );

if( p == NULL ) { free(phonebook); return NULL; }

else phonebook = (Contact*) p; /* update with new address ... */

}


/* NOTE: just pointers to new copy of C string are copied below ... */

phonebook[SIZE].firstName = getString( "First Name : ", 100, 1 );

phonebook[SIZE].lastName = getString( "Last Name : ", 100, 1 );

phonebook[SIZE].phoneNumber = getString( "Phone Number : ", 10, 10 );


printf( " Contact successfully added ... " );


++SIZE;

return phonebook; /* return updated address ... */

}

void deleteContact( Contact* phonebook )

{

int x = 0;

char* deleteFirstName;

char* deleteLastName;


/* NOTE: just pointers to new copy of C string are copied below ... */

deleteFirstName = getString( "First Name : ", 100, 0 );

deleteLastName = getString( "Last Name : ", 100, 0 );

  

for( x = 0; x < SIZE; ++x )

{

if( strcmp(deleteFirstName, phonebook[x].firstName) == 0 )

{

if( strcmp(deleteLastName, phonebook[x].lastName) == 0 )

{

int i = x ;

free( phonebook[i].phoneNumber ); /* free dynamic C string */

free( phonebook[i].lastName );

free( phonebook[i].firstName );

for( ; i < SIZE-1; ++i )

{ /* copy pointers down ... */

phonebook[i].firstName = phonebook[i+1].firstName;

phonebook[i].lastName = phonebook[i+1].lastName;

phonebook[i].phoneNumber = phonebook[i+1].phoneNumber;

}

printf( " Contact deleted from contacts ... " );

--SIZE;

free( deleteLastName ); /* free all tmp memory used here ... */

free( deleteFirstName );

return;

}

}

}

  

/* else ... */

printf(" Contact NOT found ... ");

free(deleteLastName); /* free all tmp memory used here ... */

free(deleteFirstName);

}


void showAllContacts( const Contact* phonebook )

{

int x = 0;

printf( " Contacts: " );

for( x = 0; x < SIZE; ++x )

{

printf( "(%d) ", x+1);

printf( "Name: %s %s ", phonebook[x].firstName, phonebook[x].lastName );

printf( "Number: %s ", phonebook[x].phoneNumber );

}

printf( "size = %d, capacity = %d ", SIZE, CAP );

}

Explanation / Answer

This is the link to the file: https://docs.google.com/file/d/0BwqdmXu18WiMWjRLZU4xMkV0M1E/edit?usp=sharing If anything else then please ask.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote