Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include <windows.h>
#include<stdio.h> //contains printf,scanf etc
#include<conio.h> //contains delay(),getch(),gotoxy(),etc.
#include <stdlib.h>
#include<string.h> //contains strcmp(),strcpy(),strlen(),etc
#include<dos.h> //contains _dos_getdate
#include<time.h>
void bookmang();
void addcopies();
void mainmenu();
int ind=0;
int ind2;
void addbooks();
void delay(unsigned int mseconds) /* or using nested loop with 32767 in the 2 loops */
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
struct books
{
char title[100];
char author[100];
char publisher[100];
char ISBN[100];
char dateofpublication[100];
int numberofcopies;
int currentavailablenumofcopies;
char category[100];
};
struct books book[1000];
struct members
{
int ID;
char name[100];
char address[100];
char street[100];
char city[100];
int phonenumber;
int age;
char email[100];
};
struct members member[1000];
int main()
{
printf("\n \n \n \t \t Welcome to library system management \n");
printf("\n \n \n \t \t Press any key to continue ..... ");
getch();
mainmenu();
return 0;
}
void mainmenu()
{
int n,a;
system("cls");
printf("Choose one of these choices : \n\n");
printf("1- Book managment \n\n");
printf("2- Member managment \n\n");
printf("3- Adminstrative actions \n\n");
printf("4- Quit the program \n\n");
switch(getch()){
case'1':
bookmang();
break;
case'2':
break;
case'3':
break;
case'4':
system("cls");
printf("Thank You !\n");
printf("Program will stop in\n3 seconds \n");
delay(1000);
printf("2 seconds \n");
delay(1000);
printf("1 seconds \n");
delay(1000);
exit(0);
break;
default:
printf("wrong choice !");
delay(500);
mainmenu();
}
}
void bookmang()
{
system("cls");
printf("1- Add books \n\n");
printf("2- Search for a book \n\n");
printf("3- Add new copies \n\n");
printf("4- Delete book \n\n");
printf("5- Back to mainmenu \n\n");
printf("choose the operation \n\n");
switch(getch()){
case'1':
addbooks();
break;
case'2':
break;
case'3':
addcopies();
break;
case'4':
break;
case'5':
mainmenu();
break;
default:
printf("wrong choice !");
delay(500);
bookmang();
}
}
void addbooks()
{
system("cls");
FILE * addb;
addb=fopen ("addbooks.txt","a");
printf("book title : ");
gets(book[ind].title);
printf("book author : ");
gets(book[ind].author);
printf("book publisher : ");
gets(book[ind].publisher);
printf("book ISBN : ");
gets(book[ind].ISBN);
printf("book dateofpublication : ");
gets(book[ind].dateofpublication);
printf("book numberofcopies : ");
scanf("%d",&book[ind].numberofcopies);
getchar();
book[ind].currentavailablenumofcopies=book[ind].numberofcopies;
printf("book category : ");
gets(book[ind].category);
fprintf(addb,"- %s,%s,%s,%s,%s,%d,%d,%s\n",book[ind].title,book[ind].author,book[ind].publisher,book[ind].ISBN,book[ind].dateofpublication,book[ind].numberofcopies,book[ind].currentavailablenumofcopies,book[ind].category);
fclose(addb);
ind++;
int z;
do{
printf("Do you want to add more books ?\n");
printf("Press 1 for Yes\nPress 2 for No\n\n");
z=getch();
switch(z){
case '1':
addbooks();
case '2':
mainmenu();
}
printf("Wrong input Plz answer the question with reasonable answers 1 or 2");
delay(2000);
system("cls");
}while(z!=1 || z!=2);
}
void addcopies()
{
int a,b;
int i=0;
FILE * addb;
system("cls");
printf("Enter the ISBN of the book you want to add copies of it \n");
scanf("%d",&a);
getchar();
printf("Enter the number of copies to add \n");
scanf("%d",&b);
getchar();
addb=fopen ("addbooks.txt","a");
for (i=0 ; i<1000 ; ){
if (a==book[i].ISBN){
printf("Found ISBN of book %d\n",i);
book[i].numberofcopies+=b;
book[i].currentavailablenumofcopies+=b;
printf("number of copies is : %d\n", book[i].numberofcopies);
printf("number of available copies is : %d\n",book[i].currentavailablenumofcopies);
break;
}
else
i++;
}
if (i==1000){
printf("not found\n");
}
fclose(addb);
int z;
do{
printf("Do you want to add copies again ? \n");
printf("1 for yes\n2 for no\n\n");
z=getch();
switch(z){
case '1':
addcopies();
case '2':
bookmang();
}
printf("Wrong input Plz answer the question with reasonable answers 1 or 2");
delay(2000);
system("cls");
}while(z!=1 || z!=2);
}
void savefile ()
{
FILE*addb;
char booksave[1000],*temp;
addb=fopen("addbooks.txt","r");
while(fgets(booksave,1000,addbooks)!=NULL)
{
temp=strtok(booksave,",");
strcpy(book[ind].title,temp);
temp=strtok(NULL,",");
strcpy(book[ind].author,temp);
temp=strtok(NULL,",");
strcpy(book[ind].publisher,temp);
temp=strtok(NULL,",");
strcpy(book[ind].ISBN,temp);
temp=strtok(NULL,",");
strcpy(book[ind].dateofpublication,temp);
temp=strtok(NULL,",");
book[ind].numberofcopies=atoi(temp);
temp=strtok(NULL,",");
book[ind].currentavailablenumofcopies=atoi(temp);
temp=strtok(NULL,",");
strcpy(book[ind].category,temp);
ind++;
}
ind2=ind;
for (i=0 ; i<ind2 ; i++)
{
}
}