I'm revising a small program, I have successfully made this small program into a larger one, but I still need two functions.
- have an EDIT option
- return multiple records upon searching.
I am helping a friend of mine.
#include <stdio.h>
#define CAPACITY 100
/* User commands */
#define ADD 'a'
#define FIND   'f'
#define LIST   'l'
#define QUIT   'q'
#define NAME_LEN 80
#define NUMBER_LEN 40
/* In memory representation of an entry in the phonebook. */
typedef struct {
  char name[ NAME_LEN ];
  char number[ NUMBER_LEN ];
} phone_record;
/* Procedures in phonebook.c */
char get_command( void );
phone_record *get_record( void );
void add_record( phone_record *new_record );
void list_phonebook( void );
int find_name( char *name );
int num_entries;           // Number of entries currently in the phone book
phone_record **phonebook;  // Where the names are stored
int main( int argc, char **argv ) {
  char ch;
  char name[ NAME_LEN ];
  char confirm[ 10 ];
  phone_record *rec;
  int loc;
  // Create an empty phonebook
  phonebook = (phone_record **)malloc( sizeof( phone_record *) * CAPACITY );
  num_entries = 0;
  // Read commands until the user gets tired
  while ( ( ch = get_command() ) != QUIT ) {
    switch( ch ) {
      case ADD:
        // Get new info
        rec = get_record();
          add_record( rec );
        break;
      case FIND:
        // Name to find
        printf( "Name:  " );
        scanf( "%s", name );
        // Look for the name
        if ( ( loc = find_name( name ) ) != -1 ) {
          printf( "Number:  %s\n", phonebook[ loc ]->number );
        }
        else {
          printf( "That name is not in the phonebook\n" );
        }
        break;
      case LIST:
        // List the phonebook
        list_phonebook();
        break;
    }
  }
}
/* 
 * Read and return a command from the keyboard. 
 */
char get_command() {
  char line[ 80 ];
  do {
    // Get input
    printf( "pb> " );
    // scanf returns -1 when it encoutners EOF - pretend we saw quit
    if ( scanf( "%s", line ) == -1 ) {
      line[ 0 ] = QUIT;
      printf( "\n" );  // Add new line so terminal looks nice
    }
    // Verify input (lightly)
    switch( line[ 0 ] ) {
      case ADD:
      case FIND:
      case LIST:
      case QUIT:
        break;
      default:
        printf( "Unrecognized command\n" );
        line[ 0 ] = 0;
    }
  } while ( line[ 0 ] == 0 );
  return line[ 0 ];
}
/*
 * Add a new record to the phonebook.
 */
void add_record( phone_record *new_record ) {
  int cur;
  // Make sure there is room
  if ( num_entries == CAPACITY ) {
    printf( "Sorry phonebook is full\n" );
  }
  else {
    // Insertion sort.  Start at bottom and copy elements down one until
    // you find the first one less than what we are adding or we hit the
    // top of the phonebook
    for ( cur = num_entries; 
          cur > 0 && strcmp( phonebook[ cur - 1 ]->name, new_record->name ) > 0;
          cur = cur - 1 ) {
      phonebook[ cur ] = phonebook[ cur - 1 ];
    }
    // Add the entry in the open slot
    phonebook[ cur ] = new_record;
    num_entries = num_entries + 1;
  }
}
/*
 * List the entries in the phonebook.
 */
void list_phonebook() {
  int i;
  if ( num_entries != 0 ) {
    printf( "Name\t\tNumber\n" );
    printf( "----\t\t------\n" );
    for ( i = 0; i < num_entries; i = i + 1 ) {
      printf( "%s\t\t%s\n", phonebook[ i ]->name, phonebook[ i ]->number );
    }
  }
  else {
    printf( "There are no entries in the phonebook\n" );
  }
}
/*
 * Find a name in the phonebook.  -1 means it is not there.
 */
int find_name( char *name ) {
  int pos = -1;
  int i;
  for ( i = 0; pos == -1 && i < num_entries; i = i + 1 ) {
    if ( strcmp( name, phonebook[ i ]->name ) == 0 ) pos = i;
  }  
  return pos;
}
/*
 * Read and return a phone record from the keyboard.
 */
phone_record *get_record() {
  phone_record *rec;
  char *name;
  char *number;
  // Allocate storage for the phone record.  Since we want the record
  // to live after the function returns we need to use malloc
  rec = (phone_record *)malloc( sizeof( phone_record ) );
  // Get the data
  printf( "Name:  " );
  scanf( "%s", rec->name );
  printf( "Phone:  " );
  scanf( "%s", rec->number );
  return rec;
}
The find_name() function which locates a matching name in the phonebook array is:
int find_name( char *name ) {
  int pos = -1;
  int i;
  for ( i = 0; pos == -1 && i < num_entries; i = i + 1 ) {
    if ( strcmp( name, phonebook[ i ]->name ) == 0 ) pos = i;
  }  
  return pos;
}
it only returns one integer showing the position of a matching element of phonebook:
if ( ( loc = find_name( name ) ) != -1 ) {
  printf( "Number:  %s\n", phonebook[ loc ]->number );
}
I'm thinking that the method find_name should return an array instead, but I don't know how to implement that.
 
     
    