Thread: Search function

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    24

    Search function

    Hi guys this program needs to have two options, one where the user enters an ISBN number and then it displays the book which has the ISBN number the user entered and the second option is the exact same except the user enters a title instead. My teacher says i should use a string compare function but i honestly have no idea how to do this can someone please point me in the right direction.

    This is my program so far...
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define SIZE 40
    /* global variables */			
    typedef struct
    {
      char ISBN[20];
      char Author[SIZE];
      char Title[SIZE];
    }record_structure;  
    
    FILE *record_file;
    int choice;
    record_structure record_array[8];
    int num_records;
    
    /* function prototypes */
    void intro();
    void menu();
    void close();
    void add_book();
    void search_for_isbn();
    void search_for_title();
    void display_all_records();
    void screen_pause();
    
    /*******************************************************************************************/
    
    int main()
    {
    	intro();
    	do
    	{
    		menu();
    	}
    	while (choice!=5);
    	close();
        return 0;
    }/* end of main function */
    
    /*******************************************************************************************/
    
    void intro()
    {
    	system("cls");
    	printf("\n\n\n\n\n\n\tWelcome to Forth Valley's Collection of Technical Books");
    	screen_pause();
    }/*end of intro function*/
    
    /*******************************************************************************************/
    
    void add_book()
    {
    	record_file = fopen("C:\\bookfile.bin","wb");
        
        system("cls");
        
        printf( "\n\nAdd book details");
        
        for(num_records=0;num_records<=7;num_records++)
             {
               printf( "\n\nISBN: ");
               fflush(stdin);
               fgets(record_array[num_records].ISBN, 20, stdin);
               printf( "\n\nAuthor: ");
               fflush(stdin);
               fgets(record_array[num_records].Author, SIZE, stdin);
               printf( "\n\nTitle: ");
               fflush(stdin);
               fgets(record_array[num_records].Title, SIZE, stdin);
               printf( "------------------------------------" );
               
               fwrite(&record_array[num_records],sizeof(record_structure),1,record_file);
             
              } 
               printf( "\n\nYou have succesfully inputed the details of 8 books" );
               fclose(record_file);
        
        screen_pause();
    }/* end of dummy menu option 1 function */
    
    /*******************************************************************************************/
    
    void search_for_isbn()
    {
    	system("cls");
    	printf("Search for ISBN ");
    	screen_pause();
    }/* end of dummy menu option 2 function */
    
    /*******************************************************************************************/
    
    void search_for_title()
    {
    	system("cls");
    	printf("Search for a book by title ");
    	screen_pause();
    }/* end of dummy menu option 3 function */
    
    /*******************************************************************************************/
    
    void display_all_records()
    {
    	record_file = fopen("C:\\bookfile.bin","rb");
        
        system("cls");
        
        printf("\n\t\t\tDetails of all books");
    	printf("\n\n\n ID NO\t\tISBN\t\tAuthor\t\tTitle");
    	
    	num_records=0;
    	fread(&record_array[num_records],sizeof(record_structure),1,record_file);
    	
    	while(!feof(record_file))
    	{	
    		printf("\n%3i  %18s  %35s %55s ",num_records,record_array[num_records].ISBN,
    			record_array[num_records].Title,record_array[num_records].Author);
    		fflush(stdin);
    		num_records++;
    		fread(&record_array[num_records],sizeof(record_structure),1,record_file);
    	}
    	
    	screen_pause();
    	
        fclose(record_file);
    }
    
    /*******************************************************************************************/
    
    void close()
    {
    	system("cls");
    	printf("\n\n\n\n\n\n\tThank you for using this program");
    	screen_pause();
    }/* end of close function */
    
    /*******************************************************************************************/
    
    void menu()
    {
    	system("cls");
    	printf("\n\nMENU");
    	printf("\n\n1 - Add new entry: ");
    	printf("\n2 - Book search by ISBN: ");
    	printf("\n3 - Book search by title: ");
    	printf("\n4 - Show details off all books stored: ");
    	printf("\n5 - Exit Program:");
    	
        printf("\n\nEnter your choice ");
        scanf("%i", &choice);
        while((choice<1)||(choice>5))
        {
         printf("\n\nYou eneterd an invalid number please enter a valid number");
         printf("\n\n\tEnter your choice: ");
         scanf("%i", &choice);
         }
           
    	switch(choice)
    	{
    		case 1 : add_book();break;
    		case 2 : search_for_isbn(); break;
    		case 3 : search_for_title(); break;
    		case 4 : display_all_records() ; break;
    		case 5 :  break;
    	}
    }/* end of menu function */
    
    /*******************************************************************************************/
    
    void screen_pause()
    {
    	printf("\n\n\tPress any key to continue.....");
    	getch();
    }/* end of screen_pause function */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I assume that since you have a teacher, you have some sort of book? Go read the section on arrays, then the section on strings.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    24
    He hasn't given us a book yet but he has given us a sample string compare program
    but i really don't know where to start off.

    Code:
    /* Sample string comparison program*/
    /* Stored in Samples Drive in C:\C Programs\String Compare folder */
    /* Author VG */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char name1[10];
    char name2[10];
    
    void main()
    {
         printf("Enter name 1");
         fflush(stdin);
         gets(name1);
         
         printf("Enter name 2");
         fflush(stdin);
         gets(name2);
         
         /*compare two strings using strcomp function, function returns 0 if true */
         if(strcmp(name1, name2)==0)
         {     
               printf("SAME");
         }/* end of if */
         else
         {
             printf("DIFFERENT");
         }/* end of else*/
         
         getch();
    } /* end of main*/

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    74
    you have to go through your array that contains the data, and compare the string that you want to search, to the one you already have.

    so a loop to go through the array and use the function strcmp untill you get a match.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    24
    Am I on the correct path with this noobiept?

    Code:
    char isbn_number[20]
    
    void main()
    {
         printf("Please enter ISBN number: ");
         fflush(stdin);
         gets(isbn_number);
         
         
         
         /*compare two strings using strcomp function, function returns 0 if true */
         if(strcmp(isbn_number, record_array[num_records])==ISBN)
         {     
               printf("\n %s record_array[num_records].ISBN");
         }/* end of if */
         else
         {
             printf("No match");
         }/* end of else*/
         
         getch();
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - Missing headers, you need the correct #include to use the function.
    2 - void main() should be int main( void )
    3 - fflush(stdin) - read the FAQ on it
    4 - gets - read the FAQ on it
    5 - strcmp returns zero for match < 0 or > 0 for differences
    6 - return 0; at the end of the main function


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Attempt to write function search()
    By elsewhere in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 08:18 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM