problems with strcmp

This is a discussion on problems with strcmp within the C Programming forums, part of the General Programming Boards category; I cant seem to get this to do what I want, which is to check for duplicate ISBN's and if ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    7

    Post problems with strcmp

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by laserlight View Post
    How does it not work?
    it doesnt recognise a duplicate isbn and doesnt loop properly either lol. Im a beginner and this is starting to annoy me

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Can't compare the return value of strcmp() to 1, instead test whether it is equal to, greater than, or less than zero.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And, it is returning ZERO for equal!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    fflush(stdin);
    gets (Books.Title);
    both should not be used - FAQ explains why.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    when you are do
    Code:
        strcmp (Books.ISBN,NewBooks.ISBN);
    you are do nothing (it is helpful by its return value only, hence if you aren't check this value, call is useless)
    Maybe you wanted use strcpy (to copy string from one place to another)

    Code:
        fflush(stdin);
    it is incorrect, try fflush(stdout) and only when you have no newline ('\n') in the end of output

    Code:
    	
    	gets (Books.Author);
    it will not control the length of input line (if it will be longer than Books.Author string (30 chars), it will erase Title[50]; in the structure, because it goes after the Author string)
    you can use fgets (it differs from gets by saving newline character in the line only and can control line length fgets(line, MAXLINE, stdin) )

    Code:
    return Menu();
    what is this mean ?

    Code:
        char Search_ISBN [20];
        ...
        if (Search_ISBN == Books.ISBN)
    you can't compare strings directly
    only char by char
    Code:
        if (strcmp(Search_ISBN, Books.ISBN) == 0)
            ;
    here too
    Code:
        if (Search_Title == Books.Title)
    Quote Originally Posted by loopymoo26
    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??
    I would use an array for strings

    • make it
    • load it from a file (only first field of every structure) by fread and strcpy functions
    • then sort them
    • count duplicates of every string and print error with number of them or skip duplicates of every string and print an error for every duplicate occurrence


    You can load not only first string (ISBN), if you will load full structures, you will get avail to delete duplicates and save cleared structure array back to the file, but it will be longer than the processing of only first strings and it will need linked list rather than simply array of structure (you are beginner, so it can be hard).
    Last edited by c.user; 05-13-2009 at 02:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  2. strcmp() \ flushing output
    By Brain Cell in forum C Programming
    Replies: 7
    Last Post: 01-17-2005, 12:28 PM
  3. seg fault with strcmp
    By samps005 in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 04:32 PM
  4. strcmp, any borders?
    By demonus in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 01:48 AM
  5. strcmp problems
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-14-2002, 07:17 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21