Guides:C/C Crash Course/Simple Address Book Example

From CoderGuide

Jump to: navigation, search

Back to TOC

Simple Address Book Example

Now that we understand how to use Structures, we can create a program that does something useful. It's a simple address book program:

  1. /*******************
  2.  phonebook.c -- a simple phone book program demonstrating the use
  3.                 of structures.
  4. ********************/
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #define ENTRIES 20
  10.  
  11. struct Record{
  12.         char name[40];
  13.         struct {
  14.                 unsigned char year; /*years after 1900*/
  15.                 char month;
  16.                 char day;
  17.         }dob;
  18.         char phone[20];
  19. }records[ENTRIES];
  20.  
  21. char chget();
  22. void display(struct Record *);
  23. void edit(struct Record *);
  24. void list();
  25.  
  26. /*************
  27.  ** chget() **
  28.  *************/
  29. char chget(){
  30.         char ch,x;
  31.         x=ch=getchar();
  32.         for(;x!='\n';x=getchar());
  33.         return ch;
  34. }/*end chget()*/
  35.  
  36. /************
  37.  ** main() **
  38.  ************/
  39.  
  40. int main(void){
  41.         char cmd;
  42.         int index;
  43.  
  44.         /* Initialize array*/
  45.         for(index=0;index<ENTRIES;index++){
  46.                 records[index].name[0]=0;
  47.                 records[index].phone[0]=0;
  48.                 records[index].dob.year=0;
  49.                 records[index].dob.month=0;
  50.                 records[index].dob.day=0;
  51.         }/*end for(..I<ENTRIES,, */
  52.         index=0;
  53.  
  54.         do{
  55.                 int x; /*a temporary variable*/
  56.  
  57.                 printf("\nRECORD %02d OF %02d\n",index,ENTRIES-1);
  58.  
  59.  
  60.                 display(&records[index]);
  61.  
  62.  
  63.                 puts("\n(E)dit Entry");
  64.                 puts("(N)ext Entry");
  65.                 puts("(P)revious Entry");
  66.                 puts("(L)ist entries (brief)");
  67.                 puts("(Q)uit");
  68.                 printf("\nCommand:");
  69.  
  70.                 cmd=chget();
  71.                 /*process the command*/
  72.  
  73.                 switch(cmd){
  74.                         case 'E':
  75.                         case 'e': 
  76.                                 edit(&records[index]); 
  77.                                 break;
  78.  
  79.                         case 'N': 
  80.                         case 'n':
  81.                                 index++;
  82.                                 /*loop around if we've come to the 
  83.                                 end*/
  84.                                 if(index>ENTRIES)index=0;
  85.                                 break;
  86.  
  87.                         case 'P':
  88.                         case 'p':
  89.                                 index--;
  90.                                 /*loop around if we've come to the 
  91.                                   end*/
  92.                                 if(index<0)index=ENTRIES-1;
  93.                                 break;
  94.  
  95.                         case 'L':
  96.                         case 'l': 
  97.                                 list(); 
  98.                                 break;
  99.  
  100.                         case 'Q':
  101.                         case 'q': break;
  102.  
  103.                         default:
  104.                                 puts("BAD COMMAND");
  105.                                 break;
  106.                 } /*end switch(ch)*/
  107.         }while(cmd!='q' && cmd!='Q');
  108.  
  109. } /*end main()*/
  110.  
  111.  /************
  112.  ** list() **
  113.  ************/
  114. void list(){
  115.         int x;
  116.         puts("*** LISTING ***");
  117.         for(x=0;x<ENTRIES;x++){
  118.                 printf("%02d:%-40s|%-20s|", x,records[x].name, 
  119.                    records[x].phone);
  120.  
  121.                 /*we need to type cast these as ints to ensure they 
  122.                   are passed to printf integers*/
  123.                 printf("%04d-%02d-%02d\n", 
  124.                    (int)records[x].dob.year+1900,
  125.                    (int)records[x].dob.month,
  126.                    (int)records[x].dob.day);
  127.         }/*end for()*/
  128.  
  129.         printf("** Pause **"); 
  130.         chget();
  131. }/* end list*/
  132.  
  133. /***************
  134.  ** display() **
  135.  ***************/
  136.  
  137. void display(struct Record *r){
  138.         int x;
  139.         /*print out a bar of stars*/
  140.         for(x=0;x<60;x++)putchar('*');
  141.         putchar('\n');
  142.  
  143.         printf("Name: %s\nPhone:%s\n",r->name,r->phone);
  144.         printf("DOB:%04d-%02d-%02d\n", (int)r->dob.year+1900,
  145.            (int)r->dob.month, (int)r->dob.day);
  146.  
  147.         for(x=60;x;x--)putchar('*'); /*draw a bar of 60 stars*/
  148.         putchar('\n');
  149. }/*end display()*/
  150.  
  151. /************
  152.  ** edit() **
  153.  ************/
  154.  
  155. void edit(struct Record *r){
  156.         char s[128];
  157.         int i;      
  158.         printf("Name:");
  159.  
  160.         /*You should never use gets() in a program that matters */
  161.         gets(s); 
  162.         s[39]=0; /*Ensure the null terminator is in the right place*/   
  163.         strcpy(r->name,s);
  164.  
  165.         printf("Phone:");
  166.         gets(s);
  167.         s[19]=0;
  168.         strcpy(r->phone,s);
  169.  
  170.         puts("--Birthday--");
  171.         printf("Year:");
  172.         gets(s);
  173.         i=atoi(s);
  174.  
  175.         if(i>=1900)i-=1900;
  176.         r->dob.year=i;
  177.  
  178.         printf("Month:");
  179.         gets(s);
  180.         r->dob.month=atoi(s);
  181.  
  182.         printf("Day:");
  183.         r->dob.day=atoi(gets(s)); /*Yup, you can do that too*/
  184. }/*end edit()*/

Now, this program has two limitations:

  1. You can't save your database and load it later.
  2. The program allows for only a fixed number of entries.

The first issue we'll resolve in the section on File I/O operations. The next issue can be delt with in a few ways, but all of those methods require dynamic memory allocation, and the use of pointers.

Personal tools