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);
}