Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 2 Write a program in C that simulates a book library database. We want

ID: 671202 • Letter: E

Question

Exercise 2

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 want

s

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 pri

nt all the book records

th

at 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<conio.h>
   struct book
   {
       char Title[20];
       int Year;
       char Author[20];
   };
   struct book b[5];

void ins(int n)
{
   printf("enter book Title");
   scanf("%s",&b[n].Title);

   printf("enter book Author");
   scanf("%s",&b[n].Author);

   printf("enter book Year");
   scanf("%s",&b[n].Year);

   printf("Inserted");
}

void print(int n)
{
   int i;
   for(i=0;i<n;i++)
   {
       printf("%s",b[i].Title);
       printf("%s",b[i].Author);
       printf("%d",b[i].Year);
   }
}

void main()
{
   //struct book b[5];

   int a,i=0;

   while(1)
   {

   clrscr();

   printf("1. Insertion");
   printf(" 2. Print");
   printf(" 3. Exit");

   scanf("%d",&a);

   switch(a)
   {
       case 1:
           ins (i);
           i++;
           break;
       case 2:
           print(i);
           break;
       case 3:
           return;
   }
   }
}