Thread: help me to avoid exist number and how to create search in my pogram

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    help me to avoid exist number and how to create search in my pogram

    help me to avoid enter same number of book and how to create query or search for the book number and display its details... pls... thanks in advance

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    FILE *addbook;
    
    struct book
    {
    int bno; /*book#*/
    char btitle[30];
    char bversion[30];
    char bauthorfn[30];
    char bauthorln[30];
    float bprice;
    }b;
    
    main()
    {
    
    int mainchoice;
    char a;
    
    
    mainmenu:
    {
         system("CLS");
         printf("\n\n\n\n\n\n\n\t\t\t\t* MAIN MENU *");
         printf("\n\t\t--------------------------------------------");
         printf ("\n\n\t\t  [1]  BOOK PURCHASE (ADD)");
         printf ("\n\n\t\t  [2]  BOOK LIST");
         printf ("\n\n\t\t  [3]  QUERY BY BOOK");
         printf ("\n\n\t\t  [4]  EMPTY BOOK RECORD");
         printf ("\n\n\t\t  [5]  EXIT");
         printf ("\n\n\n\t\t Enter your choice: ");
         scanf("%d",&mainchoice);
    
              switch (mainchoice)
               {
                 case 1: goto bookpurchase;
                 case 2: goto booklist;
                 case 3: goto qbook;
                 case 4: goto emptybookrecord;
                 case 5: goto exxit;
                 default: 
                     printf("\n\t\t Invalid Choice!!!\n\n\t\t Please refer to the menu...\n\n\n\t\t");
                     getchar();
                     goto mainmenu;
               }
    }
    
    bookpurchase:
    {
         system("CLS");
         printf("\n\n\n\n\n\n\n\t\t\t      * NEW BOOK ENTRY *");
         printf("\n\n\n\n\n\n\n\t\t   * Input N/A if no Availabe Information*");
         printf("\n\t\t---------------------------------------------");
         fflush(stdin);
         printf("\n\n\t\tBook Number: ");
         scanf("%d",&b.bno);
         printf("\t\tBook Title: ");
         scanf("%s",&b.btitle);
         printf("\t\tBook Version: ");
         scanf("%s",&b.bversion);
         printf("\t\tBook Author First Name: ");
         scanf("%s",&b.bauthorfn);
         printf("\t\tBook Author Last Name: ");
         scanf("%s",&b.bauthorln);
         printf("\t\tPrice: ");
         scanf("%f",&b.bprice);
    
    /* SAVE FILE */
      addbook=fopen("books.txt","a+");
      if ((addbook) == NULL)
    	{
         printf("Error: Could not open file! \n");
    	 exit(1); 
        }
      else
        {
         fprintf(addbook,"\n\t\tBook#           : %d\n",b.bno);
         fprintf(addbook,"\t\tBook Title      : %s\n",b.btitle);
         fprintf(addbook,"\t\tBook Version    : %s\n",b.bversion);
         fprintf(addbook,"\t\tBook Author     : %s %s\n",b.bauthorfn,b.bauthorln);
         fprintf(addbook,"\t\tBook Price      : P%.2f\n",b.bprice);
         fflush(addbook);
         fclose(addbook);
         }
    /*SHOW MESSAGE*/
    printf("\n\n\t\t\t    **** Record Added! ****");
    fflush(stdin);
    
    entry:
    printf("\n\n\t\tAdd new entry? [y/n]: ");
    scanf("%s",&a);
    if ((a=='y')||(a=='Y'))
    goto bookpurchase;
    else if ((a=='n')||(a=='N'))
    goto mainmenu;
    else
    system("CLS");
    printf("\n\n\n\n\n\n\n\t\t\t      * NEW BOOK ENTRY *");
    printf("\n\t\t---------------------------------------------");
    printf("\n\n\t\t Invalid Choice!!!\n\n");
    goto entry;
    }
    
    
    booklist:
    {
         system("CLS");
         printf("\n\n\n\t\t\t        * BOOK LIST *");
         printf("\n\t\t---------------------------------------------\n\n\t\t");
         system("type books.txt");
         printf("\n\n\t\t=> End of List!\n\n\t\t");
         system("pause");
         goto mainmenu;
    }
    
    qbook:
    {
        FILE *ft;
        int found = 0;
        int bno;
        int target;
        
       system("CLS");
       printf("\n\n\n\t\t\t     * QUERY BY BOOK *");
       printf("\n\t\t---------------------------------------------\n\n\t\t");
       
       ft= fopen("books.txt","r");
       if(ft==NULL)
          printf("File not found");
       else
           printf("\n\t\tEnter Book#: ");
           fflush(stdin);
           scanf("%d",&target);
           do
            {
    	     fscanf(ft,"%d %s %s %s %s %.2f",&bno,&b.btitle,&b.bversion,&b.bauthorfn,&b.bauthorln,&b.bprice);
    	     if(target==bno)
    	       {
    	         found =1;
               }
            }while(!feof(ft) && found == 0);
            if(found==1)
              {
                printf("\n\t\tBook#           : %d\n",bno);
                printf("\n\t\tBook Title      : %s\n",b.btitle);
                printf("\n\t\tBook Version    : %s\n",b.bversion);
                printf("\n\t\tBook Author     : %s %s\n",b.bauthorfn,b.bauthorln);
                printf("\n\t\tBook Price      : %.2f\n",b.bprice);
              }
            else
                {
                printf("\n\n\n\t\t=============================================");
                printf("\n\t\t\t\t Not Found!");
                fclose(ft);
                getch();
                }
                qbanother1:
                    printf("\n\n\t\tSearch Again? [y/n]: ");
                    scanf("%s",&a);
                         if ((a=='y')||(a=='Y'))
                         goto qbook;
                         else if ((a=='n')||(a=='N'))
                         goto mainmenu;
                         else
                           system("CLS");
                           printf("\n\n\n\t\t\t   * QUERY BY BORROWER *");
                           printf("\n\t\t---------------------------------------------\n\n\t\t");
                           printf("\n\n\t\t Invalid Choice!!!\n\n");
                         goto qbanother1;
                         goto mainmenu;
    }
    
    emptybookrecord:
    {
         system("CLS");
         printf("\n\n\n\n\n\n\t\t--------------------------------------------");
         printf("\n\n\t\t\t    Book Record Emptied!!\n\n");
         addbook=fopen("books.txt","w+");
         fclose(addbook);
         printf("\n\t\t--------------------------------------------\n\n\n\n\n\t\t\t");
         system("Pause");
         goto mainmenu;
    }
    
    
    exxit:
    {
         system("CLS");
         printf("\n\n\n\n\n\t\t\t     press any key to exit...");
         getch();
    }
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The way I'd do it is to sort the book records by book number, which is unique.

    Now, to make up a new book number, you just add 1 to the last book number in the list. Key thing is, when a book gets removed *don't remove the book number or it's record*, just zero out the name of the book. Your program can easily re-use those "deleted" book records, just by editing the data it has.

    This is the way databases work, as well. They keep a certain % of "deleted" records, so they can be re-used, later. Makes less work for the computers.

    To search quickly through a lot of records, you'll use the sorted by book number, disk file, or array, or linked list. (preferably an array). Then do a binary search through the book number, looking for it.

    I'll be glad to show you how to do a binary search, but you have to promise me you'll put that code in a function, and not use goto's!!


    here's a little program to whet your appetite.

    Code:
    #include <stdio.h>
    #define MAX 9
    
    int binarySearch(int array[], int target);
    void sortIt(int array[]);
    
    int main(void) {
      int num, i;
      int array[MAX] = {66,55,11,44,33,77,22,99,88};
      
      printf("\n\n\n What doubled integer do you want to search for? ");
      scanf("%d", &num);
      sortIt(array);
      i = getchar();  //just pauses the screen
      i = getchar();
      i = 0;
      i = binarySearch(array, num); //num is another name for "target"
      if(i < MAX) 
        printf("Your integer was found at array index %d", i);
      else
        printf("\n\n The integer %d was not found in the array", num);
    
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar();  //another pause before the console text window closes
      return 0;
    }
    int binarySearch(int array[MAX], int target) {
      int lo, mid, hi;
      lo = 0; 
      hi = MAX;
    
      while(lo <= hi) {
        mid = (lo + hi) / 2;
        if(array[mid] > target)
          hi = mid - 1;
        else if(array[mid] < target)
          lo = mid + 1;
        else
          return mid; //target was found, return the index to it
      }
      return MAX;  //target was not found
    
    }
    
    void sortIt(int array[]) {
      int i, j, temp;
      for(i = 0; i < MAX - 1; i++) {
        for(j = i + 1; j < MAX; j++) {
          if(array[i] > array[j]) {
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
          }
        }
      }
      printf("\n\n\n\n");
      for(i = 0; i < MAX; i++)
        printf(" %3d", array[i]);
    }
    This binary search is *VERY* fast. Any search will be fast with less than 1,000 things to search through. When the search involves 10's or 100's of thousands of items, (or more), then the binary search really comes into it's own.
    Last edited by Adak; 10-10-2009 at 11:58 AM.

Popular pages Recent additions subscribe to a feed