SlideShare a Scribd company logo
1 of 16
Download to read offline
fully comments for my program, thank you will thumb up
#include
#include
#include
#include
using namespace std;
struct book
{
int ISBN;
string Author;
string Title;
string publisher;
int Quantity;
double price;
};
void choice1(book books[], int& size, int MAX_SIZE)
{
ifstream inFile;
inFile.open("inventory.txt");
if (inFile.fail())
cout <<"file could not open"<> books[size].ISBN;
cout << "Enter the author name: ";
cin >> books[size].Author;
cout << "Enter the book tile: ";
cin >> books[size].Title;
cin.get();
cout << "Enter the books quantity: ";
cin >> books[size].Quantity;
cout << "Enter the book price: $";
cin >> books[size].price;
size++;
cout << "You have successfully inserted an entry." << endl;
}
}
void choice3(book books[], int& size)
{
if(size == 0)
cout << "Array is empty. Read the data first." << endl;
else
{
int isbn;
cout << " Enter the ISBN of the book: ";
cin >> isbn;
for(int i = 0; i < size; i++)
{
if(books[i].ISBN == isbn)
{
int j = i;
while(j < size - 1)
{
books[j] = books[j + 1];
j++;
}
size--;
break;
}
}
cout << "You have successfully deleted an entry." << endl;
}
}
void choice4(book books[], int size)
{
if(size == 0)
cout << "Array is empty. Read the data first." << endl;
else
{
int isbn;
int option;
int qty;
cout << " Enter the ISBN of the book: ";
cin >> isbn;
cout << "1. Increment" << endl;
cout << "2. Decrement" << endl;
cout << "3. Add New" << endl;
cout << "Enter your option: ";
cin >> option;
cout << "Enter the quantity: ";
cin >> qty;
for(int i = 0; i < size; i++)
{
if(books[i].ISBN == isbn)
{
if(option == 1)
books[i].Quantity += qty;
else if(option == 2)
{
books[i].Quantity -= qty;
if(books[i].Quantity)
books[i].Quantity = 0;
}
else if(option == 3)
books[i].Quantity = qty;
break;
}
}
cout << "You have successfully updated the array." << endl;
}
}
void choice5(book books[], int size)
{
for(int i = 1; i < size; i++)
{
book current = books[i];
int j = i;
while(j > 0 && (books[j - 1].Title).compare(current.Title) > 0)
{
books[j] = books[j - 1];
j--;
}
books[j] = current;
}
if(size != 0)
cout << "You have successfully sorted the array." << endl;
else
cout << "Array is empty. Read the data first." << endl;
}
void choice6(book books[], int size)
{
for(int i = 0; i < size; i++)
{
cout << endl;
cout << "Book Number: " << (i + 1) << endl;
cout << "ISBN: " << books[i].ISBN << endl;
cout << "Author: " << books[i].Author << endl;
cout << "Title: " << books[i].Title << endl;
cout << "Quantity: " << books[i].Quantity << endl;
cout << "Price: $" << books[i].price << endl;
}
if(size != 0)
cout << "You have successfully printed the array." << endl;
else
cout << "Array is empty. Read the file first." << endl;
}
void choice7(book books[], int size)
{
ofstream outFile;
outFile.open("finalData.dat");
for(int i = 0; i < size; i++)
{
outFile << "Book Number: " << (i + 1) << endl;
outFile << "ISBN: " << books[i].ISBN << endl;
outFile << "Author: " << books[i].Author << endl;
outFile << "Title: " << books[i].Title << endl;
outFile << "Quantity: " << books[i].Quantity << endl;
outFile << "Price: $" << books[i].price << endl << endl;
}
if(size != 0)
cout << "You have successfully printed the array." << endl;
else
cout << "Array is empty. Read the file first." << endl;
outFile.close();
}
// File: Boookstore.cpp
#include
#include"yuan.h"
using namespace std;
int main()
{
const int MAX_SIZE=100;
int size = 0;
int choice;
book books[MAX_SIZE];
do
{
cout << "1: Read inventory forn file" << endl;
cout << "2: Add an entry" << endl;
cout << "3: Delete an entry" << endl;
cout << "4: Update an entry" << endl;
cout << "5: Sort inventory" << endl;
cout << "6: Display Inventory" << endl;
cout << "7: Write inventory to file and exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
choice1(books, size, MAX_SIZE);
break;
case 2:
choice2(books, size, MAX_SIZE);
break;
case 3:
choice3(books, size);
break;
case 4:
choice4(books, size);
break;
case 5:
choice5(books, size);
break;
case 6:
choice6(books, size);
break;
case 7:
choice7(books, size);
cout << "Thank you." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
cout << endl;
}while(choice != 7);
return 0;
}
fully comments for my program, thank you will thumb up
Solution
Comments added.
//header files
#include
#include
#include
#include
using namespace std;
//Define a structure book
struct book
{
//structure variables
int ISBN;
string Author;
string Title;
string publisher;
int Quantity;
double price;
};
//fuction that is called when user selects choice 1 (read inventory from file)
//it accepts an array of structure type book and reference of size and MAX_SIZE as input
parameters
void choice1(book books[], int& size, int MAX_SIZE)
{
//open a file
ifstream inFile;
inFile.open("inventory.txt");
//check if file is open or failed
if (inFile.fail())
//if file not opened print a error message
cout <<"file could not open"<> books[size].ISBN;
//read Authour name from the user and store it in books array
cout << "Enter the author name: ";
cin >> books[size].Author;
//read title from the user and store it in books array
cout << "Enter the book tile: ";
cin >> books[size].Title;
cin.get();
//read Quantity from the user and store it in books array
cout << "Enter the books quantity: ";
cin >> books[size].Quantity;
//read price from the user and store it in books array
cout << "Enter the book price: $";
cin >> books[size].price;
//increment size of structure array
size++;
//display success message
cout << "You have successfully inserted an entry." << endl;
}
}
//this is the function that executes when user selects choice 3(delete an entry)
//it accepts an array of structure type book and reference of size
void choice3(book books[], int& size)
{
//check if the structure if empty
if(size == 0)
//if empty display error message
cout << "Array is empty. Read the data first." << endl;
else
{
//else read the ISBN of the book which has to be deleted
int isbn;
cout << " Enter the ISBN of the book: ";
cin >> isbn;
//loop through the books array
for(int i = 0; i < size; i++)
{
//for each book encountered check if the cuurent ISBN is equal to ISBN entered by user
if(books[i].ISBN == isbn)
{
//if found, replace current book with next book, so that current book will not be
available anymore
int j = i;
while(j < size - 1)
{
books[j] = books[j + 1];
j++;
}
//decrement size
size--;
//exit loop
break;
}
}
//display success message
cout << "You have successfully deleted an entry." << endl;
}
}
//function that is executed when user selects choice 4(update book details)
void choice4(book books[], int size)
{
//check if the books is empty
if(size == 0)
//if yes display error message
cout << "Array is empty. Read the data first." << endl;
else
{
//else read the new details and update the book
int isbn;
int option;
int qty;
//read the ISBN of the book which has to be updated
cout << " Enter the ISBN of the book: ";
cin >> isbn;
//ask the user whether he want to increment or decrement the quatity or change the quantity
to new value
cout << "1. Increment" << endl;
cout << "2. Decrement" << endl;
cout << "3. Add New" << endl;
//read user choice
cout << "Enter your option: ";
cin >> option;
//read the quantity
cout << "Enter the quantity: ";
cin >> qty;
//loop throught the books array
for(int i = 0; i < size; i++)
{
//check if current book encountered is the book we want to update
if(books[i].ISBN == isbn)
{
//if increment is selected , increment the quantity by the quantity entered
if(option == 1)
books[i].Quantity += qty;
//if decrement is selected
else if(option == 2)
{
//decrement the quantity by the quantity entered
books[i].Quantity -= qty;
//if quantity is 1 set it to 0
if(books[i].Quantity)
books[i].Quantity = 0;
}
else if(option == 3)
//if option 3 is selected, overwrite the current quantity with new quantity
books[i].Quantity = qty;
//exit loop
break;
}
}
cout << "You have successfully updated the array." << endl;
}
}
//function that executes when choice 5 is selected (sort inventory)
void choice5(book books[], int size)
{
//loop through the books array
for(int i = 1; i < size; i++)
{
//declare a new struct variable and assign the current book encountered to the new variable
book current = books[i];
//declare j and set it to i
int j = i;
//if j>0 and compare title of previous book with current book
while(j > 0 && (books[j - 1].Title).compare(current.Title) > 0)
{
//if previous book title is greater than current book title, move previous book to current
book place
books[j] = books[j - 1];
//decremet j
j--;
}
//assign current book to next book
books[j] = current;
}
//if book array size is not 0, display success message else display error message
if(size != 0)
cout << "You have successfully sorted the array." << endl;
else
cout << "Array is empty. Read the data first." << endl;
}
//function that executes when user selects choice 6(print inventory)
void choice6(book books[], int size)
{
//loop through the array
for(int i = 0; i < size; i++)
{
//print book details each in a new line
cout << endl;
cout << "Book Number: " << (i + 1) << endl;
cout << "ISBN: " << books[i].ISBN << endl;
cout << "Author: " << books[i].Author << endl;
cout << "Title: " << books[i].Title << endl;
cout << "Quantity: " << books[i].Quantity << endl;
cout << "Price: $" << books[i].price << endl;
}
//if book array size is not 0, display success message else display error message
if(size != 0)
cout << "You have successfully printed the array." << endl;
else
cout << "Array is empty. Read the file first." << endl;
}
//function that executes when user selects choice 7(write inventory to file)
void choice7(book books[], int size)
{
//create a new file to which inventory has to be written
ofstream outFile;
outFile.open("finalData.dat");
//loop through the array
for(int i = 0; i < size; i++)
{
//write inventory details to file
outFile << "Book Number: " << (i + 1) << endl;
outFile << "ISBN: " << books[i].ISBN << endl;
outFile << "Author: " << books[i].Author << endl;
outFile << "Title: " << books[i].Title << endl;
outFile << "Quantity: " << books[i].Quantity << endl;
outFile << "Price: $" << books[i].price << endl << endl;
}
//if book array size is not 0, display success message else display error message
if(size != 0)
cout << "You have successfully printed the array." << endl;
else
cout << "Array is empty. Read the file first." << endl;
//close file
outFile.close();
}
// File: Boookstore.cpp
#include
#include"yuan.h"
using namespace std;
int main()
{
//declare max size of structure
const int MAX_SIZE=100;
//set initial size to 0
int size = 0;
//variable to read choice
int choice;
//array of structure typr
book books[MAX_SIZE];
//loop that repeats it executes until the user exits
do
{
//display menu
cout << "1: Read inventory forn file" << endl;
cout << "2: Add an entry" << endl;
cout << "3: Delete an entry" << endl;
cout << "4: Update an entry" << endl;
cout << "5: Sort inventory" << endl;
cout << "6: Display Inventory" << endl;
cout << "7: Write inventory to file and exit" << endl;
//read choice from user
cout << "Enter your choice: ";
cin >> choice;
//call the appropriate function based on the choice of the user
switch(choice)
{
//if choice 1 is selected, call function choice1, pass books array,size and max size as input
parameters
case 1:
choice1(books, size, MAX_SIZE);
break;
//if choice 2 is selected, call function choice2, pass books array,size and max size as input
parameters
case 2:
choice2(books, size, MAX_SIZE);
break;
//if choice 3 is selected, call function choice3, pass books array and size as input
parameters
case 3:
choice3(books, size);
break;
//if choice 4 is selected, call function choice4, pass books array and size as input
parameters
case 4:
choice4(books, size);
break;
//if choice 5 is selected, call function choice5, pass books array and size as input
parameters
case 5:
choice5(books, size);
break;
//if choice 6 is selected, call function choice6, pass books array and size as input
parameters
case 6:
choice6(books, size);
break;
//if choice 7 is selected, call function choice7, pass books array and size as input
parameters
case 7:
choice7(books, size);
cout << "Thank you." << endl;
break;
//if invalid choice is entered display error message
default:
cout << "Invalid choice!" << endl;
}
cout << endl;
}while(choice != 7); //repeat execution until user wnts to exit
return 0;
}

More Related Content

More from arjuncp10

In 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdf
In 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdfIn 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdf
In 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdf
arjuncp10
 
Diversity Paper Each student will complete a diversity research assig.pdf
Diversity Paper Each student will complete a diversity research assig.pdfDiversity Paper Each student will complete a diversity research assig.pdf
Diversity Paper Each student will complete a diversity research assig.pdf
arjuncp10
 
Define intermediate phenotype and then imagine some intermediate phe.pdf
Define intermediate phenotype and then imagine some intermediate phe.pdfDefine intermediate phenotype and then imagine some intermediate phe.pdf
Define intermediate phenotype and then imagine some intermediate phe.pdf
arjuncp10
 
Can “discovery science” (for example, the discovery of a new species.pdf
Can “discovery science” (for example, the discovery of a new species.pdfCan “discovery science” (for example, the discovery of a new species.pdf
Can “discovery science” (for example, the discovery of a new species.pdf
arjuncp10
 
You are given a mixed culture containing a anaerobic thermophile, ae.pdf
You are given a mixed culture containing a anaerobic thermophile, ae.pdfYou are given a mixed culture containing a anaerobic thermophile, ae.pdf
You are given a mixed culture containing a anaerobic thermophile, ae.pdf
arjuncp10
 
What do you call the cellular process of RNA production What is the.pdf
What do you call the cellular process of RNA production  What is the.pdfWhat do you call the cellular process of RNA production  What is the.pdf
What do you call the cellular process of RNA production What is the.pdf
arjuncp10
 

More from arjuncp10 (20)

In 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdf
In 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdfIn 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdf
In 2006 the CEO of Bear Sterns, James Caynes, received a compensatio.pdf
 
How would a CFE devise a plan to prevent subsequent employee fraud.pdf
How would a CFE devise a plan to prevent subsequent employee fraud.pdfHow would a CFE devise a plan to prevent subsequent employee fraud.pdf
How would a CFE devise a plan to prevent subsequent employee fraud.pdf
 
Hilary rode her horse for 8 miles until it was hurt.Then she walked .pdf
Hilary rode her horse for 8 miles until it was hurt.Then she walked .pdfHilary rode her horse for 8 miles until it was hurt.Then she walked .pdf
Hilary rode her horse for 8 miles until it was hurt.Then she walked .pdf
 
Find the admittance Yab in the circuit seen in the figure. Take that.pdf
Find the admittance Yab in the circuit seen in the figure. Take that.pdfFind the admittance Yab in the circuit seen in the figure. Take that.pdf
Find the admittance Yab in the circuit seen in the figure. Take that.pdf
 
Diversity Paper Each student will complete a diversity research assig.pdf
Diversity Paper Each student will complete a diversity research assig.pdfDiversity Paper Each student will complete a diversity research assig.pdf
Diversity Paper Each student will complete a diversity research assig.pdf
 
Detailed solutions please 1. Let R and S be commutative rings and le.pdf
Detailed solutions please 1. Let R and S be commutative rings and le.pdfDetailed solutions please 1. Let R and S be commutative rings and le.pdf
Detailed solutions please 1. Let R and S be commutative rings and le.pdf
 
Describe the mechanisms of asexual reproduction inProkaryotesPr.pdf
Describe the mechanisms of asexual reproduction inProkaryotesPr.pdfDescribe the mechanisms of asexual reproduction inProkaryotesPr.pdf
Describe the mechanisms of asexual reproduction inProkaryotesPr.pdf
 
Define intermediate phenotype and then imagine some intermediate phe.pdf
Define intermediate phenotype and then imagine some intermediate phe.pdfDefine intermediate phenotype and then imagine some intermediate phe.pdf
Define intermediate phenotype and then imagine some intermediate phe.pdf
 
Consider a population of lizards living on the coast of Africa. A sto.pdf
Consider a population of lizards living on the coast of Africa. A sto.pdfConsider a population of lizards living on the coast of Africa. A sto.pdf
Consider a population of lizards living on the coast of Africa. A sto.pdf
 
Can “discovery science” (for example, the discovery of a new species.pdf
Can “discovery science” (for example, the discovery of a new species.pdfCan “discovery science” (for example, the discovery of a new species.pdf
Can “discovery science” (for example, the discovery of a new species.pdf
 
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdfC++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
 
All of the following are features or functions of nanobodies except _.pdf
All of the following are features or functions of nanobodies except _.pdfAll of the following are features or functions of nanobodies except _.pdf
All of the following are features or functions of nanobodies except _.pdf
 
A girl running at a constant speed of 1.4ms in a straight line thro.pdf
A girl running at a constant speed of 1.4ms in a straight line thro.pdfA girl running at a constant speed of 1.4ms in a straight line thro.pdf
A girl running at a constant speed of 1.4ms in a straight line thro.pdf
 
You have a rural dial-up customer who complains that a large number .pdf
You have a rural dial-up customer who complains that a large number .pdfYou have a rural dial-up customer who complains that a large number .pdf
You have a rural dial-up customer who complains that a large number .pdf
 
You are given a mixed culture containing a anaerobic thermophile, ae.pdf
You are given a mixed culture containing a anaerobic thermophile, ae.pdfYou are given a mixed culture containing a anaerobic thermophile, ae.pdf
You are given a mixed culture containing a anaerobic thermophile, ae.pdf
 
Woyld removing phenylethyl alcohol from PEA alter the mediumsS.pdf
Woyld removing phenylethyl alcohol from PEA alter the mediumsS.pdfWoyld removing phenylethyl alcohol from PEA alter the mediumsS.pdf
Woyld removing phenylethyl alcohol from PEA alter the mediumsS.pdf
 
Which abstraction uses slates, state transitions, inputs and outputs .pdf
Which abstraction uses slates, state transitions, inputs and outputs .pdfWhich abstraction uses slates, state transitions, inputs and outputs .pdf
Which abstraction uses slates, state transitions, inputs and outputs .pdf
 
What is the target cell for the hormone AngiotensionogenSolut.pdf
What is the target cell for the hormone AngiotensionogenSolut.pdfWhat is the target cell for the hormone AngiotensionogenSolut.pdf
What is the target cell for the hormone AngiotensionogenSolut.pdf
 
What is a possible evolutionary advantage of having intronsexons in.pdf
What is a possible evolutionary advantage of having intronsexons in.pdfWhat is a possible evolutionary advantage of having intronsexons in.pdf
What is a possible evolutionary advantage of having intronsexons in.pdf
 
What do you call the cellular process of RNA production What is the.pdf
What do you call the cellular process of RNA production  What is the.pdfWhat do you call the cellular process of RNA production  What is the.pdf
What do you call the cellular process of RNA production What is the.pdf
 

Recently uploaded

Recently uploaded (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 

fully comments for my program, thank you will thumb up#include io.pdf

  • 1. fully comments for my program, thank you will thumb up #include #include #include #include using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<> books[size].ISBN; cout << "Enter the author name: "; cin >> books[size].Author; cout << "Enter the book tile: "; cin >> books[size].Title; cin.get(); cout << "Enter the books quantity: "; cin >> books[size].Quantity; cout << "Enter the book price: $"; cin >> books[size].price;
  • 2. size++; cout << "You have successfully inserted an entry." << endl; } } void choice3(book books[], int& size) { if(size == 0) cout << "Array is empty. Read the data first." << endl; else { int isbn; cout << " Enter the ISBN of the book: "; cin >> isbn; for(int i = 0; i < size; i++) { if(books[i].ISBN == isbn) { int j = i; while(j < size - 1) { books[j] = books[j + 1]; j++; } size--; break; } } cout << "You have successfully deleted an entry." << endl; } } void choice4(book books[], int size) {
  • 3. if(size == 0) cout << "Array is empty. Read the data first." << endl; else { int isbn; int option; int qty; cout << " Enter the ISBN of the book: "; cin >> isbn; cout << "1. Increment" << endl; cout << "2. Decrement" << endl; cout << "3. Add New" << endl; cout << "Enter your option: "; cin >> option; cout << "Enter the quantity: "; cin >> qty; for(int i = 0; i < size; i++) { if(books[i].ISBN == isbn) { if(option == 1) books[i].Quantity += qty; else if(option == 2) { books[i].Quantity -= qty; if(books[i].Quantity) books[i].Quantity = 0; } else if(option == 3) books[i].Quantity = qty;
  • 4. break; } } cout << "You have successfully updated the array." << endl; } } void choice5(book books[], int size) { for(int i = 1; i < size; i++) { book current = books[i]; int j = i; while(j > 0 && (books[j - 1].Title).compare(current.Title) > 0) { books[j] = books[j - 1]; j--; } books[j] = current; } if(size != 0) cout << "You have successfully sorted the array." << endl; else cout << "Array is empty. Read the data first." << endl; } void choice6(book books[], int size) { for(int i = 0; i < size; i++) { cout << endl; cout << "Book Number: " << (i + 1) << endl; cout << "ISBN: " << books[i].ISBN << endl; cout << "Author: " << books[i].Author << endl; cout << "Title: " << books[i].Title << endl; cout << "Quantity: " << books[i].Quantity << endl;
  • 5. cout << "Price: $" << books[i].price << endl; } if(size != 0) cout << "You have successfully printed the array." << endl; else cout << "Array is empty. Read the file first." << endl; } void choice7(book books[], int size) { ofstream outFile; outFile.open("finalData.dat"); for(int i = 0; i < size; i++) { outFile << "Book Number: " << (i + 1) << endl; outFile << "ISBN: " << books[i].ISBN << endl; outFile << "Author: " << books[i].Author << endl; outFile << "Title: " << books[i].Title << endl; outFile << "Quantity: " << books[i].Quantity << endl; outFile << "Price: $" << books[i].price << endl << endl; } if(size != 0) cout << "You have successfully printed the array." << endl; else cout << "Array is empty. Read the file first." << endl; outFile.close(); } // File: Boookstore.cpp #include #include"yuan.h" using namespace std; int main() {
  • 6. const int MAX_SIZE=100; int size = 0; int choice; book books[MAX_SIZE]; do { cout << "1: Read inventory forn file" << endl; cout << "2: Add an entry" << endl; cout << "3: Delete an entry" << endl; cout << "4: Update an entry" << endl; cout << "5: Sort inventory" << endl; cout << "6: Display Inventory" << endl; cout << "7: Write inventory to file and exit" << endl; cout << "Enter your choice: "; cin >> choice; switch(choice) { case 1: choice1(books, size, MAX_SIZE); break; case 2: choice2(books, size, MAX_SIZE); break; case 3: choice3(books, size); break; case 4: choice4(books, size); break; case 5: choice5(books, size); break; case 6: choice6(books, size);
  • 7. break; case 7: choice7(books, size); cout << "Thank you." << endl; break; default: cout << "Invalid choice!" << endl; } cout << endl; }while(choice != 7); return 0; } fully comments for my program, thank you will thumb up Solution Comments added. //header files #include #include #include #include using namespace std; //Define a structure book struct book { //structure variables int ISBN; string Author; string Title; string publisher; int Quantity; double price; };
  • 8. //fuction that is called when user selects choice 1 (read inventory from file) //it accepts an array of structure type book and reference of size and MAX_SIZE as input parameters void choice1(book books[], int& size, int MAX_SIZE) { //open a file ifstream inFile; inFile.open("inventory.txt"); //check if file is open or failed if (inFile.fail()) //if file not opened print a error message cout <<"file could not open"<> books[size].ISBN; //read Authour name from the user and store it in books array cout << "Enter the author name: "; cin >> books[size].Author; //read title from the user and store it in books array cout << "Enter the book tile: "; cin >> books[size].Title; cin.get(); //read Quantity from the user and store it in books array cout << "Enter the books quantity: "; cin >> books[size].Quantity; //read price from the user and store it in books array cout << "Enter the book price: $"; cin >> books[size].price; //increment size of structure array size++; //display success message cout << "You have successfully inserted an entry." << endl;
  • 9. } } //this is the function that executes when user selects choice 3(delete an entry) //it accepts an array of structure type book and reference of size void choice3(book books[], int& size) { //check if the structure if empty if(size == 0) //if empty display error message cout << "Array is empty. Read the data first." << endl; else { //else read the ISBN of the book which has to be deleted int isbn; cout << " Enter the ISBN of the book: "; cin >> isbn; //loop through the books array for(int i = 0; i < size; i++) { //for each book encountered check if the cuurent ISBN is equal to ISBN entered by user if(books[i].ISBN == isbn) { //if found, replace current book with next book, so that current book will not be available anymore int j = i; while(j < size - 1) { books[j] = books[j + 1]; j++; } //decrement size size--; //exit loop break;
  • 10. } } //display success message cout << "You have successfully deleted an entry." << endl; } } //function that is executed when user selects choice 4(update book details) void choice4(book books[], int size) { //check if the books is empty if(size == 0) //if yes display error message cout << "Array is empty. Read the data first." << endl; else { //else read the new details and update the book int isbn; int option; int qty; //read the ISBN of the book which has to be updated cout << " Enter the ISBN of the book: "; cin >> isbn; //ask the user whether he want to increment or decrement the quatity or change the quantity to new value cout << "1. Increment" << endl; cout << "2. Decrement" << endl; cout << "3. Add New" << endl; //read user choice cout << "Enter your option: "; cin >> option; //read the quantity cout << "Enter the quantity: ";
  • 11. cin >> qty; //loop throught the books array for(int i = 0; i < size; i++) { //check if current book encountered is the book we want to update if(books[i].ISBN == isbn) { //if increment is selected , increment the quantity by the quantity entered if(option == 1) books[i].Quantity += qty; //if decrement is selected else if(option == 2) { //decrement the quantity by the quantity entered books[i].Quantity -= qty; //if quantity is 1 set it to 0 if(books[i].Quantity) books[i].Quantity = 0; } else if(option == 3) //if option 3 is selected, overwrite the current quantity with new quantity books[i].Quantity = qty; //exit loop break; } } cout << "You have successfully updated the array." << endl; } } //function that executes when choice 5 is selected (sort inventory) void choice5(book books[], int size) { //loop through the books array
  • 12. for(int i = 1; i < size; i++) { //declare a new struct variable and assign the current book encountered to the new variable book current = books[i]; //declare j and set it to i int j = i; //if j>0 and compare title of previous book with current book while(j > 0 && (books[j - 1].Title).compare(current.Title) > 0) { //if previous book title is greater than current book title, move previous book to current book place books[j] = books[j - 1]; //decremet j j--; } //assign current book to next book books[j] = current; } //if book array size is not 0, display success message else display error message if(size != 0) cout << "You have successfully sorted the array." << endl; else cout << "Array is empty. Read the data first." << endl; } //function that executes when user selects choice 6(print inventory) void choice6(book books[], int size) { //loop through the array for(int i = 0; i < size; i++) { //print book details each in a new line cout << endl; cout << "Book Number: " << (i + 1) << endl; cout << "ISBN: " << books[i].ISBN << endl;
  • 13. cout << "Author: " << books[i].Author << endl; cout << "Title: " << books[i].Title << endl; cout << "Quantity: " << books[i].Quantity << endl; cout << "Price: $" << books[i].price << endl; } //if book array size is not 0, display success message else display error message if(size != 0) cout << "You have successfully printed the array." << endl; else cout << "Array is empty. Read the file first." << endl; } //function that executes when user selects choice 7(write inventory to file) void choice7(book books[], int size) { //create a new file to which inventory has to be written ofstream outFile; outFile.open("finalData.dat"); //loop through the array for(int i = 0; i < size; i++) { //write inventory details to file outFile << "Book Number: " << (i + 1) << endl; outFile << "ISBN: " << books[i].ISBN << endl; outFile << "Author: " << books[i].Author << endl; outFile << "Title: " << books[i].Title << endl; outFile << "Quantity: " << books[i].Quantity << endl; outFile << "Price: $" << books[i].price << endl << endl; } //if book array size is not 0, display success message else display error message if(size != 0) cout << "You have successfully printed the array." << endl; else cout << "Array is empty. Read the file first." << endl;
  • 14. //close file outFile.close(); } // File: Boookstore.cpp #include #include"yuan.h" using namespace std; int main() { //declare max size of structure const int MAX_SIZE=100; //set initial size to 0 int size = 0; //variable to read choice int choice; //array of structure typr book books[MAX_SIZE]; //loop that repeats it executes until the user exits do { //display menu cout << "1: Read inventory forn file" << endl; cout << "2: Add an entry" << endl; cout << "3: Delete an entry" << endl; cout << "4: Update an entry" << endl; cout << "5: Sort inventory" << endl; cout << "6: Display Inventory" << endl; cout << "7: Write inventory to file and exit" << endl; //read choice from user cout << "Enter your choice: "; cin >> choice; //call the appropriate function based on the choice of the user switch(choice) {
  • 15. //if choice 1 is selected, call function choice1, pass books array,size and max size as input parameters case 1: choice1(books, size, MAX_SIZE); break; //if choice 2 is selected, call function choice2, pass books array,size and max size as input parameters case 2: choice2(books, size, MAX_SIZE); break; //if choice 3 is selected, call function choice3, pass books array and size as input parameters case 3: choice3(books, size); break; //if choice 4 is selected, call function choice4, pass books array and size as input parameters case 4: choice4(books, size); break; //if choice 5 is selected, call function choice5, pass books array and size as input parameters case 5: choice5(books, size); break; //if choice 6 is selected, call function choice6, pass books array and size as input parameters case 6: choice6(books, size); break; //if choice 7 is selected, call function choice7, pass books array and size as input parameters case 7: choice7(books, size); cout << "Thank you." << endl; break;
  • 16. //if invalid choice is entered display error message default: cout << "Invalid choice!" << endl; } cout << endl; }while(choice != 7); //repeat execution until user wnts to exit return 0; }