You are on page 1of 5

COMSATS University Islamabad (Lahore Campus)

Assignment 4– SPRING 2022


Course Title: Introduction to Computer Programming Course Code: CS141 Credit Hours: 3+1
Course Instructor: Kisaa Fatima Programme Name: BS-CHE
Due Date: 20.6.2022 Maximum Marks: 10
Important Instructions / Guidelines:

Nouman Mushtaq

FA20-CHE-085

Question No 1. Marks: 10
CLO:4; C5: PLO3
Learning Objectives: Demonstrate the knowledge of Structs.
Question 1:
Develop a program which handled book information for a bookshop. Your program should
store following information of a book:
BoodID, BookName, Author Name, Price, PublishingDate
In main create a menu and ask user’s choice:
1. Press 1 to add a book
2. Press 2 to display a book
3. Press 3 to display a book

Create functions to Add book. Ask user to input the information of a book. You have to add
at least 5 books.
Create function to display all book in tabular format.
Create a function to search a book by id. If the book found, display complete information of
that book.

You will be evaluated on the following basis:


1. Functions defined in the program.
2. Coding style
3. Output format
Sample Output:
(In sample output books are not displayed in tabular form but you have to print in
tabular form)

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
int b_id;
char b_name[60];
char b_author[40];
float b_price;
int b_publishingDate;
};
void AddBook(struct book *book, int n)
{
fflush(stdin);
printf("-------------------------------------\n");
printf("Add Details of %d Book\n",n);
printf("-------------------------------------\n");
for(int i = 0 ; i < n ; i++)
{
fflush(stdin);
printf("Enter Book ID : ");
scanf("%d",&book[i].b_id);
fflush(stdin);
printf(" Enter Book title : ");
scanf(" %[^\n]%*c", &book[i].b_name);
fflush(stdin);
printf("Enter Author Name : ");

scanf(" %[^\n]%*c", &book[i].b_author);


printf("Enter Price : ");
scanf("%f",&book[i].b_price);
printf("Enter Publishing Date: ");
scanf("%d",&book[i].b_publishingDate);
printf("-------------------------------------\n");
}
}
void display(struct book *b, int n)
{
printf("\n\t\tDetails of All Book");
printf("\n-----------------------------------------------------------\n");
printf("Book ID. Book Name\t Author Name\t Price\t publishing Date");
printf("\n------------------------------------------------------------");
for(int i = 0 ; i < n ; i++)
{
printf("\n %d\t %s\t %s\t %.2f\t
%d",b[i].b_id,b[i].b_name,b[i].b_author,b[i].b_price, b[i].b_publishingDate);
}
printf("\n\n");
}
void SearchByID(struct book *b, int n)
{
int bookid;
printf("\nEnter Book id: ");
scanf("%d",&bookid);
printf("--------------------------------------");
for(int i = 0 ; i < n ; i++)
{
if(b[i].b_id == bookid)
{
printf("\n-----------------------------------------------------------\n");
printf("Book No. Book Name\t Author Name\t Price\t publishing
Date");
printf("\n------------------------------------------------------------");
printf("\n %d\t %s\t %s\t %.2f\t
%d",b[i].b_id,b[i].b_name,b[i].b_author,b[i].b_price, b[i].b_publishingDate);
printf("\n\n");
break;
}
}
}
int main()
{
struct book b[20];
int ch, i, n, count = 0;
for(i=0;i<n;i++)
{
fflush(stdin);
printf("\t\tMENU");
printf("\n-------------------------------------\n");
printf("PRESS 1. To ADD BOOK.");
printf("\nPRESS 2. To DISPLAY BOOK.");
printf("\nPRESS 3. TO DISPLAY BOOK OF GIVEN ID.");
printf("\nPRESS 4. TO EXIT.");
printf("\n-------------------------------------\n");
printf("Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow Many Records You Want to Add: ");
scanf("%d",&n);
AddBook(b, n);
break;
case 2:
display(b, n);
break;
case 3:
SearchByID(b,n);
break;
case 4 :
exit(0);
}
}
return 0;
}

Output:

You might also like