2017-08-13 7 views
-1

ライブラリシステムのループを作成してこのメ​​ニューを表示しました。C++を使用してtxtファイルの特定のデータを削除/更新する方法

cout << "1. Add a new book.\n2. Update a book.\n3. Delete a book.\n4. Show all 
books.\n5. Search book by ID.\n6. Search book by Name.\n7. Borrow a book.\n8. 
Exit." << endl; 
cin >> choice; 
while (choice >= 0 && choice <8) { 
if (choice == 5) { 
     int x; 
     cout << "Enter the ID you want to search for" << endl; 
     cin >> x; 
     SearchByID(x); 
     choice = 0; 
    } 
cout << "1. Add a new book.\n2. Update a book.\n3. Delete a book.\n4. Show 
all books.\n5. Search book by ID.\n6. Search book by Name.\n7. Borrow a 
book.\n8. exit." << endl; 
cin >> choice; 
} 

SearchByID機能は、そのようにコーディングされています。

void SearchByID(int y) 
{ 
bool found = false; 
Book book; 
string id, name, author, price, copies; 
iLib >> id >> name >> author >> price >> copies; 
while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) { 
    if (book.ID == y) { 
     cout << "Book is found:" << endl; 
     cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl; 
     cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl << endl << endl; 
     found = true; 
     break; 
    } 
} 
if (found == false) 
    cout << "Book is not found" << endl; 
} 

私はそれを実行し、それが正常に動作しますidで書籍を検索、しかし、機能の仕事は、このメニューを再度whileループショーを終了したとき、私は同じ書籍IDのために検索した場合、それは私もそれを示して存在しませんが、プログラムの開始時に、本が存在することがわかりました(それは正しいです) どうすればそれを修正でき、間違いはどこにありますか?

概要:私は2倍以上のための既存のIDを検索したときに、それが正常に動作し、初めてと本が

存在しないことを私に示すために、残りのすべての書籍コードを表示: -

UpdateOrDel機能で
if (choice == 4) { // show all books 
     string id, name, author, price, copies; 
     iLib >> id >> name >> author >> price >> copies; 
     cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl; 
     while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) { 
      cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl; 
      cout << "================================================" << endl; 
     } 
     iLib.clear(); 
     iLib.seekg(0); 
     choice = 0; 
    } 

借用コード: -

void UpdateOrDel(int id, int s) { 
if (s == 3) { // borrow 
    while (!iLib.eof() && i <= 1) { 
     string idd, name, author, price, copies; 
     iLib >> idd >> name >> author >> price >> copies; 
     oTmpLib << left << setw(4) << idd << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl; 
     i++; 
    } 
    while (iLib >> b.ID >> b.name >> b.author >> b.price >> b.copies) { 
     if (b.ID == id) { 
      found = true; 
      oTmpLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies - 1 << endl; 
      cout << "Book has been Borrowed! \n \n" << endl; 
     } 
     else 
      oTmpLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl; 
    } 
    iLib.clear(); 
    iLib.seekg(0); 
    OLib.close(); 
    iLib.close(); 
    remove("library.txt"); 
    ofstream OLib("library.txt", ios::app); 
    ifstream iLib("library.txt", ios::in); 
    i = 1; 
    while (!iTmpLib.eof() && i <= 1) { 
     string idd, name, author, price, copies; 
     iTmpLib >> idd >> name >> author >> price >> copies; 
     OLib << left << setw(4) << idd << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl; 
     i++; 
    } 
    while (iTmpLib >> b.ID >> b.name >> b.author >> b.price >> b.copies) { 
     OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl; 
    } 
    iTmpLib.close(); 
    oTmpLib.close(); 
    remove("LibraryTemp.txt"); 
    if (found == false) 
     cout << "Book is not found \n \n" << endl; 
    } 
} 

答えて

2

(私はあなたがそれが定義だ示さなかったので、自分のファイルだと思う)ILIBはFIL内側の位置へのポインタが含まれていますe。あなたは最初から新しい検索をしたい場合は、まず再び開始に設定する必要があります。

iLib.clear(); 
iLib.seekg(0); 
+0

おかげで多く、それがメニューのほとんどのために働いたが、私は本を借りて、その後にしようとすると、それは何も表示されていないすべての本を表示します。 詳細を更新しました – MiDo

関連する問題