2015-10-17 106 views
0

我寫了下面的函數來讀取文件,並將「記錄」加載到Visual Studio 2015中的BST中。它的工作方式與我在Windows中預期的一樣,但是當我在mac上運行該函數時,會導致無限大循環。我無法弄清楚爲什麼。有人可以闡明它。有完整的代碼更新了,問題是用下面的函數:爲什麼這會導致Mac無限循環?

BinarySearchTree readFile() 
{ 
    ifstream myfile; 
    myfile.open("character.txt"); 
    string name; 
    string aString = "Empty"; 
    BinarySearchTree loadTree; 
    List list1; 
    int index = 1; 
    bool finishRecord = false; 
    if (!myfile.peek() == myfile.eof()) 
    { 
     while (!myfile.eof()) 
     { 
      getline(myfile, name); 
      while (!finishRecord) 
      { 
       getline(myfile, aString); 
       if (aString == "/") 
       { 
        finishRecord = true; 
       } 
       else 
       { 
        list1.insert(index, aString); 
        index += 1; 
       } 
      } 

      KeyedItem an_item(name, list1, true); 
      loadTree.searchTreeInsert(an_item); 

      //reset variables used for each record 
      index = 1; 
      list1.removeAll(); 
      finishRecord = false; 
     } 
    } 
    myfile.close(); 
    return loadTree; 
} // end readFile 

全碼:

#include <iostream>  //needed for input and output and the console screen 
#include <fstream>  //needed to read/write a file 
#include <iomanip> 
#include <algorithm> //needed to use the transform function 

#include "ListP.h" //linked list class 
#include "BST.h" // binary tree operations 

using namespace std; 

string suspectsArray[256]; //array of the suspects 
int suspectIndex = 0; 
KeyedItem ADD(); 
BinarySearchTree readFile(); 
void QUIT(BinarySearchTree& passedTree, string addedItems[], int arrayIndex); 
bool withinTree(BinarySearchTree& passedTree, string name); 
string INQUIRY(BinarySearchTree& passedTree); 
void suspects(TreeItemType& anItem); 
void findTip(BinarySearchTree& passedTree, string tip, int &noSuspects); 


int main() 
{ 
    string aString; 
    BinarySearchTree characterTree = readFile(); 
    KeyedItem an_Item; 
    string addedItems[256] = {}; 
    int index = 0; 
    string answer; 
    string inquiryReturn = ""; 
    bool main = false; 
    bool inTree = false; 

    while (!main) 
    { 
     cout << "WE ARE AT MAIN LOOP. YOU CAN ADD, INQUIRY, OR QUIT"; 
     cout << endl; 
     if (inquiryReturn == "") 
     { 
      cin >> answer; 
      cin.ignore(); //need to flush the input for the getline() 
     } 
     else 
     { 
      inquiryReturn = ""; 
     } 


     //since we need to accept the input regardless of it case 
     //so going to make the input into lower case 
     transform(answer.begin(), answer.end(), answer.begin(), tolower); 
     //determine which operation the user called 
     if (answer == "add") //user wishes to ADD a character 
     { 
      cout << endl; 
      an_Item = ADD(); 
      //need to determine if the record for the character 
      //is already in the tree or not 
      inTree = withinTree(characterTree, an_Item.getKey()); 
      if (!inTree) 
      { 
       addedItems[index] = an_Item.getKey(); 
       index += 1; 
       characterTree.searchTreeInsert(an_Item); 
      } 
      else 
      { 
       cout << "Character already in tree." << endl; 
      } 
      inTree = false; 
     } 
     else if (answer == "inquiry") //user wishes to do an INQUIRY 
     { 
      cout << "User wants to do an INQUIRY" << endl; 

      characterTree.postorderTraverse(suspects); 
      inquiryReturn = INQUIRY(characterTree); 
      answer = inquiryReturn; 
      suspectIndex = 0; 
     } 
     else if (answer == "quit")  //user wishes to QUIT the program 
     { 
      if (index > 0)  //if the user added a character, save 
      { 
       cout << "OK, saving database to file " 
        "\"character.txt\" "; 
       QUIT(characterTree, addedItems, index); 
       cout << "... Done. Goodbye." << endl; 
       main = true; 
      } 
      else 
      { 
       cout << "OK, saving database to file " 
        "\"character.txt\" "; 
       cout << "... Done. Goodbye." << endl; 
       main = true; 
      } 
     } 
     else 
     { 
      cout << "You didn't enter a valid command." << endl; 
     } 

    } 

    return 0; 
} //end main 

