【发布时间】: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文件。
-
为什么你不能这样做?你的文本编辑器坏了吗? ... 说真的,你必须问一个具体的问题,否则帮助你的唯一方法就是为你编写完整的代码
-
我不知道怎么做。这就是简单的答案。在此之前我从未做过文件处理。