Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include using namespace std; / / Structure to represent a book record struct bookRecord { string title; string author; int stock; double price; string

#include
#include
using namespace std;
// Structure to represent a book record
struct bookRecord {
string title;
string author;
int stock;
double price;
string publisher;
};
// Global array to store book records
bookRecord books[100];
int totalBooks =0;
// Function to add a new book to the list
void addBook(){
if (totalBooks >=100){
cout << "Cannot add more books. Array full." << endl;
return;
}
bookRecord newBook;
cout << "Enter title: ";
cin.ignore();
getline(cin, newBook.title);
cout << "Enter author: ";
getline(cin, newBook.author);
cout << "Enter stock: ";
4
cin >> newBook.stock;
cout << "Enter price: ";
cin >> newBook.price;
cout << "Enter publisher: ";
cin.ignore();
getline(cin, newBook.publisher);
books[totalBooks++]= newBook;
cout << "Book added successfully." << endl;
}
// Function to display details of all books in the shop
void displayBooks(){
if (totalBooks ==0){
cout <<"No books available." << endl;
return;
}
cout << "Books in the shop:" << endl;
for (int i =0; i < totalBooks; ++i){
cout << "Title: "<< books[i].title << endl;
cout << "Author: "<< books[i].author << endl;
cout << "Stock: "<< books[i].stock << endl;
cout << "Price: "<< books[i].price << endl;
cout << "Publisher: "<< books[i].publisher << endl;
cout <<"---------------------"<< endl;
}
}
// Function to search for a book by title and author
void searchBook(){
string title, author;
cout << "Enter title of the book: ";
cin.ignore();
getline(cin, title);
cout << "Enter author of the book: ";
getline(cin, author);
5
bool found = false;
for (int i =0; i < totalBooks; ++i){
if (books[i].title == title && books[i].author == author){
found = true;
cout << "Book details:" << endl;
cout << "Title: "<< books[i].title << endl;
cout << "Author: "<< books[i].author << endl;
cout << "Stock: "<< books[i].stock << endl;
cout << "Price: "<< books[i].price << endl;
cout << "Publisher: "<< books[i].publisher << endl;
break;
}
}
if (!found){
cout << "Book not found." << endl;
}
}
// Function to sell books to a customer
void sellBook(){
string title, author;
int copies;
cout << "Enter title of the book: ";
cin.ignore();
getline(cin, title);
cout << "Enter author of the book: ";
getline(cin, author);
cout << "Enter number of copies to sell: ";
cin >> copies;
bool found = false;
for (int i =0; i < totalBooks; ++i){
if (books[i].title == title && books[i].author == author){
found = true;
if (books[i].stock >= copies){
cout << "Total cost: "<<(copies * books[i].price)<<
endl;
6
books[i].stock -= copies;
cout << "Books sold successfully." << endl;
} else {
cout << "Required copies not in stock." << endl;
}
break;
}
}
if (!found){
cout << "Book not found." << endl;
}
}
// Main function to test implementation
int main(){
int choice;
do {
cout <<"
Menu:" << endl;
cout <<"1. Add a new book" << endl;
cout <<"2. Display all books" << endl;
cout <<"3. Search for a book by title" << endl;
cout <<"4. Sell books to a customer" << endl;
cout <<"5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice){
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
searchBook();
break;
case 4:
7
sellBook();
break;
case 5:
cout << "Exiting program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice !=5);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions