Hi, I am writing a program which is to contain a list of library books. My problem is that the program is not too allow 2 ISBN numbers the same to be entered. I have used strcmp to check that the ISBN numbers are not the same, but I cannot get this to work, correctly. At the moment the program allows me to enter one ISBN then jumps to say ISBN already entered. Can anyone see where I am going wrong with my code. Thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Data Structure */
typedef struct
{
char ISBN[20]; /* ISBN Number of book*/
char author[20]; /* Author of book*/
char title[30]; /* Title of book*/
} library_book;
/* global variables */
FILE * bookfile; /* File of books*/
library_book books; /* Array of books*/
int choice=0; /*Variable to allow menu choice*/
/* Declare Functions */
void menu (void); /*Function to run menu*/
void add_book (void); /*Function to add a book*/
void list_books (void); /*Function to list books*/
void search_ISBN (void); /*Function to searc for an ISBN*/
void search_title (void); /*Function to search for a title*/
int main (int argc, char *argv[])
/******************************************************************************/
/*Function to add a book */
void add_book (void)
{ int found = 0; /* Variable to allow ISBN check*/
system ("cls"); /* Clear the Screen */
bookfile = fopen("BookFile.bin","ab"); /* Open File */
if (bookfile == 0)
{ printf ("An error occurred while opening the file.\a\n");
}/* End of if statement*/
printf ("Please enter ISBN or q to quit.\n");
scanf ("%s", &books.ISBN);
strcmp (books.ISBN,books.ISBN);
/* Do the following until user wishes to quit or ISBN = ISBN*/
while ((tolower(books.ISBN[0]) != 113)&&(strcmp (books.ISBN,books.ISBN)==1))
found =0;
{
printf ("Please enter author.\n");
/* flush buffers and use gets to enable spaces */
fflush(stdin);
gets (books.author);
/* Check string length is not too long*/
while((strlen(books.author)<1)||
(strlen(books.author)>20))
{
printf ("Author between 1 and 20 characters.\n");
fflush(stdin);
gets (books.author);
}/*End of error check*/
printf ("Please enter title of book.\n");
/* flush buffers and use gets to enable spaces */
fflush(stdin);
gets (books.title);
/* Check string length is not too long*/
while((strlen(books.title)<1)||
(strlen(books.title)>20))
{
printf ("Title between 1 and 20 characters.\n");
fflush(stdin);
gets (books.title);
}/*End of error check*/
fwrite (&books,sizeof(books),1,bookfile);
/*Error message for if ISBN is already of file*/
strcmp (books.ISBN,books.ISBN);
while (strcmp (books.ISBN,books.ISBN)==0)
{
printf ("Book is already on file, Enter another ISBN\n");
scanf ("%s", &books.ISBN);
found = 1;
}/* End of while loop to check ISBN*/
printf ("Please enter ISBN or q to quit.\n");
scanf ("%s", &books.ISBN);
}/* End of while loop */
fclose (bookfile); /* Close the file*/
}