0% found this document useful (0 votes)
9 views8 pages

Message

The document is a C program for a library management system that includes functionalities for managing members, books, and borrowed items. It features structures to hold member and book data, functions to read from input files, and a menu-driven interface for user interaction. The program includes features for adding members, searching for members and books, updating member information, and displaying borrowed books.

Uploaded by

matthew.renwick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Message

The document is a C program for a library management system that includes functionalities for managing members, books, and borrowed items. It features structures to hold member and book data, functions to read from input files, and a menu-driven interface for user interaction. The program includes features for adding members, searching for members and books, updating member information, and displaying borrowed books.

Uploaded by

matthew.renwick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <stdio.

h>
#include <stdlib.h>
#define max 100
int actualsize = 0;
int actualsize1 = 0;
int actualsize2 = 0;

//prototyping all the functions


//void welcome();
//int password();
//int menu();
//int readdata();
//int add_member();
//int search_member();
//int update_member();

//int books_borrowed();
//int new_book();
//int search_book();
//int searchb_code ();
//int display_popular_books();
//int exitp();

//this structure of the file holds the members of the library


struct members{
char name[50];
int age;
char gender;
int contact;
char email[30];
}m[100];

//this structure of the file holds the books in the library


struct books{
int bookcode;
char title[50];
char author[50];
char category[50];
char genre[50];
}b[100];

//this structure of the file holds the members who borrowed books
struct returns
{
char membername[50];
int bookcode;
char booktitle[50];
char author[50];
char borrowed[20];
char returns[20];
}r[100];

// password function checks for a passcode and returns 1 for correct and 0 for
wrong
int password()
{
int p=0, code= 1111; int attempts=0;
while(attempts<3){
printf("\nPlease enter the correct 4-digit passcode:");
scanf("%d",&p);

if (p == code)
{
return 1;
}
else
{
printf("\nError!Incorrect passcode.Please try again\n");
attempts++;
}
}

printf("\nToo many incorrect attempts.Exiting program..");

//this function reads the data from the input file into the array of structures
with the members of the library
int readdata(){
FILE*in=fopen("inputmembers.txt","r");
int c=0;

if (in == NULL)
printf("\nThe file is empty");

else
{
while (!feof(in))
{
fscanf(in,"%s",&m[c].name);
printf("Name: %s",m[c].name);
fscanf(in,"%d",&m[c].age);
printf(" Age: %d",m[c].age);
fscanf(in," %c",&m[c].gender);
printf(" Gender: %c",m[c].gender);
fscanf(in,"%d",&m[c].contact);
printf(" Contact: %d",m[c].contact);
fscanf(in,"%s",m[c].email);
printf(" Email: %s \n\n",m[c].email);
c++;
}
printf("/nThe data from the file was read properly.");
actualsize = c;
}

//this function reads the data from the input file into the array of
structures with the books in the library
FILE*in2=fopen("inputbooks1.txt","r");
int i=0;

if (in2 == NULL)
printf("\nThe file is empty");

else
{
while (!feof(in2))
{
fscanf(in2,"%d",&b[i].bookcode);
printf("\nBook Code: %d \n",b[i].bookcode);
fscanf(in2,"%s ",&b[i].title);
printf("Title: %s \n",b[i].title);
fscanf(in2,"%s ",&b[i].author);
printf("Author: %s \n",b[i].author);
fscanf(in2,"%s ",&b[i].category);
printf("Category: %s \n",b[i].category);
i++;
printf("Counter: %d\n\n",i);
}
printf("/nThe data from the file was read properly.");
actualsize1 = i;
}

//this function reads the data from the input file into the array of
structures with the members of the library who borrowed books
FILE*in3=fopen("inputreturns.txt","r");
int n=0;

if (in3 == NULL)
printf("\nThe file is empty");

else
{
while (!feof(in3))
{
fscanf(in3,"%s",&r[n].membername);
printf("\n\nMember Name: %s \n",r[n].membername);
fscanf(in3,"%d",&r[n].bookcode);
printf("Book Code: %d \n",r[n].bookcode);
fscanf(in3,"%s",&r[n].booktitle);
printf("Book Title: %s \n",r[n].booktitle);
fscanf(in3,"%s",&r[n].author);
printf("Book Title: %s \n",r[n].author);
fscanf(in3,"%s",&r[n].borrowed);
printf("Date Borrowed: %s \n",r[n].borrowed);
fscanf(in3,"%s",&r[n].returns);
printf("Date to be Returned: %s \n\n",&r[n].returns);
n++;
printf("\nCounter: %d\n\n",n);
}
printf("/nThe data from the file was read properly.");
actualsize2 = n;
}
}

//this function welcomes the user


void welcome()
{
printf("\nWELCOME TO THE POS LIBRARY. WHERE BOOKS HEAL\n\n");
}

//this function displays the options to the user and returns an option
int menu()
{
int choice=-1;
printf("Please choose from the following menu options\n\n");
printf("1: Display all data\n");
printf("2: Add a member\n");
printf("3: Search for a member\n");
printf("4 :Update a member's contact information'\n");
printf("5 :Search for a book\n");
printf("6: Display books borrowed\n");
printf("0 :Exit\n");

printf("\nPlease enter your Choice: ");


scanf("%d", &choice);
if ((choice <0) && (choice >6 ))
{
printf("\nError!\nPlease choose a valid number between 1 and 6");

}
else
return choice;

//this function is used to add a new member to the structure


char add_member()
{
//prompting the user to enter member info
printf("/nEnter Member Information: \n");

printf("Name: ");
scanf(" %s", m[actualsize].name);
printf("Age: ");
scanf(" %d", &m[actualsize].age);
printf("Gender (M/F): ");
scanf(" %c", &m[actualsize].gender);
printf("Contact: ");
scanf(" %d", &m[actualsize].contact);
printf("Email: ");
scanf(" %s", m[actualsize].email);

actualsize++;
}

//this function is used to search for a member in the system


int search_member(int search_value)
{
int c=0;
for (c=0;c<actualsize;c++)
{
if (search_value == m[c].contact)
{
printf("NAME: %s",m[c].name);
return c;
}
}
return -1;
}

//this function is used to update a member's contact information


int update_member()
{
int contact=0;
int index=0;

printf("\nPlease enter the contact update you would like to make: \n");
scanf("%d",&contact);

index=search_member(contact);
printf("\nPlease enter the member name:\n");
scanf("%s",&m[index].name);

//print_info(index);

//print out full member updated info to the screen after given an index
void print_info(int index)
{
if (index == -1)
{
printf("Member does not exist");
}
else
{
printf("Name: %s",m[index].name);
printf("Age: %d",m[index].age);
printf("Gender: %c",m[index].gender);
printf("Contact: %d",m[index].contact);
printf("Email: %s \n",m[index].email);
}
}

int books_borrowed()
{
int n=0;
{
if (actualsize2==0){
printf("\n No books have been been borrowed yet\n");
return;
}
}
FILE*out=fopen("borrowed_books","w");
for(n=0;n<actualsize;n++){
printf("Member Name:%s\n",r[n].membername);
printf("Book Code:%d\n",r[n].bookcode);
printf("Book title:%s\n",r[n].booktitle);
printf("Author:%s\n",r[n].author);
printf("Date borrowed:%s\n",r[n].borrowed);
printf("Return Date:%s\n",r[n].returns);

fprintf(out,"Member Name:%s\n",r[n].membername);
fprintf(out,"Book code:%d\n",r[n].bookcode);
fprintf(out,"Book title:%d\n",r[n].booktitle);
fprintf(out,"Author:%s\n",r[n].author);
fprintf(out,"Date borrowed:%s\n",r[n].borrowed);
fprintf(out,"Return Date:%s\n",r[n].returns);
}
}

//this function is to add a book to the system


void new_book()
{
printf("Add New Book Information\n");

printf("Bookcode: ");
scanf("%d", b[actualsize].bookcode);
printf("Title: ");
scanf("%s", b[actualsize].title);
printf("Author: ");
scanf("%s", b[actualsize].author);
printf("Category: ");
scanf("%s ", b[actualsize].category);
printf("Genre: ");
scanf("%s", b[actualsize].genre);

actualsize++;

//this function is to search for a book


int search_book(int searchb)
{
printf("\nThis is working");
printf("The search value is %d", searchb);

int g=0;
for (g=0;g<actualsize1;g++)
{
printf("this is searching");
if (searchb == b[g].bookcode)
{
printf("Book: %s",b[g].title);
return g;
}
}
return -1;

int exitp()
{
printf("Exiting the system... Goodbye!\n");
}

//main function where all choices are contained


void main ()
{
int c=0;
int loc=0;
int index=0;
int search_contact=0;
int searchb_code = 0;

welcome();
int p = password();
int code=1111;
while (p==0)
{
printf("passcode incorrect, please enter correct code: \n");
p=password();
}

//function one executed


int choice = menu();

printf("\nThe option chosen was %d\n", choice);

//function two executed to add a member to the system


while (choice != 0)
{
if (choice == 1)
{
printf("Option one was chosen\n");
readdata();
}

if (choice == 2)
{
printf("Option two was chosen\n");
add_member();
}

if (choice == 3)
{
printf("\nOption 3 was choosen\n");

printf("Enter the contact of the member you are in search of: \


n");
scanf("%d", &search_contact);
loc=search_member(search_contact);

if (loc == -1)
{
printf("\nNobody was found\n\n\n");
}
}

if (choice == 4)
{
printf("Option 4 was choosen\n");
update_member();
}

if (choice == 5)
{

printf("Option 5 was choosen\n");


printf("Enter the book you are in search of: \n");
scanf("%d",&searchb_code);
loc=search_book(searchb_code);

if(loc == -1);
{
printf ("\n Book was not found\n\n\n");
}

if (choice == 6)
{
books_borrowed();
printf("Option 6 was choosen\n");
}

choice = menu();
}

You might also like