Hi, your C++ program does not compile due to the following problems (on a general Windows machine):
Following is the corrected code for Windows. Note that your text files will be updated AFTER you quit the program. To change that, you can call the Save() function every time you exit a menu, and then flush the data structures.
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class Borrower
{
private:
string ID, name;
public:
Borrower() : ID("0"), name("no name yet") {}
void setID(string nID);
void setName(string nID);
string getID();
string getName();
};
void Borrower::setID(string nID)
{
ID = nID;
}
void Borrower::setName(string nname)
{
name = nname;
}
string Borrower::getID()
{
return ID;
}
string Borrower::getName()
{
return name;
}
class Book
{
private:
string ISBN, Publisher, title;
public:
Book() : ISBN("0"), Publisher("None"), title("None") {}
void setISBN(string nISBN);
void setTitle(string ntitle);
void setPublisher(string npublisher);
string getISBN();
string getTitle();
string getPublisher();
};
void Book::setISBN(string nISBN)
{
ISBN = nISBN;
}
void Book::setPublisher(string npublisher)
{
Publisher = npublisher;
}
void Book::setTitle(string ntitle)
{
title = ntitle;
}
string Book::getISBN()
{
return ISBN;
}
string Book::getTitle()
{
return title;
}
string Book::getPublisher()
{
return Publisher;
}
class Loan
{
private:
string ID, ISBN;
bool available;
public:
Loan() : ID("0"), ISBN("0"), available(true) {}
void setID(string nID);
void setISBN(string nISBN);
void setAvailable(string navailable);
string getID();
string getISBN();
string getAvailable();
void returnBook();
};
void Loan::setID(string nID)
{
ID = nID;
}
void Loan::setISBN(string nISBN)
{
ISBN = nISBN;
}
void Loan::setAvailable(string navailable)
{
if (navailable == "0")
available = false;
else if (navailable == "1")
available = true;
else
cout << "Invalid input!\n";
}
string Loan::getID()
{
return ID;
}
string Loan::getISBN()
{
return ISBN;
}
string Loan::getAvailable()
{
if (available == true)
return "1";
else
return "0";
}
void Loan::returnBook()
{
available = true;
}
class BorrowerList
{
private:
vector<Borrower> listofBorrowers;
fstream rFileHandler;
public:
void readBorrowerList(string fileName);
void addBorrower(Borrower b);
int searchForBorrower(string ID);
void deleteBorrowerat(int x);
void findBorrower(string find);
void listBorrowers();
void printBorrowerList(ofstream &outb);
};
void BorrowerList::readBorrowerList(string fileName)
{
ifstream in;
in.open(fileName);
if (in.fail())
{
cout << "Failed to open file!\n";
system("pause");
exit(1);
}
string ID, name;
Borrower nb;
while (in >> ID)
{
nb.setID(ID);
getline(in, name);
nb.setName(name);
listofBorrowers.push_back(nb);
}
in.close();
}
void BorrowerList::listBorrowers()
{
for (int i = 0; i < listofBorrowers.size(); i++)
cout << listofBorrowers[i].getID() << " " <<
listofBorrowers[i].getName() << endl;
}
void BorrowerList::addBorrower(Borrower b)
{
string ID, name;
cout << "Enter the borrower ID: ";
cin >> ID;
b.setID(ID);
cout << endl;
cout << "Enter the borrower name: ";
cin.ignore(1000, '\n');
getline(cin, name);
b.setName(name);
listofBorrowers.push_back(b);
}
int BorrowerList::searchForBorrower(string x)
{
for (int i = 0; i < listofBorrowers.size(); i++)
{
if (listofBorrowers[i].getID() == x)
return i;
}
return -1;
}
void BorrowerList::deleteBorrowerat(int x)
{
if (x < 0 || x >= listofBorrowers.size())
{
cout << "Index out of range!\n";
return;
}
listofBorrowers.erase(listofBorrowers.begin() + x);
}
void BorrowerList::findBorrower(string find)
{
int j = 0;
for (int i = 0; i < listofBorrowers.size(); i++)
{
if ((listofBorrowers[i].getName() == " " + find) ||
(listofBorrowers[i].getID() == find))
{
cout << listofBorrowers[i].getID() << " " <<
listofBorrowers[i].getName() << endl;
j++;
}
}
if (j == 0)
cout << "Not found\n";
}
void BorrowerList::printBorrowerList(ofstream &outbor)
{
for (int i = 0; i < listofBorrowers.size(); i++)
outbor << listofBorrowers[i].getID() << " " <<
listofBorrowers[i].getName() << endl;
}
class BookList
{
private:
vector<Book> listofBooks;
public:
void readBookList(string fileName);
void addBook(Book b);
int searchBook(string x);
void findBook(string find);
void deleteBook(int x);
void listBooks();
Book getBook(string ISBN);
void printBookList(ofstream &outb);
};
void BookList::printBookList(ofstream &outb)
{
for (int i = 0; i < listofBooks.size(); i++)
outb << listofBooks[i].getISBN() << "\n"
<< listofBooks[i].getTitle() << "\n"
<< listofBooks[i].getPublisher() << endl;
}
Book BookList::getBook(string ISBN)
{
Book none;
for (int i = 0; i < listofBooks.size(); i++)
{
if (listofBooks[i].getISBN() == ISBN)
return listofBooks[i];
}
return none;
}
void BookList::readBookList(string fileName)
{
ifstream in;
in.open(fileName);
if (in.fail())
{
cout << "Failed to open file!\n";
system("pause");
exit(1);
}
string ISBN, title, publisher;
Book tb;
while (getline(in, ISBN))
{
tb.setISBN(ISBN);
getline(in, title);
tb.setTitle(title);
getline(in, publisher);
tb.setPublisher(publisher);
listofBooks.push_back(tb);
}
in.close();
}
void BookList::findBook(string find)
{
int j = 0;
for (int i = 0; i < listofBooks.size(); i++)
{
if ((listofBooks[i].getISBN() == find) ||
(listofBooks[i].getTitle() == find))
{
cout << listofBooks[i].getISBN() << "\n"
<< listofBooks[i].getTitle() << "\n"
<< listofBooks[i].getPublisher() << endl;
j++;
}
}
if (j == 0)
cout << "Not found\n";
}
int BookList::searchBook(string x)
{
for (int i = 0; i < listofBooks.size(); i++)
{
if (listofBooks[i].getISBN() == x)
return i;
}
return -1;
}
void BookList::deleteBook(int x)
{
if ((x < 0) || (x >= listofBooks.size()))
{
cout << "Index out of range!\n";
return;
}
listofBooks.erase(listofBooks.begin() + x);
}
void BookList::addBook(Book b)
{
string ISBN, title, publisher;
cout << "Enter the book ISBN: ";
cin >> ISBN;
b.setISBN(ISBN);
cout << endl;
cout << "Enter the book title: ";
cin.ignore(1000, '\n');
getline(cin, title);
b.setTitle(title);
cout << endl;
cout << "Enter the book publisher: ";
getline(cin, publisher);
b.setPublisher(publisher);
listofBooks.push_back(b);
}
void BookList::listBooks()
{
for (int i = 0; i < listofBooks.size(); i++)
cout << listofBooks[i].getISBN() << "\n"
<< listofBooks[i].getTitle() << "\n"
<< listofBooks[i].getPublisher() << endl;
}
class LoanList
{
private:
vector<Loan> list;
fstream lFileHandler;
public:
void readLoanList(string fileName);
void addLoan(Loan b);
bool findLoan(string ISBN);
void listAllLoans();
void returnBook(Book b);
void printLoanList(ofstream &outb);
bool isAvailable(Loan n);
};
bool LoanList::isAvailable(Loan n)
{
string c = n.getAvailable();
if (c == "1")
return true;
else
return false;
}
void LoanList::printLoanList(ofstream &outl)
{
for (int i = 0; i < list.size(); i++)
outl << list[i].getID() << " " <<
list[i].getISBN() << " " << list[i].getAvailable()
<< endl;
}
bool LoanList::findLoan(string ISBN)
{
for (int i = 0; i < list.size(); i++)
{
if (list[i].getAvailable() == "1")
return true;
}
return false;
}
void LoanList::readLoanList(string fileName)
{
ifstream in;
in.open(fileName);
if (in.fail())
{
cout << "Failed to open file!\n";
exit(1);
}
string ID, ISBN, available;
Loan tl;
while (in >> ID >> ISBN >> available)
{
tl.setID(ID);
tl.setISBN(ISBN);
tl.setAvailable(available);
list.push_back(tl);
}
in.close();
}
void LoanList::listAllLoans()
{
for (int i = 0; i < list.size(); i++)
cout << list[i].getID() << " " <<
list[i].getISBN() << " " << list[i].getAvailable()
<< endl;
}
void LoanList::addLoan(Loan b)
{
for (int i = 0; i < list.size(); i++)
if (b.getAvailable() == "0")
{
cout << "Loan is invalid!\n";
system("pause");
exit(1);
}
list.push_back(b);
}
void LoanList::returnBook(Book b)
{
for (int i = 0; i < list.size(); i++)
if ((b.getISBN() == list[i].getISBN()) &&
(list[i].getAvailable() == "1"))
{
cout << "Book is already returned!\n";
system("pause");
exit(1);
}
for (int i = 0; i < list.size(); i++)
if ((b.getISBN() == list[i].getISBN()) &&
(list[i].getAvailable() == "0"))
{
list[i].setAvailable("1");
}
}
void printMenu()
{
cout << "\nChoose an option: \n";
cout << "1.\t Register a new borrower.\n";
cout << "2.\t Add a new book to the catalog.\n";
cout << "3.\t Delete a borrower by ID.\n";
cout << "4.\t Delete a book by ISBN.\n";
cout << "5.\t List all borrowers.\n";
cout << "6.\t List all books.\n";
cout << "7.\t List all loans.\n";
cout << "8.\t Search for a given borrower (by name or ID) and
display his/her info.\n";
cout << "9.\t Search for a given book (by title or ISBN) and
display its info.\n";
cout << "10.\t Loan a book. \n";
cout << "11.\t Return a book.\n";
cout << "12.\t Quit.\n";
cout << "Your option: \n";
}
void Save(string catalog, string readershiplist, string loans,
BookList &booklist, BorrowerList &Borlist, LoanList
&Llist)
{
ofstream out1, out2, out3;
out1.open(catalog);
if (out1.fail())
{
cout << "Faild to output to the file!\n";
system("pause");
exit(1);
}
booklist.printBookList(out1);
out1.close();
out2.open(readershiplist);
if (out2.fail())
{
cout << "Faild to output to the file!\n";
system("pause");
exit(1);
}
Borlist.printBorrowerList(out2);
out2.close();
out3.open(loans);
if (out3.fail())
{
cout << "Faild to output to the file!\n";
system("pause");
exit(1);
}
Llist.printLoanList(out3);
out3.close();
}
void main()
{
string cFileName = "catalog.txt";
string rFileName = "readershiplist.txt";
string lFileName = "loans.txt";
BorrowerList Borlist;
Borlist.readBorrowerList(rFileName);
BookList Booklist;
Booklist.readBookList(cFileName);
LoanList loanList;
loanList.readLoanList(lFileName);
int choice = 0;
while (choice != 12)
{
printMenu();
cin >> choice;
cout << endl;
if (choice == 1)
{
Borrower b;
Borlist.addBorrower(b);
}
else if (choice == 2)
{
Book b;
Booklist.addBook(b);
}
else if (choice == 3)
{
string ID;
cout << "Enter the ID of the borrower you want to delete:
";
cin >> ID;
int i = Borlist.searchForBorrower(ID);
if (i == -1)
cout << "No such borrower!\n";
else
Borlist.deleteBorrowerat(i);
}
else if (choice == 4)
{
string ISBN;
cout << "Enter the ISBN of the book you want to delete:
";
cin >> ISBN;
int i = Booklist.searchBook(ISBN);
if (i == -1)
cout << "No such book!\n";
else
Booklist.deleteBook(i);
}
else if (choice == 5)
{
Borlist.listBorrowers();
}
else if (choice == 6)
{
Booklist.listBooks();
}
else if (choice == 7)
{
loanList.listAllLoans();
}
else if (choice == 8)
{
string nameOrID;
cout << "Enter the ID or the name of the borrower you're
searching for: ";
cin.ignore(1000, '\n');
getline(cin, nameOrID);
Borlist.findBorrower(nameOrID);
}
else if (choice == 9)
{
string ISBNORTitle;
cout << "Enter the ISBN or the title of the book you're
searching for: ";
cin.ignore(1000, '\n');
getline(cin, ISBNORTitle);
Booklist.findBook(ISBNORTitle);
}
else if (choice == 10)
{
Loan b;
string ISBN;
cout << "Enter the ISBN of the book you want to loan:
\n";
cin >> ISBN;
int i = Booklist.searchBook(ISBN);
if (i == -1)
{
cout << "No such book!\n";
exit(1);
system("pause");
}
else
{
if (loanList.findLoan(ISBN))
{
string ID;
cout << "Enter the ID of the student you want to loan the
book to: \n";
cin >> ID;
int i = Borlist.searchForBorrower(ID);
if (i == -1)
{
cout << "There is no student with the ID you entered!
\n";
}
else
{
b.setID(ID);
b.setISBN(ISBN);
loanList.addLoan(b);
}
}
}
}
else if (choice == 11)
{
string ISBN;
BookList b;
cout << "Enter the ISBN of the book you want to return:
";
cin >> ISBN;
if (loanList.findLoan(ISBN))
{
loanList.returnBook(b.getBook(ISBN));
}
else
cout << "The ISBN you entered is either wrong or the book is
already returned!\n";
}
else if (choice == 12)
Save(cFileName, rFileName, lFileName, Booklist, Borlist,
loanList);
else
cout << "Your option should be one of the above 12
ones!\n";
}
system("pause");
}
Also note that return 0 is implicit in C++ main function, so it is not required here.
I wrote this code but there’s an issue with it : #include <iostream> #include <vector&...
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,...
Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class LibraryCard { private String id; private String cardholderName; ...
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"...
Please provide original Answer, I can not turn in the same as my classmate. thanks In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a “next” pointer. Textbook Type Attribute String title String author String ISBN Textbook* next Textbook Type Attribute String title String...
Here is what I got so far also can anyone help me align the output #include <iostream> #include <iomanip> using namespace std; class Person { private: string name; public: Person() { name = ""; } Person(string name) { this->name = name; } string getName() { return name; } void setName(string name) { this-> name = name; } }; class Publication { private: int pubNumber; string title; Person borrower; public: static int count; Publication() { title = ""; }...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...
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...
Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...
#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...
Below is a class definition (.h) for the Bookclass. class Book { private: int numPages; string author; string isbn; double price; public: string title; public: Book (); void setAuthor(string); string getAuthor(); void setIsbn(string); string getIsbn (); void setPrice(double); double getPrice (); string getTitle (); }; Assume you are writing code within the maindriver program: declare variable of type Book. Using the Bookvariable, set your book’s author to Edgar Allan Poe Using the Bookvariable, set your book’s title to The...