C problem - Create a Personal Library

This is a discussion on C problem - Create a Personal Library within the C Programming forums, part of the General Programming Boards category; Code: struct book shelf; The shelf stores one book? Code: ifp = fopen("library.txt", "r"); You should be checking to see ...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct book shelf;
    The shelf stores one book?
    Code:
    	ifp = fopen("library.txt", "r");
    You should be checking to see if your file failed to open or not before you try to use it.
    Code:
    	fscanf(ifp, "%d", &cases);
    You should probably be reading entire lines at a time into a buffer with something like fgets instead, simply because it's cleaner to do so.
    Code:
    		fscanf(ifp, "%d", &instruct);
    You should really be checking your return values to see if you get what you expect or not.

    You might consider using a switch:
    Code:
    		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);
    			
    		}
    To...
    Code:
            switch( instruct )
            {
                case 1: addBook(ifp, &myLib, shelf); break;
                case 2: removeBook(ifp, &myLib, &shelf); break;
                case 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);
    			} break;
                case 4: author_list(ifp, myLib.collection, &shelf, &myLib); break;
                case 5: genre_list(ifp, myLib.collection, &shelf, &myLib); break;
                default: printf( "Invalid instruct: %d\n", instruct );
    		}
    Not always the preferred method, but suits this nicely.
    Code:
    //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);
    }
    I would probably separate my file reading functions from my book adding functions. Also, you seem to have a great deal of that mixed up. You're passing struct book b by value, not pointer, so anything you do here is lost anyway--plus, this is just a single book, not a library. You're getting those confused again.


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

  2. #17
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    yes...for some reason once we started functions
    in my class...I went to hell. It is so confusing for me,
    and I don't even want to do this as a career. I wanted to
    do game design art...but I have to take C for my major

    Thanks for helping though guys

  3. #18
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Harliqueen View Post
    yes...for some reason once we started functions
    in my class...I went to hell. It is so confusing for me,
    and I don't even want to do this as a career. I wanted to
    do game design art...but I have to take C for my major

    Thanks for helping though guys
    Don't give up now! Finish this project. It won't take you that long and you will feel that your work has been rewarding. Everyone asks for help sometimes even experienced programmers.

  4. #19
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Ok...well I changed the coding to what was suggested and now I have a problem with the

    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);
    	}
    }
    which I remember you guys saying was wrong

  5. #20
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well it depends what you are trying to do here. The main problem is that as it is, you are iterating around i and then printf-ing something completely unrelated to the iterator, so basically printing the same thing over and over again. What you want is to pass the book collection and print each book in that collection. (i.e. book[i]).

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Functions are easy if you remember the difference between pointers and non-pointers: they are in effect the same thing.

    Everything in C is a value. An 'int' holds a value that is just a number. A pointer to an integer also holds a number as its value, but that number represents a spot in memory.

    How does this relate to functions? Functions take (optionally) arguments (read: values). This can mean two things:[code]void foo( int x );[code]This function takes an integer value as its argument. What does that mean?
    Code:
    int bar = 5;
    ...
    foo( bar ); /* take the value that bar has, in this case five, and pass it to foo */
    ...
    foo( 5 ); /* take the value 5 and pass it to foo */
    In both of those function calls, the program copies (duplicates) the value passed as its argument, and sticks it in the variable x that is the argument to this function foo. In both cases, x is five.

    The only difference is if we make that "value" be a memory address or not. And really the only reason that seems to matter is because of how you can get away with using it. Say we have a pointer:
    Code:
    int *p;
    And it points some place.
    Code:
    p = &someSpotThatRepresentsAnInt;
    If we actually knew what that address was, we could pass that value to the function:
    Code:
    void bar( int *x );
    ...
    bar( p ); /* pass the value stored in p to bar */
    ...
    bar( (int *) 0x1234 ); /* pass some value that happens to be a valid memory address to p instead */
    In both cases, just like when you don't use a pointer, the only thing that actually happens to arrive at bar is a value. We just treat these values as if they are actual variables by dereferencing them. Basically we say "Hey, compiler, when I do this, go to this value, as a memory address, and give me what's there!"

    That's really all you have to remember. Everything is passed as a value. Pointers are just like anything else, except you get to dereference them and say "give me what's there" or "go here and put this to that".


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

  7. #22
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Quote Originally Posted by claudiu View Post
    Well it depends what you are trying to do here. The main problem is that as it is, you are iterating around i and then printf-ing something completely unrelated to the iterator, so basically printing the same thing over and over again. What you want is to pass the book collection and print each book in that collection. (i.e. book[i]).
    so i basically need to get rid of the int i

  8. #23
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Harliqueen View Post
    so i basically need to get rid of the int i
    No. You are trying to print a whole library right? A library is made of a number of books. What you want to print in that function is the title and author of each book from what I gather. Right now you are printing the author and title of a single book, the one you pass as parameter. What you need to do is pass an array of books which is basically your myLib struct. Then iterate through the books[] in myLib using the i counter and print the info for each of them.

    Code:
    for(i = 0;i < MAX_LENGTH;i++)
    
     printf("%s %s", myLib->collection[i]->author, myLib->collection[i]->title);

  9. #24
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Oh yea...ok I see what you mean now

  10. #25
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Code:
    void printLib(struct book *l, int length)
    should that be changed to something like this

    Code:
    void printLib(struct library *l, int MAX_LENGTH)

  11. #26
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Harliqueen View Post
    Code:
    void printLib(struct book *l, int length)
    should that be changed to something like this

    Code:
    void printLib(struct library *l, int MAX_LENGTH)
    Correct. Only you don't actually need to pass MAX_LENGTH since that is part of a #define which makes it available throughout your entire code. The library pointer will sufice. Then you can call the function like so:

    Code:
    printLib(&myLib);
     /* myLib is of type struct library so &myLib is the address of that struct, thus of type struct library* */

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Harliqueen View Post
    Code:
    void printLib(struct book *l, int length)
    should that be changed to something like this

    Code:
    void printLib(struct library *l, int MAX_LENGTH)
    If you trust your data, you don't even need that.
    Code:
    void printLib( struct lib *l )
    {
        if( l != NULL )
        {
            size_t x;
            for( x = 0; x < l->librarysize; x++ )
            {
                print out the parts of this book
            }
        }
    }
    If you don't, add in a:
    Code:
            for( x = 0; x < l->librarysize && x < LIBRARYSIZE; x++ )
    Since you've already got a #define for it some place.


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

  13. #28
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    ok...I changed it to this
    Code:
    void printLib(struct lib *l ) {
    	int i;
    	for(i = 0;i < MAX_LENGTH;i++)
        printf("%s %s", myLib->collection[i]->author, myLib->collection[i]->title);
    	}
    }
    but its being retarded and not working

  14. #29
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It might actually be myLib->collection[i].author and myLib->collection[i].title because collection[i] is not a pointer.

  15. #30
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also my mistake, you have to iterate until myLib->num_books since that is your book counter for the library. MAX_LENGTH is just the total number of possible books.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 10: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, 10:14 AM
  4. Graphics.h library problem
    By ChronoSquare in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2006, 10:18 AM
  5. Problem with Apache XML C++ Parser library
    By gandalf_bar in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2004, 10:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21