Mega Code Archive
Using a structure representing a persons name
/*
Beginning C, Third Edition
By Ivor Horton
ISBN: 1-59059-253-0
Published: Apr 2004
Publisher: apress
*/
#include
#include
#include
#define FIRST_NAME_LEN 31
#define SECOND_NAME_LEN 51
#define NUMBER_LEN 16
#define MAX_NUMBERS 50
#define TRUE 1
#define FALSE 0
/* Structure defining a name */
struct Name
{
char firstname[FIRST_NAME_LEN];
char secondname[SECOND_NAME_LEN];
};
/* Structure defining a phone record */
struct PhoneRecord
{
struct Name name;
char number[NUMBER_LEN];
};
struct Name read_name(); /* Read a name from the keyboard */
void show(struct PhoneRecord record); /* Output a phone record */
int has_name(struct PhoneRecord record, struct Name name); /* Test for a name */
void main()
{
char answer = 'n';
struct PhoneRecord records[MAX_NUMBERS]; /* Array of phone records */
struct Name aName; /* Stores a name */
int count = 0; /* Number of phone records */
int found = FALSE; /* Records when a name has been found */
int i = 0; /* Loop control variable */
/* Read an arbitrary number of phone records from the keyboard */
do
{
records[count].name = read_name(); /* Read the name */
printf("Enter the number for this name: ");
scanf(" %[ 0123456789]",records[count++].number); /* Read the number - including spaces */
printf("Do you want to enter another(y or n)?: ");
scanf(" %c", &answer);
}while(count<=MAX_NUMBERS && tolower(answer) == 'y');
/* Search the array of phone records for a number */
do
{
printf("Enter a name for which you want the number.");
aName =read_name();
for(i = 0 ; i