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;
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;
}