// File: yuan.h #include <iostream> #include <fstream> #include <string> #includ
ID: 3925150 • Letter: #
Question
// File: yuan.h
#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;
}
need comments cuase im bad at English ty
Explanation / Answer
// File: yuan.h
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// declare book structure
struct book
{
int ISBN;
string Author;
string Title;
string publisher;
int Quantity;
double price;
};
/*function1: taking in input book structure array, size as pass by reference and the maximum size as input
return type: void
This function open the inventory file, reads the file line by line and fill the book
structure till maximum size of book structure array
*/
void choice1(book books[], int& size, int MAX_SIZE)
{
// open file
ifstream inFile;
inFile.open("inventory.txt");
// check if file is opened
if (inFile.fail())
cout <<"file could not open"<<endl;
string str;
// read from file while maxi mum size is reached or there are no lines to read from file
while(inFile && size < MAX_SIZE)
{
// read the corresponding values of book structure
// and fill the book structure array
getline(inFile, str);
// convert string to integer using atoi and fill in the ISBN
// for corresponding array index
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);
// increment the size to fill the array for next index
size++;
}
cout << "You have successfully read the file." << endl;
// close file
inFile.close();
}
/*function1: taking in input book structure array, size as pass by reference and the maximum size as input
return type: void
This function asks for the user input fill the book
structure till maximum size of book structure array
*/
void choice2(book books[], int& size, int MAX_SIZE)
{
if(size < MAX_SIZE)
{
string str;
// user input
cout << " Enter the book ISBN: ";
// fille array
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;
}
}
/*function1: taking in input book structure array, size as pass by reference
return type: void
This function asks the user for ISBN number of the book, finds the book and delete the record
*/
void choice3(book books[], int& size)
{
if(size == 0)
cout << "Array is empty. Read the data first." << endl;
else
{
int isbn;
// get isbn from user
cout << " Enter the ISBN of the book: ";
cin >> isbn;
// search for isbn in structure array
for(int i = 0; i < size; i++)
{
// if book is found
if(books[i].ISBN == isbn)
{
int j = i;
while(j < size - 1)
{
// delete book
books[j] = books[j + 1];
j++;
}
// decrement size
size--;
break;
}
}
cout << "You have successfully deleted an entry." << endl;
}
}
/*function1: taking in input book structure array, size as pass by reference
return type: void
This function allows the user to increment, decrement or add new new quantity
for a particular book by entering its ISBN
*/
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;
// input isbn
cout << " Enter the ISBN of the book: ";
cin >> isbn;
// ask for user choice
cout << "1. Increment" << endl;
cout << "2. Decrement" << endl;
cout << "3. Add New" << endl;
cout << "Enter your option: ";
cin >> option;
// input quantity
cout << "Enter the quantity: ";
cin >> qty;
// iterate array, find the isbn
for(int i = 0; i < size; i++)
{
if(books[i].ISBN == isbn)
{
// increment, decrement or set new quantity
if(option == 1)
books[i].Quantity += qty;
else if(option == 2)
{
books[i].Quantity -= qty;
if(books[i].Quantity)
// if quantity is less than 0, set the quantity to zero.
books[i].Quantity = 0;
}
else if(option == 3)
books[i].Quantity = qty;
break;
}
}
cout << "You have successfully updated the array." << endl;
}
}
/*function1: taking in input book structure array, and size of array
return type: void
this function sorts the book array by comparing titles of books
*/
void choice5(book books[], int size)
{
// iterate the array
for(int i = 1; i < size; i++)
{
book current = books[i];
int j = i;
// compare the titles of book and sort on basis of that
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;
}
/*function1: taking in input book structure array, and size of array
return type: void
This function prints data in array to the output console
*/
void choice6(book books[], int size)
{
// itearte the book array
for(int i = 0; i < size; i++)
{
// output the data in array
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;
}
/*function1: taking in input book structure array, and size of array
return type: void
This function open the output file, and puts the data in array to the output file
*/
void choice7(book books[], int size)
{
// open file
ofstream outFile;
outFile.open("finalData.dat");
// iterate the book array and put data in file
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;
// close the file
outFile.close();
}
// File: Boookstore.cpp
#include <iostream>
#include "yuan.h" // include header file
using namespace std;
int main()
{
// declare cosntant variable maxsize
const int MAX_SIZE=100;
// declare and initialize variables
int size = 0;
// choice variable for user input
int choice;
// book structure array
book books[MAX_SIZE];
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;
cout << "Enter your choice: ";
// user input choice
cin >> choice;
// call function of basis of 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;
// get user choice until it wasnts to exit
}while(choice != 7);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.