Answered step by step
Verified Expert Solution
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; int totalBooks ; Function to add a new book to the list void addBook if totalBooks cout "Cannot add more books. Array full." endl; return; bookRecord newBook; cout "Enter title: ; cin.ignore; getlinecin newBook.title; cout "Enter author: ; getlinecin newBook.author; cout "Enter stock: ; cin newBook.stock; cout "Enter price: ; cin newBook.price; cout "Enter publisher: ; cin.ignore; getlinecin newBook.publisher; bookstotalBooks newBook; cout "Book added successfully." endl; Function to display details of all books in the shop void displayBooks if totalBooks cout No books available." endl; return; cout "Books in the shop:" endl; for int i ; i totalBooks; i cout "Title: booksititle endl; cout "Author: booksiauthor endl; cout "Stock: booksistock endl; cout "Price: booksiprice endl; cout "Publisher: booksipublisher 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; getlinecin title; cout "Enter author of the book: ; getlinecin author; bool found false; for int i ; i totalBooks; i if booksititle title && booksiauthor author found true; cout "Book details:" endl; cout "Title: booksititle endl; cout "Author: booksiauthor endl; cout "Stock: booksistock endl; cout "Price: booksiprice endl; cout "Publisher: booksipublisher 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; getlinecin title; cout "Enter author of the book: ; getlinecin author; cout "Enter number of copies to sell: ; cin copies; bool found false; for int i ; i totalBooks; i if booksititle title && booksiauthor author found true; if booksistock copies cout "Total cost: copies booksiprice endl; booksistock 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 Add a new book" endl; cout Display all books" endl; cout Search for a book by title" endl; cout Sell books to a customer" endl; cout Exit" endl; cout "Enter your choice: ; cin choice; switchchoice case : addBook; break; case : displayBooks; break; case : searchBook; break; case : sellBook; break; case : cout "Exiting program. Goodbye!" endl; break; default: cout "Invalid choice. Please try again." endl; while choice ; return ;
#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;
int totalBooks ;
Function to add a new book to the list
void addBook
if totalBooks
cout "Cannot add more books. Array full." endl;
return;
bookRecord newBook;
cout "Enter title: ;
cin.ignore;
getlinecin newBook.title;
cout "Enter author: ;
getlinecin newBook.author;
cout "Enter stock: ;
cin newBook.stock;
cout "Enter price: ;
cin newBook.price;
cout "Enter publisher: ;
cin.ignore;
getlinecin newBook.publisher;
bookstotalBooks newBook;
cout "Book added successfully." endl;
Function to display details of all books in the shop
void displayBooks
if totalBooks
cout No books available." endl;
return;
cout "Books in the shop:" endl;
for int i ; i totalBooks; i
cout "Title: booksititle endl;
cout "Author: booksiauthor endl;
cout "Stock: booksistock endl;
cout "Price: booksiprice endl;
cout "Publisher: booksipublisher 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;
getlinecin title;
cout "Enter author of the book: ;
getlinecin author;
bool found false;
for int i ; i totalBooks; i
if booksititle title && booksiauthor author
found true;
cout "Book details:" endl;
cout "Title: booksititle endl;
cout "Author: booksiauthor endl;
cout "Stock: booksistock endl;
cout "Price: booksiprice endl;
cout "Publisher: booksipublisher 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;
getlinecin title;
cout "Enter author of the book: ;
getlinecin author;
cout "Enter number of copies to sell: ;
cin copies;
bool found false;
for int i ; i totalBooks; i
if booksititle title && booksiauthor author
found true;
if booksistock copies
cout "Total cost: copies booksiprice
endl;
booksistock 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 Add a new book" endl;
cout Display all books" endl;
cout Search for a book by title" endl;
cout Sell books to a customer" endl;
cout Exit" endl;
cout "Enter your choice: ;
cin choice;
switchchoice
case :
addBook;
break;
case :
displayBooks;
break;
case :
searchBook;
break;
case :
sellBook;
break;
case :
cout "Exiting program. Goodbye!" endl;
break;
default:
cout "Invalid choice. Please try again." endl;
while choice ;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started