Answer to Question #222570 in C++ for Adilyousafmug20

Question #222570
Q3.Create a bookstore in the following manner: 10
• Add books in a bookstore using a member function, count and display the number of books in a bookstore
1
Expert's answer
2021-08-02T15:55:52-0400
#include <iostream>
#include <string>


using namespace std;




struct Book{
	int id;
	string type;
	string bookName;
	string authorName;
	double price;
};


void addNewBook(struct Book bookstore[],int* totalBooks);




int main(){
	//10 bookstore
	struct Book bookstore[10];
	int totalBooks=0;


	int choice=0;
	while(choice!=8){
		cout<<"1. Add a new book in a bookstore\n";
		cout<<"2. Count and display the number of bookstore in a bookstore\n";
		cout<<"3. Exit\n";
		cout<<"Your choice: ";
		cin>>choice;
		switch (choice)
		{
		case 1:
			{
				addNewBook(bookstore,&totalBooks);
			}
			break;


		case 2:
			if(totalBooks>0){
				cout<<"The number of bookstore in a bookstore: "<<totalBooks<<"\n\n";
				for(int i=0;i<totalBooks;i++){
					cout<<"The book id: "<<bookstore[i].id<<"\n";
					cout<<"The book type: "<<bookstore[i].type<<"\n";
					cout<<"The book book name: "<<bookstore[i].bookName<<"\n";
					cout<<"The book author name: "<<bookstore[i].authorName<<"\n";
					cout<<"The book price: "<<bookstore[i].price<<"\n\n";
				}
			}else{
				cout<<"\nThe bookstore is empty.\n";
			}
			break;
		case 3:
			//exit
			break;
		default:
			break;
		}	
	}


	return 0;
}


void addNewBook(struct Book bookstore[],int* totalBooks){
	cout<<"Enter the book id: ";
	cin>>bookstore[*totalBooks].id;
	cin.ignore();
	cout<<"Enter the book type: ";
	getline(cin,bookstore[*totalBooks].type);
	cout<<"Enter the book name: ";
	getline(cin,bookstore[*totalBooks].bookName);
	cout<<"Enter the book author name: ";
	getline(cin,bookstore[*totalBooks].authorName);
	cout<<"Enter the book price: ";
	cin>>bookstore[*totalBooks].price;
	*totalBooks=*totalBooks+1;
	cout<<"\nA new book has been added.\n";
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS