address.c |
/* Address and Phone book program using an array of structures */ #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #define listings 20 struct add_ph { char first_name[10]; char last_name[15]; char phone[15]; char address1[20]; char address2[20]; char city[15]; char use_state[4]; char zip[11]; } entry[listings]; void change(), newentry(), show(); void initialize(), enter(), input(int); int menu(); int main(){ char option; initialize(); for(;;) { option = menu(); switch(option) { case 'c': change(); break; case 'n': newentry(); break; case 's': show(); break; case 'q': return 0; } } } // Subroutine to initialize the array void initialize(){ int j; for(j=0; j<listings; j++) *entry[j].last_name = '\0'; } // Subroutine for menu selection int menu(){ char sel[10]; char fed; printf("\n"); do { //sel=0; printf("Choose from the following options:\n"); printf("(N)ew entry\n"); printf("(C)hange an existing entry\n"); printf("(S)how all current entries\n"); printf("(Q)uit\n\n"); printf("Make your selection: "); scanf("%s", sel); //sel=tolower(getchar()); } while (!strchr("ncsq", tolower(sel[0]))); // will repeat until the user inputs any of the letters n, c, s, q return tolower(sel[0]); } // Inputs information for new entries or updating entries void input(int j){ // char str[90]; printf("First name: "); scanf("%s", entry[j].first_name); printf("Last name: "); scanf("%s", entry[j].last_name); printf("Phone number: "); scanf("%s", entry[j].phone); printf("Address line 1: "); scanf("%s", entry[j].address1); printf("Address line 2: "); scanf("%s", entry[j].address2); printf("City: "); scanf("%s", entry[j].city); printf("2-letter state abbreviation: "); scanf("%s", entry[j].use_state); printf("Zip code: "); scanf("%s", entry[j].zip); } // Subroutine to make new entry void newentry(){ int j; for(j=0; j<listings; j++) if (!*entry[j].last_name) break; // finds first empty listing if (j==listings) { printf("I'm sorry, the phone book is full.\n"); return; } input(j); } // Subroutine to change an existing entry void change(){ int j; char LN[50]; printf("Enter the person's last name: "); scanf("%s",LN); for(j=0; j<listings; j++) if(!strcmp(LN, entry[j].last_name)) break; if(j==listings) { printf("No listing found.\n"); return; } printf("Enter new information.\n"); input(j); } // Subroutine to show the listings void show(){ int j; for(j=0; j<listings; j++) { if (*entry[j].last_name) { printf("Name: %s ",entry[j].first_name); printf("%s\n",entry[j].last_name); printf("Phone: %s\n",entry[j].phone); printf("Address: %s st.\n",entry[j].address1); printf("Address: %s\n",entry[j].address2); printf("City: %s, ",entry[j].city); printf("%s ",entry[j].use_state); printf("%s\n\n",entry[j].zip); } } } |
James Little |