2014-11-22 70 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