Advertisement
rafikamal

Card Catalog

Jan 27th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX 100
  5.  
  6. void display(int i);
  7. void addNew();
  8. void search();
  9. void showAll();
  10.  
  11. int top=0; /*last created value*/
  12.  
  13. struct catalog
  14. {
  15.     char name[80];
  16.     char author[80];
  17.     char pub[80];
  18.     char date[80];
  19.     char ed[10];
  20. } cat[MAX];
  21.  
  22. int main()
  23. {
  24.     int choice;
  25.     int i;
  26.    
  27.     printf("1.New catalog\n");
  28.     printf("2.Search by title\n");
  29.     printf("3.Show All\n");
  30.     printf("4.Exit\n");
  31.        
  32.     do
  33.     {
  34.         printf("Choose your selection:\n");
  35.         scanf("%d", &choice);
  36.         getchar();
  37.        
  38.         if(choice < 1 || choice > 3)
  39.             printf("Invalid Selection\n");
  40.         else if(choice == 1)
  41.             addNew();
  42.         else if(choice == 2)
  43.             search();
  44.         else if(choice == 3)
  45.             showAll();
  46.  
  47.     } while(choice != 4);
  48.    
  49.     return 0;
  50. }
  51.  
  52. void display(int i)
  53. {
  54.     printf("%s\n",cat[i].name);
  55.     printf("by %s\n",cat[i].author);
  56.     printf("published by %s\n",cat[i].pub);
  57.     printf("copyright: %s, %s edition\n\n",cat[i].date,cat[i].ed);
  58. }
  59.  
  60. void showAll()
  61. {
  62.     int i;
  63.     for(i = 0; i < top; i++)
  64.     {
  65.         display(i);
  66.         printf("\n");
  67.     }
  68. }
  69.  
  70. void addNew(void)
  71. {
  72.     int i;
  73.     char temp[80];
  74.  
  75.     printf("Enter title:\n");
  76.     gets(cat[top].name);
  77.     printf("Enter author:\n");
  78.     gets(cat[top].author);
  79.     printf("Enter publisher:\n");
  80.     gets(cat[top].pub);
  81.     printf("Enter copyright date:\n");
  82.     gets(temp);
  83.     printf("Enter edition:\n");
  84.     gets(cat[i].ed);
  85.  
  86.     top++;
  87. }
  88.  
  89. void search(void)
  90. {
  91.     int i,found=1;
  92.     char title[80];
  93.  
  94.     printf("Enter the title of the book you want to search:\n");
  95.     gets(title);
  96.  
  97.     for(i=0;i<=top;i++)
  98.         if(!strcmp(title,cat[i].name))
  99.         {
  100.             display(i);
  101.             found=0;
  102.         }
  103.     if(found) printf("Sorry! Book not found.\n");
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement