Thread: C problem - Create a Personal Library

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    15

    C problem - Create a Personal Library

    Hello, I am new to the forums and I just have a quick question.

    For the most part the program does what it needs to (which is print off
    the list of what was added and what was removed based on my
    file)

    but I cannot figure out why it is crashing

    here is my code

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_LENGTH 40
    #define MAX_BOOKS 1000
    
    //Make struct for book
    struct book {
    	char title[MAX_LENGTH];
    	char author[MAX_LENGTH];
    	char subject[MAX_LENGTH];
    };
    
    //Make struct for library
    struct library {
    	struct book collection[MAX_BOOKS];
    	int num_books;
    };	
    
    //Create each of the functions
    void copybook(struct book *dest, struct book *source);
    void addBook(FILE *ifp, struct library *thislib, struct book b);
    void removeBook(FILE *ifp, struct library *thislib, struct book *b);
    void author_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib);
    void genre_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib);
    int searcher(struct book *b, struct book *l, struct library *thislib);
    void printLib(struct book *l, int length);
    
    
    
    int main (void) {
    	
    	struct book shelf;
    	struct library myLib;
    	
    	FILE *ifp;
    	ifp = fopen("library.txt", "r");
    	
    	int cases;
    	fscanf(ifp, "%d", &cases);
    	
    	int instruct, i;
    	
    	for (i=0; i<cases; i++) {
    		fscanf(ifp, "%d", &instruct);
    		if (instruct == 1) {
    			addBook(ifp, &myLib, shelf);
    		} else if (instruct == 2) {
    			removeBook(ifp, &myLib, &shelf);
    		} else if (instruct == 3) {
    			
                int k = 0;
    			fscanf(ifp, "%s", &shelf.title[k]);
    			if (searcher(&shelf, myLib.collection, &myLib) == 1) {
    				printf("The book %s is currently in the library.\n\n", shelf.title);
    			} else {
    				printf("The book %s is NOT currently in the library.\n\n", shelf.title);
    			}
    		} else if (instruct == 4) {
    			author_list(ifp, myLib.collection, &shelf, &myLib);
    		} else {
    			genre_list(ifp, myLib.collection, &shelf, &myLib);
    			
    		}
    	}
    	fclose(ifp);
    	system("PAUSE");
        return 0;
    }
    
    //Function to copy book
    void copybook(struct book *dest, struct book *source) {
    	strcpy(dest->title, source->title);
    	strcpy(dest->author, source->author);
    	strcpy(dest->subject, source->subject);
    }
    
    //Function to add book
    void addBook(FILE *ifp, struct library *thislib, struct book b) {
    	fscanf(ifp, "%s%s%s", &b.title[thislib->num_books], &b.author[thislib->num_books], &b.subject[thislib->num_books]);
    	copybook(thislib->collection, &b);
    	thislib->num_books++;
    	printf("The book %s has been added to the library.\n\n", b.title);
    }
    
    //Function to remove book
    void removeBook(FILE *ifp, struct library *thislib, struct book *b) {
    	int i;
    	fscanf(ifp, "%s", &b->title[i]);
    	for (i=0; i<thislib->num_books; i++) {
    		if (searcher(thislib->collection, b, thislib) == 1) {
    		}
    	}
    	thislib->num_books--;
    	printf("The book %s has been removed from the library.\n\n", b->title);
    }
    
    //Function to search
    int searcher(struct book *b, struct book *l, struct library *thislib) {
    	int i;
    	for (i=0; i<thislib->num_books; i++) {
    		if (strcmp(b->title, l->title) == 0) {
    			return 1;
    		}
    	}
    	return 0;
    }
    
    //Function to scan author
    void author_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib) {
    	int i, count;
    	fscanf(ifp, "%s", &b->author[i]);
    	printf("List of all books by %s\n", b->author);
    	for (i=0; i<thislib->num_books; i++) {
    		if (strcmp(b->author, l->author) == 0) {
    			count++;
    			printf("%s\n", l->title);
    		}
    	}
    }
    
    //Function to scan genre
    void genre_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib) {
    	int i, count;
    	fscanf(ifp, "%s", &b->subject[i]);
    	printf("List of all books on %s\n", b->subject);
    	for (i=0; i<thislib->num_books; i++) {
    		if (strcmp(b->subject, l->subject) == 0) {
    			count++;
    			printf("%s\n", l->title);
    		}
    	}
    }
    
    //Funtion to print list
    void printLib(struct book *l, int length) {
    	int i;
    	for (i=0; i<length; i++) {
    		printf("%s\n%s\n%s\n", l->title, l->author, l->subject);
    	}
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you at least indicate where it is crashing? Have you tried using a debugger? Even if you can't pin point the location at least describe how you are attempting to run the program, what input you are providing, etc.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Basically it is just running that exact code and using a file i made called library.txt as the input in dev c++, it prints off exactly what it is suppose to output...but 5 seconds later in suddenly does the "Library.c program has suddenly stopped working windows is checking for a solution" so I am not exactly sure where the problem is

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Hmm. It's probably some memory issue, but we will have to pin point where the problem occurs. Can you post the content of your input file here as well, so we can see the control path more clearly?

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    yep

    11
    1 TOM_SAWYER TWAIN FICTION
    1 Intro_to_C_Programming King Computer_Science
    3 Huckleberry_Finn
    1 Huckleberry_Finn TWAIN FICTION
    1 Guns,Germs,Steel Diamond Anthropology
    1 Java5.0 Cohoon Computer_Science
    4 Twain
    5 computer_science
    5 Computr_Science
    2 Intro_to_C_Programming
    4 King


    this is in the input file library.txt

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Aight. Let me give it a try on my machine. I'll get back to you.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    thanks

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    //Funtion to print list
    void printLib(struct book *l, int length) {
    	int i;
    	for (i=0; i<length; i++) {
    		printf("%s\n%s\n%s\n", l->title, l->author, l->subject);
    	}
    }
    What is this supposed to do? It looks like you just print the same thing over and over again. What's the purpose of that? Did you mean this to print your library, and take a library as an argument instead?

    Edit:

    And this...
    Code:
    //Function to scan author
    void author_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib) {
    	int i, count;
    	fscanf(ifp, "%s", &b->author[i]);
    	printf("List of all books by %s\n", b->author);
    	for (i=0; i<thislib->num_books; i++) {
    		if (strcmp(b->author, l->author) == 0) {
    			count++;
    			printf("%s\n", l->title);
    		}
    	}
    }
    Should really be something like this:
    Code:
    for( i = 0; i < thislib->num_books; i++ )
        if( strcmp( b->author, thislib->collection[ i ]->author ) )
            ...
    You are passing a library, but never actually doing anything with it. Or maybe...
    Code:
    struct book *p = thislib->collection;
    for( i = 0; i < thislib->num_books; i++, p++ )
        if( strcmp( b->author, p->author ) == 0 ) ...
    Quzah.
    Last edited by quzah; 04-20-2010 at 10:35 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Here is what I get in my console when running your code:

    The program doesn't crash however there are some problems with your string manipulation.

    Code:
    The book TOM_SAWYER has been added to the library.
    
    The book AIntro_to_C_Programming has been added to the library.
    
    The book Huckleberry_Finn is NOT currently in the library.
    
    The book HuHuckleberry_Finn has been added to the library.
    
    The book HucGuns,Germs,Steel has been added to the library.
    
    The book HuckJava5.0 has been added to the library.
    
    List of all books by wain
    List of all books on omputer_science
    List of all books on omputr_Science
    The book ntro_to_C_Programming has been removed from the library.
    
    List of all books by ing
    
    Process returned 0 (0x0)   execution time : 0.046 s
    Press any key to continue.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Mine didn't even get that far
    my friend is doing the same code...and I think
    his isn't even showing two of the lines

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As quzah indicated your print lib function is wrong. Also note that the strings are not correctly formed, presumably because of using %s in fscanf with the spaces that separate the inputs.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Ok....so fix the spaces around the %s...I am not sure exactly how to fix lib function
    I am quite stupid when it comes to programming
    Last edited by Harliqueen; 04-20-2010 at 10:35 PM.

  13. #13
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    your max length is 40 and if i counted right one of your titles is longer than 40.
    -- Will you show me how to c++?

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    I tried to change the max length...and it crashed without printing anything out

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You have multiple bugs throughout your code. Another bug is in your remove_book function. You declare i, don't initialize it and then attempt to read title[i]. Solve this and then post your newly updated code so we can keep improving it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 09:06 AM
  2. personal library
    By Aisthesis in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2010, 08:47 PM
  3. Problem calling external method from within a shared library
    By Major Tom in forum C++ Programming
    Replies: 0
    Last Post: 04-21-2007, 09:14 AM
  4. Graphics.h library problem
    By ChronoSquare in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2006, 09:18 AM
  5. Problem with Apache XML C++ Parser library
    By gandalf_bar in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2004, 09:42 AM