【问题标题】:How to implement search in this?如何在此实现搜索?
【发布时间】:2016-10-05 07:47:53
【问题描述】:

我已经满意地处理了选项 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();
}
}

【问题讨论】:

  • 是什么让您无法编写代码?你必须告诉我们有什么问题
  • 如果该项目在文件中,我无法编写从用户输出中获取搜索查询的代码,如果是,则获取书名加上它在文件中出现的次数。
  • 那是选择 2。对于选择 3,我不完全知道如何获取用户输入的书籍并将其记录从文件中删除,并将其添加到借阅部分 os student.txt文件。
  • 为什么你不能这样做?你的文本编辑器坏了吗? ... 说真的,你必须问一个具体的问题,否则帮助你的唯一方法就是为你编写完整的代码
  • 我不知道怎么做。这就是简单的答案。在此之前我从未做过文件处理。

标签: c++ search


【解决方案1】:

您应该做的第一件事是为书籍定义数据结构。比如:

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::cinifstream)来读/写一本书,例如

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

其实你的问题太笼统了,我只能给你一些提示。您将不得不从您的讲座或一些书籍中学习所有这些。但是,如果我是一名教师,以上是我要教的关于 C++ 的第一件事。

【讨论】:

  • @LightnessRacesinOrbit ups,已修复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 2011-04-08
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-27
相关资源
最近更新 更多