Computer Science Canada Telephone Directory ( TurboC ) |
Author: | rickdragon [ Tue Jan 20, 2004 7:57 am ] |
Post subject: | Telephone Directory ( TurboC ) |
/* proj_2.c - Telephone Directory */ #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> void add_num(void) , lookup(void); int menu(void); main() { char ch; do { ch = menu(); switch(ch) { case 'a' : add_num(); break; case 'l' : lookup(); break ; } } while ( ch != 'q'); } // DISPLAY MENU & GET REQUEST menu(void) { char ch; do { printf(" (A)dd , (L)ookup , (Q)uit : "); ch = tolower(getche()); printf("\n"); } while(ch != 'q' && ch != 'a' && ch != 'l'); return ch ; } // Add a name and number void add_num(void) { FILE *fp; char name[80]; int a_code , exchg , num ; // open for append if((fp=fopen(" phone","a"))==NULL) { printf(" can not open file \n "); exit(1); } printf("Enter name and number "); fscanf(stdin, "%s %d %d %d ",name,&a_code,&exchg, &num); fscanf(stdin, "%*c"); // remove CR from input stream // write to file fprintf(fp,"%s %d %d %d \n",name,a_code,exchg,num); fclose(fp); } // find number given a name void lookup(void) { FILE *fp; char name[80], name2[80]; int a_code , exchg , num ; // open for read if((fp = fopen("phone ","r"))==NULL) { printf("can not open a file \n"); exit(1); } printf("name ..?");gets(name); // lookup for number while(!feof(fp)) { fscanf(fp, "%s %d %d %d ",name2,&a_code,&exchg, &num); if(!strcmp(name,name2)) { printf("%s : (%d) %d - %d \n ",name,a_code,exchg,num); break; } } fclose(fp); } |