void suspects(TreeItemType& anItem) 
{ 
    suspectsArray[suspectIndex] = anItem.getKey(); 
    suspectIndex++; 
} 

//Function to get a record to add to the binary search tree 
//gets the record in the type KeyedItem 
KeyedItem ADD() 
{ 
    string name; 
    string a_string; 
    List list1; 
    int index = 1; 
    bool end = false; 

    cout << "OK, we are now adding a character to the database." 
     << endl; 
    cout << "Name of Character:" << setw(5) << " "; 
    getline(cin, name); 
    cout << "Attributes:" << setw(12) << " "; 

    //loop to get attributes if any 
    while (!end) 
    { 
     getline(cin, a_string); 
     if (a_string == "") 
     { 
      //if no attributes were added, need to prompt for one 
      if (list1.getLength() == 0) 
      { 
       cout << "Need to enter at least one attribute." << 
        endl << setw(23) << " "; 
      } 
      else 
      { 
       cout << endl; 
       end = true; 
      } 
     } 
     else 
     { 
      list1.insert(index, a_string); 
      index += 1; 
      cout << setw(23) << " "; 
     } 
    } 
    KeyedItem an_item1(name, list1, true); 
    return an_item1; 
} // end ADD 

//Function to read the database file and 
//load all of the records into a Binary Search Tree. 
BinarySearchTree readFile() 
{ 
    ifstream myfile; 
    myfile.open("character.txt"); 
    string name; 
    string aString = "Empty"; 
    BinarySearchTree loadTree; 
    List list1; 
    int index = 1; 
    bool finishRecord = false; 
    if (!myfile.peek() == myfile.eof()) 
    { 
     while (!myfile.eof()) 
     { 
      getline(myfile, name); 
      while (!finishRecord) 
      { 
       getline(myfile, aString); 
       if (aString == "/") 
       { 
        finishRecord = true; 
       } 
       else 
       { 
        list1.insert(index, aString); 
        index += 1; 
       } 
      } 

      KeyedItem an_item(name, list1, true); 
      loadTree.searchTreeInsert(an_item); 

      //reset variables used for each record 
      index = 1; 
      list1.removeAll(); 
      finishRecord = false; 
     } 
    } 
    myfile.close(); 
    return loadTree; 
} // end readFile 

//Function to run if additional shady characters were manually added to the Binary Search Tree 
//It should the added characters to the database text file. 
void QUIT(BinarySearchTree& passedTree, string addedItems[], int arrayIndex) 
{ 
    ofstream outfile; //variable that will be assigned to the file 
    KeyedItem an_item; //record that needs to be added to file 
    string aString; 
    outfile.open("character.txt", ios::app); 
    if (outfile.is_open()) 
    { 
     for (int i = 0; i < arrayIndex; i++) 
     { 
      passedTree.searchTreeRetrieve(addedItems[i], an_item); 
      outfile << an_item.getKey() << "\n"; 
      int jkl = an_item.attributes.getLength(); 
      for (int bnm = 1; bnm < jkl + 1; bnm++) 
      { 
       an_item.attributes.retrieve(bnm, aString); 
       outfile << aString << "\n"; 
      } 
      outfile << "/\n"; 
     } 
     outfile.close(); 
    } 
} // end QUIT 


//function to check whether or not the character is already in the tree 
bool withinTree(BinarySearchTree& passedTree, string name) 
{ 
    KeyedItem item; 
    try 
    { 
     passedTree.searchTreeRetrieve(name, item); 
    } 
    catch (TreeException& error) 
    { 
     //the character is not within the tree 
     return false; 
    } 
    //the character is within the tree 
    return true; 
} // end withinTree 

