Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Use strcmp to check 2 values are not equal (http://www.go4expert.com/forums/strcmp-check-2-values-equal-t17150/)

chocrose 26Apr2009 17:55

Use strcmp to check 2 values are not equal
 
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*/
     
      }


xpi0t0s 26Apr2009 18:57

Re: Use strcmp to check 2 values are not equal
 
Code:

strcmp (books.ISBN,books.ISBN);
Ignoring the return value from strcmp isn't going to help...
Usually you would test the return value, if it's zero the strings are equal, and do something accordingly, e.g.
Code:

if (!strcmp(str1,str2))
{
  // strings are equal.
}

Now this bit's interesting. What's the point of the first strcmp call?
Code:

strcmp (books.ISBN,books.ISBN);
while (strcmp (books.ISBN,books.ISBN)==0)


chocrose 26Apr2009 19:02

Re: Use strcmp to check 2 values are not equal
 
I'm a beginner at this and I thought that the first strcmp would let the program continue as normal as the ISBN does not equal the ISBN and that the second strcmp would let the program know that the user has to be asked for a second ISBN to be entered as the ISBN is already on file.

xpi0t0s 27Apr2009 01:53

Re: Use strcmp to check 2 values are not equal
 
> "...would let the program know..."

Ah. No, the program doesn't "know" anything. If you call a function and do nothing with the return value, then the return value is lost. The program knows nothing that you don't explicitly tell it, and if you don't explicitly tell it to do something about the return value then it'll do nothing. There is absolutely no intelligence there, and the program doesn't know what you mean.

Assuming you're referring to the strcmp before the while (strcmp), the first is effectively a no-operation, because it doesn't modify the strings passed in and nothing happens to the return value.


All times are GMT +5.5. The time now is 10:48.