2014-11-22 72 views
-1

我目前正在研究圖書館計劃,特別是從文本文件中刪除一本書。這裏是有問題的功能:輸入不註冊

void deleteBook(){ 

ifstream fin; 
ofstream fout; 

int fileLength = 0; 
bool bookExist, looper = false, looper2 = false; 
string isbn, author, title, checkOutDate, dueDate, answer, fileLine, isbnCurrent; 

while (looper == false){  //Loop to ensure user inputs a valid book 

    cout << endl << "Enter the Isbn of the book you would like to delete: ";  //isbn determines book 
    cin >> isbn; 

    bookExist = isBookExist(isbn); 

    if (bookExist == true){ 
    looper = true; 
    } 
    else{ 
    cout << endl << "The book you have entered does not exist in the system, please try again" << endl; 
    } 
} 

getBookInfo(isbn, author, title, checkOutDate, dueDate); //Retrieves data about book and displays 

cout << endl << "The isbn of the book is: " << isbn; 
cout << endl << "The author of the book is: " << author; 
cout << endl << "The title of the book is: " << title; 
cout << endl << "The check out date of the book is: " << checkOutDate; 
cout << endl << "The due date of the book is: " << dueDate; 

while (true){ 

cout << endl << "Are you sure you would like to delete this book? (Enter 'yes' or 'no'): "; 
    cin >> answer; 

    if (cin.fail()){  //This most likely wont come up under any circumstances, but is here for completion 
    cout << "Your answer was not valid, please try again." << endl; 
    } 

    if ((answer.compare("yes") == 0) || (answer.compare("Yes") == 0)){ 

    fin.open(booksFile);  //open books file and a temporary 
    fout.open("tempBook.txt"); 

    while (getline(fin, fileLine)) //Check how long file is 
     fileLength++; 

    fin.close();   //reset book file 
    fin.open(booksFile); 

    while (looper2 == false){ 

     for (int i = 0; i < fileLength; i += 5){ 

      getline(fin, isbnCurrent);    //Go through file and look for book in question 

      if (isbn == isbnCurrent){ 

       fout << " " << endl << " " << endl << " " << endl << " " << endl << " "; 
       looper = true; //If book is found, set all fields to empty 
       break; 

      } 

      getline(fin, author);  //If not, continue through file 
      getline(fin, title); 
      getline(fin, checkOutDate); 
      getline(fin, dueDate); 

      fout << isbn << endl << title << endl << author << endl << checkOutDate << endl << dueDate << endl; 
      //Outputs data back onto file 
     } 


    fin.close(); 
    fout.close(); 

    remove("books.txt"); //sets book file to the temp 
    rename("tempBook.txt", "books.txt"); 
    } 
    } else if ((answer.compare("no") == 0) || (answer.compare("No") == 0)){ 
    break;  //Leave function if user decides not to delete 
    } 
    else 
    cout << "Input invalid, please enter either 'yes' or 'no' " << endl; 
    } 
} 

該程序似乎工作,直到是/否部分。在哪一點上沒有按照預期進行工作,但是進入是什麼都不做,該方案只是坐着。我不能完全肯定這個問題從,雖然booksFile的格式如下莖:

ISBN 標題 作者 檢查日期 提前退房日期

謝謝!

回答

1

在你的內心,而循環您有:

while (looper2 == false) { ... } 

但是,循環您不設置lopper2true內(要設置loppertrue)。

BTW:看着你的方式做你的whiles,你有沒有想過使用break代替looper變量?例如,第一種情況會更清潔,如下所示:

while (true) 
{ 
    cout << endl << "Enter the Isbn of the book you would like to delete: ";determines book 
    cin >> isbn; 

    if (isBookExist(isbn)) 
     break; 

    cout << endl << "The book you have entered does not exist in the system, please try again" << endl; 
} 
+0

謝謝DW!這是一個漫長的一天,小事情正在追趕我不會在一段時間內嵌入的for循環中使用中斷,導致終止和時間繼續? – Dozar 2014-11-22 23:57:28