Library program - need some help

This is a discussion on Library program - need some help within the C Programming forums, part of the General Programming Boards category; Hi I am creating a library programme. I want to write the date issue/return, name - first & last, and ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Library program - need some help

    Hi

    I am creating a library programme. I want to write the date issue/return, name - first & last, and title of book to a text file.
    I have done this, but I am now at some point I need help. For example, the program doesn't give me a chance to type in when the book must be returned, it goes straight to ask me if I want to add another record.
    Also, I want to be able to delete any old records from the file. Can somebody give me any tip on how to do this?


    Thanks

    Code:
    #define LinesPerScreen 12 
    
    #include <stdio.h> 
    #include <process.h> 
    #include <conio.h> 
    
    void add_records(void); 
    void display_records(void); 
    void delete_record(void); 
    
    void main() 
    { 
    
       int reply=0; 
       int items = 3; 
    
       system("cls"); 
       printf("Library Management Program(Development Phase"); 
       while (reply < items) 
       { 
          printf("\n\nChoose from the following options:"); 
    
          printf ("\n%d. Choose this option if you want to add an item", 1); 
          printf ("\n%d. Choose this option if you want to display items", 2); 
          printf ("\n%d. (or %d, ..., etc.) EXIT program.", items, (items+1)); 
          printf ("\n");                    // Changes the line 
          printf("\nTo exit this program choose the option %d, or higher!", items); 
          printf("\nPlease choose the item to run (e.g. 1, ..., %d) ", items); 
          scanf("%d", &reply);  // Inputs your reply 
          printf("Your choice was option number %d", reply); //Option display 
    
          if (reply == 1) 
          { 
    
             printf ("\nThe function that adds a record is called in"); 
             add_records(); //Call to the function 
          } 
          if (reply == 2) 
          { 
    
             printf ("\nThe function that displays a record is called in"); 
             display_records(); //Call to the function 
          } 
    
          if (reply >= items) 
          { 
    
             printf (" which terminates this program!"); 
          } 
          if (reply < 1) 
          { 
    
             printf ("\nChoice %d out of range (retype 1, ..., %d).",reply, items); 
          } 
       } 
       //The final displays 
       printf ("\n\nThank you for using this program. Good bye!"); 
       printf ("\n"); 
       system("pause"); 
    } 
    
    void add_records() 
    { 
       FILE *tofile; 
       char name_in[30]; 
       char date_issue[30]; 
       char title_book[30]; 
       char date_return[30]; 
       char reply; 
       int value_in, rec_no=1; 
       printf ("\nInside of write a record function"); 
       tofile = fopen ("library_file.txt", "w"); 
       fprintf (tofile, "This is the content of your text file."); 
       do 
       { 
          rec_no++; 
          printf("\n\nWrite here if you want to add a book %d", rec_no); 
          printf("\nType in when the book has been issued(e.g. 11/09/04): "); 
          scanf("%s", date_issue); 
          printf("Type in the title of the book(e.g. Deitel_and_Deitel): "); 
          scanf("%s", &title_book); 
          printf("Type in the name of the borrower(e.g John Pojak): "); 
          scanf("%s", &name_in); 
          printf("Type in when the book has to be returned(e.g. 01/10/04): "); 
          scanf("%s", &date_return); 
          fprintf (tofile, "\nRecord %d:  %s, %s, %s, %s, ", rec_no, date_issue, 
          title_book, name_in, date_return); 
          printf("Would you like to add another record (y/n): "); 
          reply = getche(); 
          if ((reply != 'n') && (reply != 'N')) 
          { 
             printf("\nTo quite adding records you need to type N or n -"); 
             printf(" your reply was %c", reply); 
          } 
       } 
       while ((reply == 'y') || (reply == 'Y')); 
       fclose(tofile); 
    } 
    
    void display_records() 
    { 
       FILE *fromfile; 
       char content_in[60]; 
       int rec_no = 0; 
       printf ("\nRead/Displaying all the current records\n"); 
       fromfile = fopen ("library.txt", "r"); 
       while (!feof(fromfile)) 
       { 
          rec_no++; 
          //fscan (fromfile, "%s", content_in); 
          fgets (content_in, 60, fromfile); 
          printf ("\nRecord No [%d] = %s", rec_no, content_in); 
          if (rec_no >= LinesPerScreen) 
          { 
             printf ("\n"); 
             system("pause"); 
             system("cls"); 
          } 
       } 
       fclose(fromfile); 
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    While doing scanf for character arrays,we just specify the name of the array and not adress of that array.
    Eg . scanf("%s",title_book);

    and not
    scanf("%s",&title_book);

    For deleting records from a file ,
    1-> read the records one by one in a structure
    2-> write all other records except the one to be deleted into another file
    3-> rename the file

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >While doing scanf for character arrays,we just specify the name of the array and not adress of that array.
    Correct, but why? Just telling somebody that we do things one way isn't enough. You need to explain the reasoning behind it or they won't understand and will probably make the mistake again.

    The reason that arrays are not prefixed with & in scanf is because an array name is almost always converted to a pointer to the first element of the array. Because scanf expects a pointer to char, the bare array name is the correct type. If you prefix the array name with an ampersand, it changes the type to pointer to array of char, which is undefined behavior because of an unchecked type mismatch.
    My best code is written with the delete key.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > void main()
    Ahem!!!!
    You know the drill by now surely.

    > reply = getche();
    Mixing input methods is bound to produce the occasional surprise.
    You best bet is to ALWAYS read a line of input using fgets(), then extract what you want from the line using sscanf() or whatever else seems appropriate.

    > while (!feof(fromfile))
    This is wrong - and why it's wrong is in the FAQ

    > fgets (content_in, 60, fromfile);
    Check the return result - this applies to an awful lot of stuff actually
    Also
    fgets (content_in, sizeof content_in, fromfile);
    is more maintainable.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Just a suggestion... on the deletion side, add another field/property to your data to indicate a boolean value of deleted or not. Then you will be able to institute a behind the scenes batch update to remove records. Otherwise your wasting time re writing the file every time you delete a record...

    Safe Codin.
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with audio library on DOS...
    By Marton79 in forum C Programming
    Replies: 10
    Last Post: 08-25-2006, 12:32 AM
  2. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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