//Function to if the tip matches any of the attributes of the suspects 
void findTip(BinarySearchTree& passedTree, string tip,int &noSuspects) 
{ 
    KeyedItem aItem; 
    bool tipFound = false; 
    string aString; 
    //look at each of the characters attributes 
    //to see if they are a match for the tip 
    for (int asd = 0; asd < suspectIndex; asd++) 
    { 
     tipFound = false; 
     if (suspectsArray[asd] != "") 
     { 
      passedTree.searchTreeRetrieve(suspectsArray[asd], aItem); 

      //goes through each of the attributes of a character 
      for (int iop = 1; iop < aItem.attributes.getLength() + 1; iop++) 
      { 
       aItem.attributes.retrieve(iop, aString); 
       transform(aString.begin(), aString.end(), aString.begin(), toupper); 
       if (aString == tip) 
       { 
        tipFound = true; 
       } 
      } 
      //if none of character's attribute match 
      //remove them from the suspectArray 
      if (tipFound == false) 
      { 
       suspectsArray[asd] = ""; 
       noSuspects -= 1; 
      } 
     } 
    } 
} // end findTIP 

//Function to find the perpetrator, if any 
string INQUIRY(BinarySearchTree& passedTree) 
{ 
    string codeName;  //string variable for inquiry code name 
    string command;   //string variable for command input 
    string aString; 
    string checkSuspect; 
    int noSuspects = suspectIndex; 
    bool tipFound = false; 


    cout << "OK, we are now conducting an inquiry." << endl; 
    cout << endl << "Enter a Code Name for this Inquiry:"; 
    cout << setw(5) << " "; 
    getline(cin, codeName); 

    while (command != "ADD" && command != "QUIT" && command != "INQUIRY") 
    { 
     cout << "What would you like to do?" << endl; 
     getline(cin, command); 

     transform(command.begin(), command.end(), command.begin(), toupper); 
     if (command == "TIP") 
     { 
      cout << "Enter Tip Info:" << setw(25) << " "; 
      getline(cin, command); 
      transform(command.begin(), command.end(), command.begin(), toupper); 

      findTip(passedTree, command, noSuspects); 

      //if there's only 1 suspect left, alert the user 
      if (noSuspects == 1) 
      { 
       cout << "ALERT! That leaves only one \n" 
        << "suspect in the " << codeName << " inquiry:" 
        << setw(13) << " "; 
       for (int k = 0; k < suspectIndex; k++) 
       { 
        if (suspectsArray[k] != "") 
        { 
         cout << suspectsArray[k] << endl; 
        } 
       } 
      } 
      else if (noSuspects == 0) //all suspects have been eliminated 
      { 
       cout << "There no suspects that are match." << endl; 
      } 
     } 
     else if (command == "CHECK") 
     { 
      bool found = false; 
      cout << "Enter Name of character:" << setw(16) << " "; 
      getline(cin, checkSuspect); 
      transform(checkSuspect.begin(), checkSuspect.end(), 
       checkSuspect.begin(), toupper); 
      for (int p = 0; p < suspectIndex; p++) 
      { 
       aString = suspectsArray[p]; 
       transform(aString.begin(), aString.end(), aString.begin(), toupper); 
       if (aString == checkSuspect) 
       { 
        cout << aString << " is a suspect." << endl; 
        found = true; 
       } 
      } 
      if (!found) 
      { 
       cout << checkSuspect << " is not a suspect." << endl; 
      } 
     } 
     else if (command == "PRINT") 
     { 
      cout << "Current Suspects are:" << endl; 
      for (int k = 0; k < suspectIndex; k++) 
      { 
       if (suspectsArray[k] != "") 
       { 
        cout << suspectsArray[k] << endl; 
       }    
      } 
     } 
     else if (command == "ADD" || command == "QUIT" || command == "INQUIRY") 
     { 
      return command; 
     } 
    } 
} // end INQUIRY 
+0

你能發佈一個*完整的*例子嗎?例如,這裏缺少重要的東西,比如你的'#include'和''''''。 –

+0

添加完整的代碼。 –

回答

0

使用Visual Studio編譯而不是Mac編譯的原因是,你越來越不走運了。你有一個錯誤,它使看起來就像它在Windows上工作,但Mac更幸運並且失敗。

readFile函數開始,因爲如果程序無法正確讀取文件,沒有必要測試程序的其餘部分。垃圾在==垃圾出來。

if (!myfile.peek() == myfile.eof()) 

分解爲int兩個塊,然後比較其結果。

!myfile.peek() 

返回所有錯誤的讀字符或EOF。然後不是。除非從文件讀取NULL,否則這將導致false。

myfile.eof() 

如果peek碰到文件的末尾,將始終返回true,否則爲false。

所以空文件的情況下比較真假應該是不是輸入if的正文。不知道你如何在這裏獲得無限循環。

至於

while (!myfile.eof()) 

Why is iostream::eof inside a loop condition considered wrong?

getline(myfile, name)返回到輸入流和所述輸入流的引用具有常規operator bool()如果流是可讀,則返回true,所以

while (getline(myfile, name)) 

實際上是你所需要的。如果由於任何原因無法讀取文件,包括EOF,繁榮!該計劃在這裏失控。

這意味着所有的輸入都可以被簡化爲:

while (getline(myfile, name)) 
{ // loop until read fails 
    string aString; 
    while (getline(myfile, aString) && aString != "/") 
    { // loop until read fails or find end of record 
     list1.insert(index, aString); 
     index += 1; 
    } 
    KeyedItem an_item(name, list1, true); 
    loadTree.searchTreeInsert(an_item); 

    //reset variables used for each record 
    index = 1; 
    list1.removeAll(); 
} 

雖然之前用於分別捕集aString != "/"所以程序能夠處理的文件結束(或者以其他方式變得不可讀),以用一種強韌的情況下當前記錄結束。上面的代碼只是包裝起來,每天都會調用它,好像沒有任何問題。

while (getline(myfile, name)) 
{ // loop until read fails 
    string aString; 
    while (getline(myfile, aString) && aString != "/") 
    { // loop until read fails or find end of record 
     list1.insert(index, aString); 
     index += 1; 
    } 
    if (!myfile) 
    { 
     // handle error 
    } 

    KeyedItem an_item(name, list1, true); 
    loadTree.searchTreeInsert(an_item); 

    //reset variables used for each record 
    index = 1; 
    list1.removeAll(); 
} 

填補了這個空白。

此外,利用Visual Studio的調試器來逐步查看代碼,並查看出現問題的位置。它會爲你節省噸時間。

我們沒有節點或樹的代碼,所以我們無法提供更多幫助。但是,一旦你知道你有數據並知道它是正確的(再次使用調試器來窺視你的節點和樹來確保一切正確),你就可以完成或有條件地提出一個新的,多更具體的問題。

1

我的猜測是,你的程序沒有找到一個「/」,其中,預計在輸入流,因此從不設置finishRecord = true;。您可能應該檢查內部循環以及外部循環中的eof。

+0

我注意到的另一件事是,如果文件不包含任何內容,它仍然會進入無限循環,即使第一個if語句應該阻止while循環在文件中沒有任何內容時運行。這種行爲只存在於我運行代碼的mac上,而不是在我的Windows上。 –

+0

可能是我錯過了什麼,從Visual Studio編譯器轉到g ++編譯器? –

+0

@HavikIV如果陳述不檢查你認爲它檢查。 istream :: peek(http://www.cplusplus.com/reference/istream/istream/peek/)返回流中的下一個字符。如果流爲空,則返回EOF值。 ios :: eof(http://www.cplusplus.com/reference/ios/ios/eof/)檢查是否設置了eofbit標誌,如果是,則返回true。正確的方式來檢查流是不是空的(以你想要的方式,例如通過偷看)在這裏描述:http://www.cplusplus.com/reference/string/char_traits/eof/ –

相關問題