Question

fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

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 << "\nEnter 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 << "\nEnter 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 << "\nEnter 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 << "\nEnter 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 << "\nEnter 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 << "\nEnter 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;
}

Add a comment
Know the answer?
Add Answer to:
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

  • using the source code at the bottom of this page, use the following instructions to make...

    using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...

  • #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock...

    #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After...

    Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:  implement member functions  convert a member function into a standalone function  convert a standalone function into a member function  call member functions  implement constructors  use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT