Write a program in C that simulates a book library database. We want to have two
ID: 3807475 • Letter: W
Question
Write a program in C that simulates a book library database. We want to have two actions: Insert a new book record Print the list of all the available books and its features Each book has the following information: Title Author Year Price The program should ask the user what he wants to do: Insertion (1) Print (2) Exit (3) If the user chooses Insertion, he must be asked to enter all the information about the book. Once he finishes, he should get the message "A new record was created" and he should be returned to the initial choice (Insertion (1), Print (2), Exit 3). If he chooses to print, the program should print all the book records that he has entered so far along with all the information. The program must run continuously until the user chooses Exit (3).Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
//Crates a structure for Book
struct BookLib
{
//Structure members
char title[80];
char author [20];
int year;
float price;
};//End of structure
//Function to insert a record to file
void insert()
{
//Creates a structure object dynamically
struct BookLib *myBook = malloc(sizeof(struct BookLib));
fflush(stdin);
//Accepts data
printf(" Enter Book Title: ");
gets(myBook -> title);
printf(" Enter Author Name: ");
gets(myBook -> author);
printf(" Enter Year: ");
scanf("%d", &myBook -> year);
printf(" Enter Book Price: ");
scanf("%f", &myBook -> price);
//Opens file in append mode
FILE * file= fopen("Book", "a");
//Checks whether file does not contains null
if (file != NULL)
{
//Write the object to file
fwrite(myBook, sizeof(struct BookLib), 1, file);
//Close the file
fclose(file);
}//End of if
//Other wise show error message
else
{
printf(" Cannot create file. ");
}//End of else
}//End of function
//Function to display book information
void display()
{
//Creates a structure object dynamically
struct BookLib *myBook = malloc(sizeof(struct BookLib));
//Opens the file to read
FILE * file= fopen("Book", "rb");
//If not null
if (file != NULL)
{
//Read till end of file
while(!feof(file))
{
//Reads a record and store it in structure object
fread(myBook, sizeof(struct BookLib), 1, file);
//Display the information
printf("%s %s %d %f ", myBook -> title, myBook -> author, myBook -> year, myBook -> price);
}//End of while
//Close file
fclose(file);
}//End of if
//Otherwise show error message
else
{
printf(" Cannot open file. ");
return;
}//end of else
}//End of function
//Function to show menu and returns user choice
int menu()
{
int ch;
printf(" 1 for Insert 2 for Print 3 for Exit ");
printf(" Enter your choice: ");
scanf("%d", &ch);
return ch;
}//End of function
//Main function
int main()
{
int ch;
//Loops till user choice
do
{
ch = menu();
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf(" Invalid choice.");
}//End of switch
}while(1);//End of do - while
}//End of main function
Output:
1 for Insert
2 for Print
3 for Exit
Enter your choice: 1
Enter Book Title: Program C++
Enter Author Name: Ditel
Enter Year: 2001
Enter Book Price: 400.20
1 for Insert
2 for Print
3 for Exit
Enter your choice: 1
Enter Book Title: Java
Enter Author Name: Pyari Mohan
Enter Year: 2016
Enter Book Price: 600.10
1 for Insert
2 for Print
3 for Exit
Enter your choice: 2
Program C++ Ditel 2001 400.200012
Java Pyari Mohan 2016 600.099976
1 for Insert
2 for Print
3 for Exit
Enter your choice: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.