I want to delete a specific book
Mar 19, 2017 at 12:07pm UTC
Hello guys, I'm new to C++ and right now I'm doing homework, making programs that i have think of and etc to train myself. Now I made "Library Management", but I want my program do delete specific books that are in the txt file not all books like what I have made. I had been searching something for days, trying to make something but it never works. So can anyone help me with this ? Thank you very much!
Here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
string choice; //CHOICE FOR GOING BACK OR TO EXIT
int MenuChoice; //CHOICE FOR THE MENU
fstream outfile;
//MAIN MENU
void menu(){
cout << "\t\t\tLibrary Management" << endl;
cout << "\t\t\tMAIN MENU" << endl;
cout << "\t\t\t1. Add books" <<endl;
cout << "\t\t\t2. Delete all record" <<endl;
cout << "\t\t\t3. View book list" << endl;
cout << "\t\t\t4. Exit" << endl;
}
//ADD BOOKS MENU
void AddBooks(){
int DateOfPublishing, ID;
string BookCategory, BookTitle, BookAuthor;
cout << "Book ID: " ;
cin >> ID;
cout << "Book Category: " ;
cin >> BookCategory;
cout << "Book Title: " ;
cin >> BookTitle;
cout << "Book Author: " ;
cin >> BookAuthor;
cout << "Date of Publishing: " ;
cin >> DateOfPublishing;
ofstream outfile("Library.txt" , ios::out | ios::app);
cout << "Would you like to add a new book (Y/N)" ;
cin >> choice;
if (choice == "Y" || choice=="y" ){
AddBooks();
}
outfile << "Books ID: " <<ID << " " << "Book Category: " << BookCategory <<" " << "Book Title: " << BookAuthor
<<" " << "Date of Publishing: " << DateOfPublishing << endl;
outfile.close();
}
//DELETE ALL BOOKS MENU
void DeleteBooks()
{
outfile.open("Library.txt" );
ofstream outfile("Library.txt" ,ios::trunc);
outfile.close();
cout << "All books have been deleted!" <<endl;
cout << "Woud you like to go back (Y/N)?" ;
}
//LIST OF THE BOOKS YOU HAD ADDED
void BookList(){
string line;
fstream outfile ("Library.txt" , ios::in | ios::out);
if (outfile.is_open())
{
while (! outfile.eof() ){
getline(outfile,line);
cout << line << endl;
}
outfile.close();
}
else {
cout << "Unable to open file." ;
}
}
int main(){
menu();
cout <<"\t\t\tPlease write your choice: " ; //CHOICE FOR THE MENU
cin >> MenuChoice;
if (MenuChoice == 1){
system ("CLS" );
AddBooks();
}
if (MenuChoice==2)
{
system("CLS" );
DeleteBooks();
cin >> choice;
for (int i=1; i++;){
if (choice == "Y" || choice == "y" ){
system("CLS" );
return main();
}
if (choice == "N" || choice == "n" ){
return 0;
}
if (choice != "Y" || choice != "y" || choice != "N" || choice != "n" ){
cout << "Sorry you need to choose (Y/N) " ;
cin.ignore();
cin >> choice;
}
}
}
if (MenuChoice == 3){
system ("CLS" );
BookList();
cout << "Would you like to go back to the menu (Y) or to exit (N) ? " ;
cin >> choice;
for (int i=1; i++;){
if (choice == "Y" || choice == "y" ){
system("CLS" );
return main();
}
if (choice == "N" || choice == "n" ){
return 0;
}
if (choice != "Y" || choice != "y" || choice != "N" || choice != "n" ){
cout << "Sorry you need to choose (Y/N) " ;
cin.ignore();
cin >> choice;
}
}
}
if (MenuChoice == 4){
cout << "Are you sure to exit (Y/N)" ;
cin >> choice;
for (int i=1;i++;){
if (choice=="Y" ||choice=="y" ){
return 0;
}
if (choice=="N" ||choice=="n" ){
system("CLS" );
main();
}
if (choice != "Y" || choice != "y" || choice != "N" || choice != "n" ){
cout << "Sorry you need to choose (Y/N) " ;
cin.ignore();
cin >> choice;
}
}
}
if (MenuChoice!=1||MenuChoice!=2||MenuChoice!=3||MenuChoice!=4||MenuChoice!=5){
system("CLS" );
main();
}
}
Mar 19, 2017 at 12:41pm UTC
Copy library.txt to a temporary file, omitting the book that you want to delete. Then rename the temporary file to library.txt. You may need to delete library.txt first.
Topic archived. No new replies allowed.