Thread: C Problem - Create a Personal Library

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    1

    C Problem - Create a Personal Library

    Hello, I am new to the forum. I need help in debugging the program as it does not produce the expected output and I can't figure out where goes wrong. I haven't completed the whole program and I am working on it part by part.

    Here is my code:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    struct book
    {
    	char *title;
    	char *author;
    	char *subject;
    };
    
    
    struct library
    {
    	struct book collection;
    	int num_books;
    	struct library *next;
    };
    
    
    typedef struct library lib;
    typedef lib *libPtr;
    
    
    void addBook(FILE *fPtr,libPtr *sPtr);
    void copybook(struct book *dest,struct book *source);
    void deleteBook(FILE *fPtr,libPtr *sPtr);
    void searchBook(FILE *fPtr,libPtr *sPtr);
    void listAuthor(FILE *fPtr,libPtr *sPtr);
    void listSubject(FILE *fPtr,libPtr *sPtr);
    
    
    int main()
    {
    	libPtr startPtr = NULL;
    	int choice;
    	FILE *fPtr;
    	fPtr = fopen("library.txt","r");
    	if (fPtr == NULL)
    	{
    		puts("File can't be loaded.\n");
    	}
    	else
    	{
    		while (!feof(fPtr))
    		{
    			fscanf(fPtr,"%d",&choice);
    			switch (choice)
    			{
    				case 1:
    					addBook(fPtr,&startPtr);
    					break;
    			}
    		}
    	}
    	fclose(fPtr);
    	return 0;
    }
    
    
    void addBook(FILE *fPtr,libPtr *sPtr)
    {
    	libPtr newPtr;
    	libPtr currentPtr;
    	libPtr previousPtr;
    	struct library *myLib;
    	myLib = (struct library *)malloc(sizeof(struct library));
    	myLib->collection.title = (char *)malloc(sizeof(50));
    	myLib->collection.author = (char *)malloc(sizeof(50));
    	myLib->collection.subject = (char *)malloc(sizeof(50));
    	fscanf(fPtr,"%s%s%s",myLib->collection.title,myLib->collection.author,myLib->collection.subject);
    	newPtr = malloc(sizeof(lib));
    	if (newPtr != NULL)
    	{
    		newPtr->collection = myLib->collection;
    		newPtr->next = NULL;
    		while (currentPtr != NULL)
    		{
    			previousPtr = currentPtr;
    			currentPtr = currentPtr->next;
    		}
    		if (previousPtr == NULL)
    		{
    			newPtr->next = *sPtr;
    			*sPtr = newPtr;
    		}
    		else
    		{
    			previousPtr->next = newPtr;
    			newPtr->next = currentPtr;
    		}
    	}
    	else
    	{
    		puts("No memory available.\n");
    	}
    }

  2. #2
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    Your "#include" are wrong arranged.

    C Problem - Create a Personal Library-includefalsch2018-12-14_175451-jpg

    Better:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    If correct no warnings but nevertheless it will not run:

    C Problem - Create a Personal Library-include2_2018-12-14_175726-jpg

    Also gcc -Wall -pedantic . . . shows no warnings or errors. Something is absolute wrong . . . but what?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you get rid of this typedef:
    Code:
    typedef lib *libPtr;
    It isn't wrong in itself, but generally a typedef of a pointer can make it more confusing to reason about the pointer, so it is usually better only to typedef a pointer when it is an opaque pointer, i.e., in that context the pointer will not be dereferenced or involved in pointer arithmetic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by laserlight View Post
    I suggest that you get rid of this typedef:
    Code:
    typedef lib *libPtr;
    It isn't wrong in itself, but generally a typedef of a pointer can make it more confusing to reason about the pointer, . . .
    Now I have seen my mistake: the file "library.txt" was missing. But with it it won't work still.

    "library.txt"

    Programming Books
    The Wait Group
    Computer science
    Maybe the reason for that is was @laserlight wrote.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    struct library
    {
        struct book collection;
        int num_books;
        struct library *next;
    };
    This makes no sense.
    You're saying each library consists of only one book.

    Think about it in terms of
    - a book (title, author, subject)
    - a shelf (a list of books of finite size)
    - a library (a list of shelves, as many as you need)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 37
    Last Post: 10-03-2014, 08:28 PM
  2. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  3. personal library
    By Aisthesis in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2010, 08:47 PM
  4. Personal problem
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 09-20-2003, 08:48 PM
  5. Help with Personal Problem...
    By ... in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 11-26-2002, 11:15 PM

Tags for this Thread