2016-06-05 9 views
-5

나는 다른 사람들이 공중에서 만족스럽게 선택 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(); 
} 
} 
+1

코드를 작성하지 않으려면 어떻게해야합니까? 문제가 무엇인지 알려 주어야합니다. – user463035818

+0

해당 항목이 파일에있는 경우 사용자 출력에서 ​​검색 쿼리를 사용하는 코드를 작성할 수 없습니다. 그러면 책의 이름과 파일. –

+0

이 선택 2입니다. 선택 3의 경우 사용자가 입력 한 책을 가져 와서 파일에서 해당 레코드를 삭제하고 책을 빌린 책 os student.txt 파일을 삭제하는 방법을 정확하게 알지 못합니다. –

답변

0

먼저해야 할 일은 책의 데이터 구조를 정의하는 것입니다. 뭔가 같은 : 당신이 당신이 어떤 입력 스트림 또는 출력 스트림을 사용할 수 있습니다 일단

struct Book { 
    std::string name; 
    std::string author; 
    std::string ISBN; 
} 

이 그럼 당신은 입력 및 출력 사업자

std::ostream& operator<<(std::ostream &o, const Book& b) { 
    o << b.name; 
    o << b.author; 
    o << b.ISBN; 
    return output;    
} 

std::istream& operator>>(std::istream& in, Book& b) { 
    in >> b.name; 
    in >> b.author; 
    in >> b.ISBN; 
    return in;    
} 

를 오버로드 할 수 있습니다 (예를 들어 std::cout를/ std::cin 또는 ifstream)을 사용하여 책을 읽고 쓸 수 있습니다.

Book b; 
std::cin >> b; 
std::cout << b << std::endl; 

사실 귀하의 질문이 너무 광범위하고 몇 가지 힌트 만 제공 할 수 있습니다. 당신은 강의 나 책에서이 모든 것을 배워야 할 것입니다. 그러나 제가 선생님이라면 위의 내용이 제가 C++에 대해 가르쳐야 할 첫 번째 것입니다.

+0

구조체에 첫 번째 코드 블록에 이름을 지정하십시오. –

+0

@LightnessRacesinOrbit ups, fixed – user463035818

관련 문제