I cant seem to get this to do what I want, which is to check for duplicate ISBN's and if a duplicate is there return the error message, can anyone help??
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char ISBN[20];
char Author[30];
char Title[50];
}
Library_Book;
FILE * bookfile;
Library_Book Books;
Library_Book NewBooks;
int choice=0;
void Menu (void);
void Add_Book (void);
void List_Books (void);
void Search_ISBN (void);
void Search_Title (void);
int main (int argc, char *argv[])
{
{
Menu();
}
while (choice != 5);
system("PAUSE");
return 0;
}
/******************************************************************************/
/* Menu Function */
void Menu(void)
{
int choice=0;
system ("cls");
/* Displays Menu */
printf("******************************************************\n");
printf("* LIBRARY MENU *\n");
printf("* *\n");
printf("* 1 Add a book to file *\n");
printf("* 2 List of all books on file *\n");
printf("* 3 Search for a book using an ISBN number *\n");
printf("* 4 Search for a book using the title *\n");
printf("* 5 Quit *\n");
printf("* *\n");
printf("******************************************************\n");
printf("* Please select your choice 1 to 5 *\n");
printf("******************************************************\n");
scanf ("%i", &choice);
/* Switch to take user to correct option from menu */
switch (choice){
case 1: Add_Book();
break;
case 2: List_Books();
break;
case 3: Search_ISBN();
break;
case 4: Search_Title();
break;
case 5: exit(EXIT_SUCCESS);
break;
default: printf("Incorrect selection, please select your choice 1 to 5.\n");
getchar();
system ("pause");
}
}
/*Function to add a book */
void Add_Book (void)
{
int found = 0;
system ("cls");
bookfile = fopen("BOOK.DAT","ab");
if (bookfile == 0)
{
printf("An error occurred whilst opening the file.\n");
}
{
printf ("Please enter ISBN or q to quit.\n");
scanf ("%s", &Books.ISBN);
strcmp (Books.ISBN,NewBooks.ISBN);
}
/* Do the following until user wishes to quit */
while ((tolower(Books.ISBN[0]) != 113)&&(strcmp (NewBooks.ISBN,Books.ISBN)==1))
found =0;
{
printf ("Please enter Author.\n");
fflush(stdin);
gets (Books.Author);
}
{
printf ("Please enter the title of the book.\n");
fflush(stdin);
gets (Books.Title);
}
fwrite (&Books,sizeof(Books),1,bookfile);
/*Error message for ISBN if already on file */
strcmp (NewBooks.ISBN,Books.ISBN);
while (strcmp (NewBooks.ISBN,Books.ISBN)==1)
{
printf ("Book is already held on file, Please Enter another ISBN\n");
scanf ("%s", &Books.ISBN);
found =1;
}
printf ("Please enter ISBN or q to quit.\n");
scanf ("%s", &Books.ISBN);
fclose (bookfile);
return Menu();
}
/* Lists all of the books which have been input onto the system */
void List_Books (void)
{
system ("cls");
bookfile = fopen("BOOK.DAT","rb");
if (bookfile == 0)
{ printf ("An error occurred while opening the file.\a\n");
}
else
printf (" LISTS ALL BOOKS ON FILE\n\n");
printf ("ISBN \t Author \t Title\n\n");
while (!feof(bookfile))
{
fread(&Books, sizeof(Books),1,bookfile);
if(!feof(bookfile))
printf ("%-20s \t %-20s \t %s\n", Books.ISBN, Books.Author,
Books.Title);
}
fclose (bookfile);
system("PAUSE");
return Menu();
}
/* Search the database using an ISBN */
void Search_ISBN (void)
{
char Search_ISBN [20];
int found =0;
system ("cls");
bookfile = fopen("BOOK.DAT","rb");
printf("Please enter ISBN that you wish to search for.\n");
scanf ("%s", &Search_ISBN);
{
while (!feof(bookfile))
fread(&Books, sizeof(Books),1,bookfile);
{ if (Search_ISBN == Books.ISBN)
{found = 1;
}
}
if (found ==1)
{ if(!feof(bookfile))
printf ("Title is %s\n Author is %s\n", Books.Author, Books.Title);
}
else
{printf ("ISBN is not found.\n");}
}
fclose (bookfile);
system("PAUSE");
}
/* Search the database using the Title of a Book */
void Search_Title (void)
{
char Search_Title[50];
int found =0;
system ("cls");
bookfile = fopen("BOOK.DAT","rb");
printf("Please enter book Title that you wish to search for.\n");
scanf ("%s", &Search_Title);
{
while (!feof(bookfile))
fread(&Books, sizeof(Books),1,bookfile);
{ if (Search_Title == Books.Title)
{
found = 1;
}
}
if (found ==1)
{ if(!feof(bookfile))
printf ("Title is %s\n Author is %s\n", Books.Author, Books.Title);
}
else
{
printf ("Title is not found.\n");}
}
fclose (bookfile);
system("PAUSE");
}