Thread: Help with library system

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Help with library system

    Hey guys and gals! I'm having a bit of problem with my C codes. I was supposed to make a program that adds, removes and views books. Yeahp, it's a Library System. Well, the problem is that when I input some text in like "Hello Hi", it would automatically jump to "Thank you for using the Library System" and would eventually exit. It happens when I input two or more words. I was able to add the books and all, and there are no errors, but when I get to that part, it does that. I can add 1-word letters like "Hello" and "Hi" and it wouldn't have any errors. Can you tell me what the problem is?
    Here's my code!

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<windows.h>
    
    char decide, choice;
    int booknum;
    struct libbooks{
    char title[40];
    char author[50];
    } book1, book2, book3, book4, book5, bookx[99]; 
    
    void viewbook();
    void addbook();
    void adminact();
    void removebook();
    void open();
    
    main()
    {  
     int adminuser, adminpass, memberuser, memberpass, cmpadmin, cmpmember;
     char user[20], pass[20];
     
          strcpy(book1.title,"\n\t\t\t  The Legends Of Tom Sawyer    ");
          strcpy(book2.title,"\t\t\t  The Success Principles        ");
          strcpy(book3.title,"\t\t\t  The Lord Of The Rings         ");
          strcpy(book4.title,"\t\t\t  Star Wars: Clone Wars         ");
          strcpy(book5.title,"\t\t\t  Philosophy Of the Human Mind  ");
          puts("\n\n");
          puts("\t\t\t******************************");
          puts("\t\t\t*Don Bosco Technical College*");
          puts("\t\t\t******************************");
          puts("\n");
          puts("\t\t\t******************************");
          puts("\t\t\t* Welcome to Library System. *");
          puts("\t\t\t******************************");
          puts("\t\t\t\tEnter username: ");
          printf("\t\t\t\t     ");
          scanf("%s", user);
          puts("\t\t\t\tEnter password: ");
          printf("\t\t\t\t     ");
          scanf("%s", pass);
          adminuser = strcmp(user, "admin");
          adminpass = strcmp(pass, "dbtcadmin");
          memberuser = strcmp(user, "member");
          memberpass = strcmp(pass, "dbtcmember");
          if(adminuser == 0 && adminpass == 0)
          adminact();
          else if(memberuser == 0 && memberpass == 0)
          printf("Declare function");
          else printf("Cannot log in. Username and Password invalid");
          getch();
          }
    void addbook()
         {
         int i;
         system("cls");
         puts("\n\n");
         puts("\t\t\t************************************\n");
         printf("\t\t     ** How many books do you want to enter? **\n");
         puts("\n\t\t\t************************************");
         printf("\n\t\t\t    You may enter up to 100 books.\n");
         puts("\n\t\t\t************************************");
         printf("\t\t\t\t\t ");
         scanf("%d", &booknum);
         printf("\t\t\t\t Enter the title(s):\n");
         printf("  \t\t\t\t");
         for (i=0;i<booknum;i++){
         printf("\n  \t\t\t\t");
         scanf("%s", bookx[i].title);
         }
         printf("\n\tBooks successfully added.\n");
         printf("\n\tDo you want to go back to the option admin system?<Y/N>");
         scanf("%c", &choice);
         choice = getch();
         switch(choice)
         {
         case 'Y': case 'y':
              return adminact();
              break;
         case 'N': case 'n':
              printf("\n\nThank you for using the Library System!");
              break;
              default: printf("\nNot one of the choices.");
              break;
              }
    }
    
    void viewbook()
    {
         int x, y;
         system("cls");
         puts("\n\n");
         puts("\t\t\t********************************");
         printf("%s\n%s\n%s\n%s\n%s", book1.title, book2.title, book3.title, book4.title, book5.title);
         for(x = 0; x<booknum; x++)
         printf("\n\t\t\t  %s",bookx[x].title);
         puts("\n");
         puts("\t\t\t********************************");
         puts("\n");
         printf("\t\tDo you want to go back to the option system?<Y/N>");
         scanf("%c", &choice);
         choice = getch();
     switch(choice)
         {
         case 'Y': case 'y':
              return adminact();
              break;
         case 'N': case 'n':
              printf("\n\n\t\tThank you for using the Library System!");
              break;
              default: printf("\nNot one of the choices.");
              break;
              }
         }
    
    void adminact()
    {
         int choice;
         system("cls");
          puts("\n\n");
         puts("\t\t\t# **************************** #");
         printf("\n\t\t\t# *  What do you want to do? * #\n");
         puts("\n\t\t\t# **************************** #");
         puts("\t\t\t# **************************** #");
         puts("\n\t\t\t# * 1.    View book(s)       * #");
         puts("\t\t\t# * 2.    Add Book(s)        * #");
         puts("\t\t\t# * 3.    Remove book(s)     * #");
         puts("\t\t\t# * 4.    Return to Log-in   * #");
         puts("\n\t\t\t# **************************** #");
         printf("\t\t\t\t     ");
         scanf("%d", &choice);
         switch(choice)
         {
         case 1:
              viewbook();
              break;
         case 2:
              addbook();
              break;
         case 3:
              removebook();
              break;
         case 4:
              open();
              break;
              
         default: printf("Thank you for using the Library System!");
              }             
              }
          
    void removebook(){
         int r, r1;
         int x, y;
         system("cls");
         puts("\n\n");
         puts("\t\t\t********************************");
         printf("%s\n%s\n%s\n%s\n%s", book1.title, book2.title, book3.title, book4.title, book5.title);
         for(x = 0; x<booknum; x++)
         printf("\n\t\t\t  %s",bookx[x].title);
         puts("\n");
         puts("\t\t\t********************************");
         printf("\n\t\t      Select the books you want to Remove.");
         printf("\n\n\t\t\t\t");
         r1 = getch();
         scanf("%d", &r1);
         switch(r1){
                    case 1: strcpy(book1.title," ");return adminact();break;
                    case 2: strcpy(book2.title," ");return adminact();break;
                    case 3: strcpy(book3.title," ");return adminact();break;
                    case 4: strcpy(book4.title," ");return adminact();break;
                    case 5: strcpy(book5.title," ");return adminact();break;
                    default: for(x = 0; x<booknum; x++)
                             strcpy(bookx[x].title," ");
                             return adminact();
                    
            }
            }
    void open()
    {  
     int adminuser, adminpass, memberuser, memberpass, cmpadmin, cmpmember;
     char user[20], pass[20];
     system("cls");
          puts("\n\n");
          puts("\t\t\t******************************");
          puts("\t\t\t*Don Bosco Technical College*");
          puts("\t\t\t******************************");
          puts("\n");
          puts("\t\t\t******************************");
          puts("\t\t\t* Welcome to Library System. *");
          puts("\t\t\t******************************");
          puts("\t\t\t\tEnter username: ");
          printf("\t\t\t\t     ");
          scanf("%s", user);
          puts("\t\t\t\tEnter password: ");
          printf("\t\t\t\t     ");
          scanf("%s", pass);
          adminuser = strcmp(user, "admin");
          adminpass = strcmp(pass, "dbtcadmin");
          memberuser = strcmp(user, "member");
          memberpass = strcmp(pass, "dbtcmember");
          if(adminuser == 0 && adminpass == 0)
          adminact();
          else if(memberuser == 0 && memberpass == 0)
          printf("Declare function");
          else printf("Cannot log in. Username and Password invalid");
          }
    There, edited it. I hope it's clear enough. XD
    Last edited by hexarthrius; 03-10-2011 at 03:46 AM. Reason: Wrong format of post

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you could read the "how to post code" thread, then edit your post so that your code looks like this, rather than an unindented mess.
    Code:
    int main ( ) {
        // your code should be indented to reflect the program flow
        // like this is indented away from the left hand side.
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    There. Sorry, I was in a hurry. XD

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Has to do with stuff being in the input buffer. scanf will strip off the first word, but the space and following words are still there waiting.

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    You should use gets() instead of scanf()... There is the difference between inputing strings.....

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Better use fgets() and strip newline(if you don't want) using strchr()/other methods.
    http://c-faq.com/stdio/getsvsfgets.html

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use fgets(books[i],title, SIZE, stdin); for entering your books titles. Most books have multiple words, and scanf() is a poor choice overall, for user entry. (Where SIZE is a
    #define SIZE 100

    macro for the size of your array of structs.

    The ONLY problem with fgets() is that it leaves in the newline char, in the title. Now you know how to remove it:
    Code:
    #include <string.h>  /* if you haven't done this yet */
    int len;   /* a good variable name helps */
    
    /* fgets() line of code, here, then: */
    len = strlen(books[i].title);
    if(books[i].title[len-1]=='\n') /* do we have a newline char in the title? */
      books[i].title[len-1]='\0'; /*overwrite the newline char, shortening the title by 1 char*/
    I don't understand the book1, book2, book3, book4, book5 variables, at all. Why? When you need a local book struct, just create it, in the local function.
    Last edited by Adak; 03-11-2011 at 12:55 AM.

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yeah fgets().......

    If you want to store newline character else just gets()....
    Programmer's choice :-)

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you want to invite buffer overflow/marks deducted/ , use gets().
    Otherwise use fgets() or something safer. Your choice.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Mr.777 View Post
    Yeah fgets().......

    If you want to store newline character else just gets()....
    Programmer's choice :-)
    In Theory, I agree with you, 777. In practice, gets() simply must be avoided for user input.

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yeah practically, we always need to put newline character for better processing.....

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    Well guys, thanks for the advice! I used the gets() function and it worked! Yay! But there is another downside that I discovered. Now when I use the function removebook, and I wanted to remove a book in an array, it doesn't remove anything. Any suggestions as to how it should be solved?

    Here's the code again:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<windows.h>
    
    char decide, choice;
    int booknum;
    struct libbooks{
    char title[40];
    char author[50];
    } book1, book2, book3, book4, book5, bookx[99]; 
    
    void viewbook();
    void addbook();
    void adminact();
    void removebook();
    void open();
    void useract();
    
    main()
    {  
     int adminuser, adminpass, memberuser, memberpass, cmpadmin, cmpmember;
     char user[20], pass[20];
     
          strcpy(book1.title,"\n\t\t\t  The Legends Of Tom Sawyer    ");
          strcpy(book2.title,"\t\t\t  The Success Principles        ");
          strcpy(book3.title,"\t\t\t  The Lord Of The Rings         ");
          strcpy(book4.title,"\t\t\t  Star Wars: Clone Wars         ");
          strcpy(book5.title,"\t\t\t  Philosophy Of the Human Mind  ");
          puts("\n\n");
          puts("\t\t\t******************************");
          puts("\t\t\t*Don Bosco Technical College*");
          puts("\t\t\t******************************");
          puts("\n");
          puts("\t\t\t******************************");
          puts("\t\t\t* Welcome to Library System. *");
          puts("\t\t\t******************************");
          puts("\t\t\t\tEnter username: ");
          printf("\t\t\t\t     ");
          scanf("%s", user);
          puts("\t\t\t\tEnter password: ");
          printf("\t\t\t\t     ");
          scanf("%s", pass);
          adminuser = strcmp(user, "admin");
          adminpass = strcmp(pass, "dbtcadmin");
          memberuser = strcmp(user, "member");
          memberpass = strcmp(pass, "dbtcmember");
          if(adminuser == 0 && adminpass == 0)
          adminact();
          else if(memberuser == 0 && memberpass == 0)
          printf("Declare function");
          else printf("Cannot log in. Username and Password invalid");
          getch();
          }
    void addbook()
         {
         int i;
         char temp;
         system("cls");
         puts("\n\n");
         puts("\t\t\t************************************\n");
         printf("\t\t     ** How many books do you want to enter? **\n");
         puts("\n\t\t\t************************************");
         printf("\n\t\t\t    You may enter up to 100 books.\n");
         puts("\n\t\t\t************************************");
         printf("\t\t\t\t\t ");
         scanf("%d%c", &booknum,&temp);
         printf("\t\t\t\t Enter the title(s):\n");
         printf("  \t\t\t\t");
         for (i=0;i<booknum;i++){
         printf("\n  \t\t\t\t");
         gets(bookx[i].title);
         }
         printf("\n\tBooks successfully added.\n");
         printf("\n\tDo you want to go back to the option admin system?<Y/N>");
         scanf("%c%c", &choice,&temp);
         //choice = getch();
         switch(choice)
         {
         case 'Y': case 'y':
              return adminact();
              break;
         case 'N': case 'n':
              printf("\n\nThank you for using the Library System!");
              break;
              default: printf("\nNot one of the choices.");
              break;
              }
    }
    
    void viewbook()
    {
         int x, y;
         char temp;
         system("cls");
         puts("\n\n");
         puts("\t\t\t********************************");
         printf("%s\n%s\n%s\n%s\n%s", book1.title, book2.title, book3.title, book4.title, book5.title);
         for(x = 0; x<booknum; x++)
         printf("\n\t\t\t  %s",bookx[x].title);
         puts("\n");
         puts("\t\t\t********************************");
         puts("\n");
         printf("\t\tDo you want to go back to the option system?<Y/N>");
         scanf("%c%c", &choice,&temp);
         //choice = getch();
     switch(choice)
         {
         case 'Y': case 'y':
              return adminact();
              break;
         case 'N': case 'n':
              printf("\n\n\t\tThank you for using the Library System!");
              break;
              default: printf("\nNot one of the choices.");
              break;
              }
         }
    
    void adminact()
    {
         int choice;
         char temp;
         system("cls");
          puts("\n\n");
         puts("\t\t\t# **************************** #");
        do{ printf("\n\t\t\t# *  What do you want to do? * #\n");
         puts("\n\t\t\t# **************************** #");
         puts("\t\t\t# **************************** #");
         puts("\n\t\t\t# * 1.    View book(s)       * #");
         puts("\t\t\t# * 2.    Add Book(s)        * #");
         puts("\t\t\t# * 3.    Remove book(s)     * #");
         puts("\t\t\t# * 4.    Return to Log-in   * #");
         puts("\n\t\t\t# **************************** #");
         printf("\t\t\t\t     ");
         scanf("%d%c", &choice,&temp);
         switch(choice)
         {
         case 1:
              viewbook();
              break;
         case 2:
              addbook();
              break;
         case 3:
              removebook();
              
              break;
         case 4:
              open();
              break;
              
         default: printf("Thank you for using the Library System!");
              }   
              }while(choice != 1 &&choice != 2&&choice != 3&&choice != 4 );          
              }
          
    void removebook(){
         int r, r1;
         int x, y=2;
         char temp;
         system("cls");
         puts("\n\n");
         puts("\t\t\t********************************");
         printf("%s\n%s\n%s\n%s\n%s", book1.title, book2.title, book3.title, book4.title, book5.title);
         for(x = 0; x<booknum; x++)
         printf("\n\t\t\t  %s",bookx[x].title);
         puts("\n");
         puts("\t\t\t********************************");
         printf("\n\t\t      Select the books you want to Remove.");
         printf("\n\n\t\t\t\t");
        // r1 = getch();
         scanf("%d%c", &r1,&temp);
        
         switch(r1){
                    case 1: strcpy(book1.title," ");return adminact();break;
                    case 2: strcpy(book2.title," ");return adminact();break;
                    case 3: strcpy(book3.title," ");return adminact();break;
                    case 4: strcpy(book4.title," ");return adminact();break;
                    case 5: strcpy(book5.title," ");return adminact();break;
                    default: for(r=1;r<booknum-y;r++)
                    {
                            
                         strcpy(bookx[r].title," ");
                            y--;
                             }
                              return adminact();
                          
                    
            }
            }
    void open()
    {  
     int adminuser, adminpass, memberuser, memberpass, cmpadmin, cmpmember;
     char user[20], pass[20];
     system("cls");
          puts("\n\n");
          puts("\t\t\t******************************");
          puts("\t\t\t*Don Bosco Technical College*");
          puts("\t\t\t******************************");
          puts("\n");
          puts("\t\t\t******************************");
          puts("\t\t\t* Welcome to Library System. *");
          puts("\t\t\t******************************");
          puts("\t\t\t\tEnter username: ");
          printf("\t\t\t\t     ");
          scanf("%s", user);
          puts("\t\t\t\tEnter password: ");
          printf("\t\t\t\t     ");
          scanf("%s", pass);
          adminuser = strcmp(user, "admin");
          adminpass = strcmp(pass, "dbtcadmin");
          memberuser = strcmp(user, "member");
          memberpass = strcmp(pass, "dbtcmember");
          if(adminuser == 0 && adminpass == 0)
          adminact();
          else if(memberuser == 0 && memberpass == 0)
          printf("Declare function");
          else printf("Cannot log in. Username and Password invalid");
          }

  13. #13
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Practically, you can't remove from the static arrays, and also you are assigning the title as " ".
    So, in viewbook function, don't show the books which have title as " ".
    Simple :-)

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well apart from the train wreck of using gets(), the next problem I see is your recursive calls to adminact(); every time you finish something.

    viewbook() for example should just return; and it is right back in the middle of adminact() where it was called from.

    If you want a normal user to call viewbook(), you would want to return to the normal user function as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mr.777 View Post
    If you want to store newline character else just gets()....
    Programmer's choice :-)
    It's good to know I can just never bother reading anything you post again.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 09:06 AM
  2. Replies: 1
    Last Post: 08-16-2009, 05:49 AM
  3. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  4. Library Wrapper
    By cusavior in forum C Programming
    Replies: 3
    Last Post: 03-25-2008, 10:27 AM
  5. New system build wont boot
    By lightatdawn in forum Tech Board
    Replies: 7
    Last Post: 12-02-2005, 06:58 AM