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