如何在文件中搜索图书ID [英] How do I search in a file for book id
本文介绍了如何在文件中搜索图书ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下:
my code below :
/*password is admin*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
using namespace std;
void list_all_books_1();
void list_available_books_2();
void borrow_books_3();
void search_for_abook_4();
void add_new_book_5();
void delete_books_6();
char path[] = "library books.txt";
void output(){
//this function for displaying choices only
cout << "***********************************" << endl;
cout << "1. List all books in library" << endl;
cout << "2. List available books to borrow " << endl;
cout << "3. Borrow a Book from library" << endl;
cout << "4. Search For a Book" << endl;
cout << "5. Add New Books" << endl;
cout << "6. Delete a Book" << endl;
cout << "7. EXIT The Library" << endl;
cout << "***********************************" << endl;
}
//=====================================================================================================================================================
struct books{
//identfying books with all needed things
int id, status;
string title, p_name, p_address;
string date;
};
struct author{
string aut_name;
string aut_nationality;
};
//=====================================================================================================================================================
//function for choice 1 showing the books (under constructions)
void list_all_books_1(){
ifstream show;
char all;
show.open(path, ios::in | ios::app);
while (!show.eof()){
show >> all;
if (all == '%')
cout << "\n\n";
else if (all == '.')
cout << "\n\n\n";
else if (all == ' ')
cout << " ";
else
cout << all;
}
cout << endl;
show.close();
}
//=====================================================================================================================================================
void list_available_books_2(){
//function for choice 2 (list available books to borrow)
}
//=====================================================================================================================================================
void borrow_books_3(){
//function for choice 3( Borrow a Book )
}
//=====================================================================================================================================================
void search_for_abook_4(){
ifstream search;
int idx, i = 0,get;
cout << "enter the ID of the book you're looking for";
cin >> idx;
search.open(path, ios::in | ios::app);
while (!search.eof()){
search >> get;
if (idx == get)
i++;
else
i = 0;
}
if (i > 0)
cout << "found";
else
cout << "not found";
}
//=====================================================================================================================================================
//for choice 5 to fill books (under constructions)
void add_new_book_5(){
fstream d_base;
int aut_number, booksnumber;
books newbook[1000];
author aut[100];
cout << "how many books you want to add ? ";
cin >> booksnumber;
cout << "what books you want to add :" << endl;
d_base.open(path, ios::out | ios::app);
for (int i = 0; i < booksnumber; i++){
cout << "id please : "; cin >> newbook[i].id;
cout << "title : "; cin.ignore(); getline(cin, newbook[i].title);
cout << "publisher name :"; getline(cin, newbook[i].p_name);
cout << "publisher address : "; getline(cin, newbook[i].p_address);
d_base << "[BOOK INFO]" << "%Book Id : " << newbook[i].id << "%title : " << newbook[i].title;
d_base << "%[PUBLISHER INFO]" << "%publisher name : " << newbook[i].p_name << "%puplisher address :" << newbook[i].p_address;
d_base << "%[AUTHOR(s) INFO]";
cout << "how many authors for the books";
cin >> aut_number;
for (int j = 0; j < aut_number; j++){
cout << "author" << j + 1 << " name : "; cin.ignore(); getline(cin, aut[j].aut_name);
cout << "Nationality : "; getline(cin, aut[j].aut_nationality);
d_base << "% Authors Name : " << aut[j].aut_name << "% Nationality : " << aut[j].aut_nationality;
}
cout << "Publish date :"; getline(cin, newbook[i].date);
cout << "How many copies of " << newbook[i].title << " "; cin >> newbook[i].status;
d_base << "%[MORE INFO]";
d_base << "%PublishedAt : " << newbook[i].date << "%status :" << newbook[i].status << "." << endl;
}
d_base.close();
cout << "Books Have Been Saved Sucessfully";
}
//=====================================================================================================================================================
void delete_books_6(){
//function for searching for a book
}
//=====================================================================================================================================================
int main(){
string choice;
cout << "welcome to FCIS library\n\n";
do{
output();
cout << "what do you want to do ? ";
getline(cin, choice);
if (choice == "1"){
list_all_books_1();
}
//this one for list available books
else if (choice == "2"){
//not completed
}
//this one for borrow a book
else if (choice == "3"){
//not completed yet don't choose 3
}
else if (choice == "4"){
search_for_abook_4();
}
//this one is for adding new books to the list
else if (choice == "5"){
string pass;
do{
cout << "you must be an admin to add new books." << endl << "please enter passowrd (use small letters) : ";
cin >> pass;
if (pass == "b")
break;
else if (pass == "admin"){
cout << "ACCESS GAINED WELCOME " << endl;
add_new_book_5();
}
else{
cout << "Wrong password try again or press (b) to try another choice";
continue;
}
} while (pass != "admin");
}
//this one for deleteing a book
else if (choice == "6"){
//not completed yet
}
else if (choice == "7"){
cout << "Thanks for Using FCIS LIBRARY" << endl;
break;
}
else
cout << "\nwrong choice please choose again\n\n";
} while (true);
}
我的尝试:
i试图打开文件并仅搜索id但它没有做任何事情
What I have tried:
i have tried to open the file and search for id only but it doesn't do anything
推荐答案
在行
In the lines
else
i = 0;
您将i重置为0,即使您之前找到了idx也是如此。只需从代码中删除这些行。
you are resetting i to 0, even if you have found the idx before. Just delete those lines from your code.
这篇关于如何在文件中搜索图书ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文