I am writing a library system for a small project and I have this code so far: The first thing I'm trying to show is the show menu so the user can choose and once the user choses to register a new book, the registerBook(bookInfo) should print all the questions and the data would be stored in the file that I created. However, the code is not doing this and the data that I put in when registerBook is processed is not stored in the text file. Could you someone tell me what I'm missing or why it's not storing the data?
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
struct BookShelf{
string name;
string author;
int ID;
int copiesNumber;
float price;
};
void registerBook(BookShelf bookInfo) {
cout<<"Enter the book's name: "<<endl;
std::getline(cin, bookInfo.name) ;
cout<<"Enter the author of the book: "<<endl;
std::getline(cin, bookInfo.author);
cout<<"Enter the books ID: "<<endl;
cin>>bookInfo.ID;
cout<<"Enter number of the book's copies available: "<<endl;
cin>>bookInfo.copiesNumber;
cout<<"Enter the book's price: "<<endl;
cin>>bookInfo.price;
}
void searchBook(BookShelf bookInfo) {
int choice;
cout<<"Would you like to search for the book by"<<endl;
cout<<"1)Book Name"<<endl;
cout<<"or"<<endl;
cout<<"2)Book ID"<<endl;
cin>>choice;
if(choice==1) {
string searchName;
cout<<"Enter book's name:"<<endl;
std::getline(cin, searchName); //why does the program end here? it
outputs the quetsion and the programe ends.
}
else if (choice==2) {
int bookID;
cout<<"To update the book enter book's ID:"<<endl;
cin>>bookID;
//create for loop to find a match for the entered book's ID, if found
}
}
void updateBook(BookShelf bookInfo) {
int bookID;
cout<<"Enter book's ID to update: "<<endl;
cin>>bookID;
}
void deleteBook(BookShelf bookInfo) {
int deleteID;
cout<<"Enter book's ID:"<<endl;
cin>>deleteID;
}
void borrowBook(BookShelf bookInfo) {
int borrowName;
cout<<"Enter name of the book you want to borrow:"<<endl;
cin>>borrowName;
//use for loop to search if the entered name matches any of the available book names in the file, if it is available output "book is available to borrow", if no matches were found then output"no matches were found"
}
void exitPrograme(BookShelf bookInfo) {
cout<<"You exited the programe. See you next time!"<<endl;
}
void showMenu(BookShelf bookInfo) {
int option;
cout<<"What would you like to do?"<<endl;
cout<<"1)Register new Book"<<endl;
cout<<"2)Search for book"<<endl;
cout<<"3)Update a book"<<endl;
cout<<"4) Delete a book"<<endl;
cout<<"5)Borrow a book"<<endl;
cout<<"6)Exit the programe"<<endl;
cin>>option;
if(option==1) {
registerBook(bookInfo);
}
else if(option==2) {
searchBook(bookInfo);
}
else if(option==3) {
updateBook(bookInfo);
}
else if(option==4) {
deleteBook(bookInfo);
}
else if(option==5) {
borrowBook(bookInfo);
}
else if(option==6) {
exitPrograme(bookInfo);
}
}
int main() {
ofstream library("mylibrary.txt", ios::app);
BookShelf bookInfo;
showMenu(bookInfo);
//library<<bookInfo.name<<" "<<bookInfo.author<<" "<<bookInfo.ID<<" "
<<bookInfo.copiesNumber<<" "<<bookInfo.price<<endl;
ifstream bank("mylibrary.txt", ios::in);//to read the file that I previously created
BookShelf bookInfo;
while(library>>bookInfo.name>>bookInfo.author>>bookInfo.ID<<bookInfo.copiesNumber<<bookInfo.price) {
cout<<left<<setw(50)<<bookInfo.name<<" "<<left<<setw(20)<<bookInfo.author<<" "<<left<<setw(7)<<bookInfo.ID<<" "<<endl;
}