Thread: Searching and matching strings: segmentation fault

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    7

    Searching and matching strings: segmentation fault (EDIT: code posted!)

    I am programming a simulation of a library catalogue and am using a switch statement for selections of types of data to be searched. searching year, edition, and price are all working just fine. however, searching book name, author, and publisher receive segmentation faults after they have been found. I suspect its somewhere in my while loop but I have yet to find it.

    i should mention that i am reading the data from a separate text file. when searching the first entry in the text file, that is, its book name, i receive a segmentation fault immediately. however the other books show up, but then get a segfault error.

    When searching another aspect of the first file, such as edition, the info comes up, but its missing the book name at the beginning. its very confusing and difficult to explain, but i have attached both files to this post. if there is a genius out there who can give me some insight within the next couple of hours, i would be greatly in your debt!


    EDIT: I have decided that for ease to you programmers out there, that I will post all the code and the file. There isn't really that much I suppose...


    Code:
    #include <stdio.h>
    
    struct record{
    	
    		char book_name[50];
    		char author[50];
    		char publisher[50];
    		int year;
    		int edition;
    		float price;
    };
    /*
    void add_record(char b_name,char author, char publisher, int year, int
    edition, float price)
    {
    */
    main(){
    	FILE *fp;
    	struct record r[100];
    	fp=fopen("file1.txt","r");
    	char category;      
            int counter=0;
            char book_search,author_search,publisher_search;
    	int i=0;
    	int year_search,edition_search;
    	float price_search;
    /*	
    	while(counter<2){
    	printf("Enter the name,author,publisher,year: ");
      */      
    
        while(    	
    
    	fscanf (fp,"%s%s%s%d%d%f",
    		r[counter].book_name,
    		r[counter].author,
    		r[counter].publisher, 
    		&(r[counter].year),
    		&(r[counter].edition),
    		&(r[counter].price))==6
    	 )
    		
    		{
    			counter++; 	
    		}
    /*
      	printf(" the first record is %s\t%s\t%s\t%d\t%d\t%f\n"
              ,r[0].book_name,r[0].author,r[0].publisher,
              r[0].year, r[0].edition, r[0].price);
     
      	printf(" the fourth author is %s\n" , r[3].author);
    	printf(" The number of records is %d \n" , counter);
     */ 
    
    
    	printf("Type b,a,u,y,e,or p to search for book,author,publisher,year, edition, or price:  " );
            scanf("%s", &category);
            
    	switch(category){
    	case 'b':
    		 
    		printf("Type the name of the book: ");
       	        scanf("%s", &book_search);	
    		while(i<counter){
    			
    			if(strcmp( r[i].book_name,&book_search)==0){
    							
    				printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
      				,r[i].book_name,r[i].author,r[i].publisher,
      				r[i].year, r[i].edition, r[i].price);
    			}
    			i++;
    				}
    			i=0;
    			break;
    	
    	case 'a':
    	
    	printf("Type the author of the book: ");
    	scanf("%s", &author_search);
    
      		while(i<counter){
    
              if(strcmp(&author_search, r[i].author)==0){
    
                      printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
                      ,r[i].book_name,r[i].author,r[i].publisher,
                      r[i].year, r[i].edition, r[i].price);
              }
              i++;
                      }
              i=0;
    	break;
    	
    	case 'u':
    	printf("Type the publisher of the book: ");
    	scanf("%s", &publisher_search);
    
             while(i<counter){
    
       	  if(strcmp(&publisher_search, r[i].publisher)==0){
    
               printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
               ,r[i].book_name,r[i].author,r[i].publisher,
               r[i].year, r[i].edition, r[i].price);
          	}
       	i++;
             	 }
       	i=0;	
    	break;
    
    	case 'y':
    	printf("Type the year of the book: ");
    	scanf("%d", &year_search);
    
      	while(i<counter){
    
       	if(year_search==r[i].year){
    
        		printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
        		,r[i].book_name,r[i].author,r[i].publisher,
        		r[i].year, r[i].edition, r[i].price);
     			}
     		i++;
              	}
     		i=0;
    	break;
    
    	case 'e':
    	printf("Type the edition of the book: ");
    	 scanf("%d", &edition_search);
    
     	while(i<counter){
    
     	if(edition_search==r[i].edition){
    
             printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
             ,r[i].book_name,r[i].author,r[i].publisher,
             r[i].year, r[i].edition, r[i].price);
                     }
             i++;
             }
             i=0;
     	break;
    
    
    	case 'p':
    	printf("Type the price of the book: ");
    	 
      	scanf("%f", &price_search);
    
     	while(i<counter){
    
    	 if(price_search==r[i].price){
    
     	 printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
      	 ,r[i].book_name,r[i].author,r[i].publisher,
     	 r[i].year, r[i].edition, r[i].price);
              }
      i++;
      }
      i=0;
    
    	break;	
    }	
    
    
    fclose(fp);
    	
    
            
    		
    
    }

    and here is what is contained in the text file:

    Code:
    River James pub1 1980 4.50
    Lake John pub2 1981 6.50
    Ocean Luke pub3 1982 7.65
    Pond Peter pub4 1983 8.91
    Water Jesus pub5 1985 4.75
    Last edited by Smola; 07-06-2005 at 06:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM