fully comments for my program thank you will thumb up includ
fully comments for my program, thank you will thumb up
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
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\"<<endl;
string str;
while(inFile && size < MAX_SIZE)
{
getline(inFile, str);
books[size].ISBN = atoi(str.c_str());
getline(inFile, books[size].Title);
getline(inFile, books[size].Author);
getline(inFile, books[size].publisher);
getline(inFile, str);
books[size].Quantity = atoi(str.c_str());
getline(inFile, str);
books[size].price = atoi(str.c_str());
getline(inFile, str);
size++;
}
cout << \"You have successfully read the file.\" << endl;
inFile.close();
}
void choice2(book books[], int& size, int MAX_SIZE)
{
if(size < MAX_SIZE)
{
string str;
cout << \"\ Enter the book ISBN: \";
cin >> 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<iostream>
#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 <iostream>
#include <fstream>
#include <string>
#include <iomanip>
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\"<<endl;
string str;
//read the book information from the file opened
while(inFile && size < MAX_SIZE)
{
//each time read a line from file into str
getline(inFile, str);
//read ISBN into the array, atoi is used to convert string to integer
books[size].ISBN = atoi(str.c_str());
//read title from file and store in array
getline(inFile, books[size].Title);
//read Author from file and store in array
getline(inFile, books[size].Author);
//read publisher from file and store in array
getline(inFile, books[size].publisher);
//read Quantity from file and store in array, atoi is used to convert string to integer
getline(inFile, str);
books[size].Quantity = atoi(str.c_str());
//read price from file and store in array, atoi is used to convert string to double
getline(inFile, str);
books[size].price = atoi(str.c_str());
getline(inFile, str);
//increment size, size hold the number of records entered into the structure
size++;
}
//print success message
cout << \"You have successfully read the file.\" << endl;
//close file
inFile.close();
}
//function that executes when user selects choice-2(add an entry)
//it accepts an array of structure type book and reference of size and MAX_SIZE as input parameters
void choice2(book books[], int& size, int MAX_SIZE)
{
//check if the structure reached its max size, if not reached add an item
if(size < MAX_SIZE)
{
string str;
//read ISBN from the user and store it in books array
cout << \"\ Enter the book ISBN: \";
cin >> 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<iostream>
#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;
}