Thread: need a pro to look over and comment.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19

    need a pro to look over and comment.

    i have been working on a program to make a university library database. the assignment details can be found here Programming, just click on assignment 3 specifications. im looking for someone to look over what i have so far and give me some feedback on it. thank you in advance.

    Code:
    /*Program to maintain a database of book records in memory */
    #include <stdio.h>
    #include <conio.h>
    #define MAX_BOOKS 100
    
    /* Structure definitions */
    struct date
    {
      int day;
      int month;
      int year;
    };
    typedef struct date DATE;
    
    struct Library
    {
      int librarynumber;
      char author[26];
      char title[50];
      DATE dop;
      char ISBN[14];
      char borrowed[1];
    };
    typedef struct Library BOOK;
    
    /* Function prototypes */
    void add_an_book(BOOK []);
    void delete_an_book(BOOK []);
    void display_an_book(BOOK []);
    void display_book_details(BOOK *);
    void init_database(BOOK []);
    int search_database(BOOK [], int);
    int menu(void);
    
    
    void main()
    {
    	BOOK book[MAX_BOOKS];
    	int menu_choice;
    
    	init_database(book);
    
    	do
    	{
    		menu_choice = menu();
    
    		switch ( menu_choice )
    		{
    			case 1 :
    						add_an_book( book );
    						break;
    			case 2 :
    						delete_an_book( book );
    						break;
    			case 3 :
    						display_an_book( book );
    						break;
    		}
    	}
    	while ( menu_choice != 0) ;
    }
    
    
    /********************************************************************/
    /* Function : add_an_book()                                     */
    /*                                                                  */
    /*	Purpose  : Add an Books data to the database                */
    /*                                                                  */
    /*	Arguments: A pointer to the book array.                      */
    /********************************************************************/
    
    void add_an_book( BOOK book_array[] )
    {
    	int i=0;
    
    	/* Search the array for an empty position.
    		An empty position has a 0 for the employee number */
    	while ( book_array[i].librarynumber != 0 && i < MAX_BOOKS )
    	  i++;
    
    	if ( i == MAX_BOOKS )
    	  printf("\nSorry, the database is full\n");
    	else   				   /* Add the books details to the database. */
    	{
    	  printf( "\n\nlibrary Number (1 to 3 digits, except 0) : " );
    	  do
    		 scanf( "%3d",&book_array[i].librarynumber );
    	  while ( book_array[i].librarynumber <= 0 );
    
    	  printf( "\nBook author (Maximum 25 characters) : " );
    	  scanf( "%25s",book_array[i].author );
    	  printf( "\n       title (Maximum 50 characters) : " );
    	  scanf( "%50s",book_array[i].title );
    	  printf( "\nDate of publish\n" );
    	  printf( "     Day (1 or 2 digits) : " );
    	  scanf( "%2d",&book_array[i].dop.day );
    	  printf( "   Month (1 or 2 digits) : " );
    	  scanf( "%2d",&book_array[i].dop.month );
    	  printf( "    Year (1 or 2 digits) : " );
    	  scanf( "%2d",&book_array[i].dop.year );
    	  printf( "\nISBN (13 digits): " );
    	  scanf( "%13s",&book_array[i].ISBN );
    	  printf( "\nBorrowed? (y/n)\n" );
    	  scanf ("%1s",&book_array[i].borrowed );
    	}
    }
    
    
    /********************************************************************/
    /* Function : delete_an_book()                                  */
    /*                                                                  */
    /*	Purpose  : Delete an book from the database.                 */
    /*                                                                  */
    /*	Arguments: A pointer to the book array.                      */
    /********************************************************************/
    
    void delete_an_book( BOOK book_array[] )
    
    {
    
    	int library_number;
    	int pos;
    
    	/* An book is marked as "deleted" from the database
    		by placing a 0 in the library number.             */
    
    	/* First get the library number. */
    	printf("Book Number to Delete (1 to 3 digits, except 0) :");
    	do
    	  scanf( "%3d",&library_number );
    	while ( library_number <= 0 ) ;
    
    
    	/* Find the position in the database of this book. */
    	pos = search_database( book_array, library_number );
    
    	/* Have you come to the end of the database without finding the
    		library number?                                   */
    	if ( pos == MAX_BOOKS )  /* yes */
    	  printf( "This book is not in the database\n" );
    	else                       /* no  */
    	  {
    		 printf("Book %3d deleted", library_number);
    		 book_array[pos].librarynumber = 0;
    	  }
    }
    
    
    /********************************************************************/
    /* Function : display_an_book()                                 */
    /*                                                                  */
    /*	Purpose  : Display an book details in the database.          */
    /*                                                                  */
    /*	Arguments: A pointer to the book array.                      */
    /********************************************************************/
    
    void display_an_book( BOOK book_array[] )
    {
    	int library_number;
    	int pos;
    
    	/* Get the employee number. */
    	printf("Library Number to Display (1 to 3 digits, except 0):" );
    	do
    	  scanf( "%3d",&library_number );
    	while ( library_number <= 0 );
    
    
    	/* Find the position in the database of this book. */
    	pos = search_database( book_array, library_number );
    
    	/* Does the book exist? */
    	if ( pos == MAX_BOOKS )  /* no */
    	  printf( "This book is not in the database\n" );
    	else                       /* yes - display the details */
    	  display_book_details( &book_array[pos] );
    }
    
    
    /********************************************************************/
    /* Function : display_book_details()                            */
    /*                                                                  */
    /*	Purpose  : Display book details.                             */
    /*                                                                  */
    /*	Arguments: A pointer to the book details.                    */
    /********************************************************************/
    
    void display_book_details( BOOK *ptr )
    {
    	printf("\n\n");
    	printf("Library Number: %d\n",ptr->librarynumber);
    	printf("author        : %s\n",ptr->author);
    	printf("title        : %s\n",ptr->title);
    	printf("date of publish  : %2d/%2d/%2d\n",ptr->dop.day,ptr->dop.month,ptr->dop.year);
    	printf("ISBN     : %s\n", ptr->ISBN);
    	printf("Borrowed    : %s\n", ptr->borrowed);
    }
    
    
    /********************************************************************/
    /* Function : init_database()                                       */
    /*                                                                  */
    /* Purpose  : Initialise all book numbers in database to 0.     */
    /*				  An book number of 0 is used to denote an empty    */
    /*				  position in the array.                                */
    /*                                                                  */
    /*	Arguments: A pointer to the book array.                      */
    /********************************************************************/
    
    void init_database( BOOK book_array[] )
    {
    	int i;
    
    	for ( i=0; i < MAX_BOOKS; i++ )
    	  book_array[i].librarynumber = 0;
    }
    
    /********************************************************************/
    /* Function : menu()                                                */
    /*                                                                  */
    /*	Purpose  : Display menu of options & wait for user to choose one */
    /*                                                                  */
    /*	Arguments: None.                                                 */
    /*                                                                  */
    /*	Returns  : The menu choice.                                      */
    /********************************************************************/
    
    int menu(void)
    {
    	int choice;
    
    	/* Display the menu. */
    	printf("\n\n 1. Add an book\n\n");
    	printf(" 2. Delete an book\n\n");
    	printf(" 3. Display an book\n\n");
    	printf(" 0. Quit\n\n");
    	printf( "Please enter your choice (0 to 3) " );
    
    	/* Get the option. */
    	do{
    		scanf( "%d", &choice );
            fflush(stdin);                // move this line to see the effect.
        }
    	while ( choice <0 || choice > 3 );
    
    	return (choice);
    }
    
    /********************************************************************/
    /* Function : search_database()                                     */
    /*                                                                  */
    /*	Purpose  : Search the database for an book number.           */
    /*                                                                  */
    /*	Arguments: A pointer to the book array and an book number*/
    /*                                                                  */
    /*	Returns  : The array index corresponding to the position in the  */
    /*				  database of the book record.                      */
    /********************************************************************/
    
    int search_database( BOOK book_array[], int lib_number )
    {
    	int i = 0;
    
    	/* Search for the book number in the database starting at the
    		first record and continuing until the book number is found
    		or until the end of the database is reached.                */
    
    	while ( i < MAX_BOOKS && book_array[i].librarynumber != lib_number )
    		i++;
    
    	 return (i);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The return type of main should not be void:
    Code:
    void main()
    {
    SourceForge.net: Void main - cpwiki
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    ah right, i had been trying to get rid of that warning for ages, thanks. also, could someone help me figure out how to get the program to accept spaces whn entering author and title?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ciaran789 View Post
    could someone help me figure out how to get the program to accept spaces whn entering author and title?
    Don't use scanf, use fgets() for those fields. Or have a closer look at your scanf() documentation, eg try:
    Code:
    scanf("%25[a-zA-Z -]",book_array[i].author);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    i gave what you said a try with scanf but when i entered an author name it skipped title and went straight to date of publish.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Ciaran789 View Post
    i gave what you said a try with scanf but when i entered an author name it skipped title and went straight to date of publish.
    Ah, that's because of the newline. You could try it this way:
    Code:
    scanf("%25[a-zA-Z -]%*c",book_array[i].author);
    Altho that may not be perfect in all circumstances (eg, if the user enters "Bob Smith!").

    You may want to read this:
    STDIN pitfalls
    My recommendation for user input is at the bottom of the page. This might mean writing your own short function (which uses fgets and scanf internally) in place of scanf(). Your code is pretty well organized, so I don't think it would be much trouble for you to swap something like that in. This gives you the opportunity to write a fairly bulletproof form of validation.
    Last edited by MK27; 04-25-2010 at 07:00 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Nov 2009
    Location
    Ireland
    Posts
    19
    i went back and tried fgets instead of scanf, it told me that there were too few arguments to use fgets.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmm. What documentation are you using?

    fgets takes three args.
    1) the buffer to place the data into, eg, book_array[i].author
    2) the size of that buffer, in this case 26, since fgets takes '\0' into account.
    3) the stream to read from, in this case stdin

    Do read the last half of post #6, above, using input validation function(s) might be considered a real improvement to your program. You probably want two, one for ints and one for strings. "Validation" would mean you could check to make sure the input is okay. So if you have a function:
    Code:
    int getstring (char *buffer, int size);
    The return value indicates whether a valid string was read in (you can tailor that to your purposes in the function), then you can use a loop:
    Code:
    printf( "\nBook author (Maximum 25 characters) : " );
    while (!getstring(book_array[i].author, 26))
    	  printf("Invalid. Try again: ");
    How getstring() works will be clearer if you read that link I posted. ! is negation, so this loop will only happen if getstring() returns 0.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed