gpt4 book ai didi

c++ - 为什么这会导致 Mac 上的无限循环?

转载 作者:行者123 更新时间:2023-11-28 06:01:35 25 4
gpt4 key购买 nike

我编写了以下函数来读取文件并将“记录”加载到 visual studio 2015 中的 BST 中。它按照我在 Windows 中的预期工作,但是当我在 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

最佳答案

我的猜测是您的程序没有在输入流中预期的位置找到“/”,因此永远不会设置 finishRecord = true;。您可能应该在内部循环和外部循环中检查 eof。

关于c++ - 为什么这会导致 Mac 上的无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com