나는 다른 사람들이 공중에서 만족스럽게 선택 1을 다뤄 냈습니다. 새 책을 파일 끝에 추가했지만 기존 책에서 검색어를 검색 할 방법이 없습니다. 책을 발행하여 레코드에서 제거하고 students.txt 파일에서 제거하는 적절한 코드도 없습니다.검색 기능을 구현하는 방법은 무엇입니까?
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int choice;
cout<<endl<<endl<<" ***************|Central Library Welcomes you|*************** "<<endl<<endl<<endl;
cout<<" What would you like to do today:"<<endl<<endl;
cout<<" 1. Add a new Title."<<endl;
cout<<" 2. Search."<<endl;
cout<<" 3. Issue a book."<<endl;
cout<<" 4. Return a book."<<endl;
cout<<" 5. Student records."<<endl<<endl;
cout<<" Please enter your query here:";
cin>>choice;
cin.ignore();
//for adding a new record. Works Fine. So far. I have added the app mode so that it enters new records at the end rather than deleting the whole file.
if (choice==1){
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<" Please enter the name of the book."<<endl;
string name;
cout<<" ";
getline(cin, name);
cout<<endl<<" Please enter the name of the author."<<endl;
string author;
cout<<" ";
getline(cin, author);
ofstream addbook;
addbook.open("library.txt", ios::app);
addbook<<"NAME: "<<name<<endl<<"AUTHOR: "<<author<<endl<<endl;
addbook.close();
}
if (choice==2){
string search;
ifstream openFile;
openFile.open("library.txt");
cout<<endl<<" Please enter the Keyword you will like to search for."<<endl;
bool isFound = 0;
cout<<" ";
getline(cin, search);
if (openFile.is_open())
{
while (!openFile.eof())
{
//The search should go here. I want to return the whole record if the search was successful.
openFile.close();
}
}
}
if (choice == 3){
ifstream issuebook;
issuebook.open("library.txt");
cout<<endl<<" Please enter you name."<<endl;
string sName;
cout<<" ";
getline(cin, sName);
cout<<" Please eneter the name of the book you will like to issue."<<endl;
string bookFind;
cout<<" ";
getline(cin, bookFind);
//here we have to find the book in the file and remove its records from library file.
issuebook.close();
}
//CHOICE 4. This will take the name of the author and book and add it back to the library.txt file.
if (choice == 4){
string returnedBook;
string authorName;
cout<<endl<<" Please enter the name of the book you would like to return."<<endl;
string name;
cout<<" ";
getline(cin, name);
cout<<endl<<" Please enter the name of the author."<<endl;
string author;
cout<<" ";
getline(cin, author);
ofstream addbook;
addbook.open("library.txt", ios::app);
addbook<<"NAME: "<<name<<endl<<"AUTHOR: "<<author<<endl<<endl;
addbook.close();
}
if (choice == 5){
ifstream studentRecords;
studentRecords.open("student.txt");
cout<<endl<<" Enter the name of the student you will like to search for:"<<endl;
string student;
cout<<" ";
getline(cin, student);
//Display the record of student.
studentRecords.close();
}
}
코드를 작성하지 않으려면 어떻게해야합니까? 문제가 무엇인지 알려 주어야합니다. – user463035818
해당 항목이 파일에있는 경우 사용자 출력에서 검색 쿼리를 사용하는 코드를 작성할 수 없습니다. 그러면 책의 이름과 파일. –
이 선택 2입니다. 선택 3의 경우 사용자가 입력 한 책을 가져 와서 파일에서 해당 레코드를 삭제하고 책을 빌린 책 os student.txt 파일을 삭제하는 방법을 정확하게 알지 못합니다. –