I have a complicated error when I use xcode for my program. This program is going over a linked list in c++. The program prompts the user to enter a city to search for in the linked list and if found, displays the city, state, and year of the city the user entered. (The city, state, and year were read off a text file but is not shown in the code below)
Whenever I enter "QUIT" in the program where it prompts me to enter a city, it gives me the Thread 1 error and I do not know how to fix it.
//HEADER FILE
class Data
{
private:
string state;
int year;
string city;
public:
//Constructor
Data()
{ state = ""; year = 0; city = ""; }
void setState(string s);
void setYear(int y);
void setCity(string c);
string getState();
int getYear();
string getCity();
};
class City
{
private:
// Declare a structure for the list
struct ListNode
{
//Class declaration of data
Data data;
ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
//Constructor
City()
{ head = NULL; }
//Destructor
~City();
// Linked list operations
void insertNode(Data);
void deleteNode(string);
void displayList() const;
void searchList(string);
};
//OUT OF CLASS CPP FILE
void Data::setState(string s)
{
state = s;
}
void Data::setYear(int y)
{
year = y;
}
void Data::setCity(string c)
{
city = c;
}
string Data::getState()
{
return state;
}
int Data::getYear()
{
return year;
}
string Data::getCity()
{
return city;
}
/**************************************************
This function searches for the answer that
the user inputted from searchCity(). Traverses
through the nodes to find userAnswer.
***************************************************/
void City::searchList(string userAnswer)
{
ListNode *pNode;
pNode = head;
bool search = true;
while(search)
{
if(pNode -> data.getCity() == userAnswer)
{
cout << pNode ->data.getState() <<" " <<pNode->data.getYear()
<<" " <<pNode->data.getCity() <<endl;
search = false;
}
else if( pNode->next == NULL )
{
if(userAnswer != "QUIT")
{
cout << userAnswer <<" was not found." <<endl;
}
search = false;
}
else if(userAnswer == "QUIT")
{
return;
}
pNode = pNode -> next;
}
}
//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************
void City::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
cout << left <<setw(15) << "STATE" <<left << setw(15)
<<"YEAR" <<left <<setw(15) <<"CITY" <<endl;
cout << left <<setw(15) << "-----" <<left <<setw(15)
<<"-----" <<left <<setw(15) <<"-----" <<endl;
while (nodePtr)
{
// Display the value in this node.
//cout << nodePtr->value << endl;
cout <<left <<setw(15)<< nodePtr->data.getState() ;
cout <<left <<setw(15)<< nodePtr->data.getYear() ;
cout <<left <<setw(15)<< nodePtr->data.getCity() <<endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
//**************************************************
// The insertNode function inserts a node with *
// Data copied. *
//**************************************************
void City::insertNode(Data dataIn)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = NULL; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
//newNode->value = num;
newNode->data = dataIn;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to NULL.
previousNode = NULL;
// Skip all nodes whose value is less than num.
//while (nodePtr != NULL && nodePtr->value < num)
while (nodePtr != NULL && nodePtr->data.getCity() < dataIn.getCity())
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
City::~City()
{
ListNode *nodePtr; // To traverse the list
ListNode *nextNode; // To point to the next node
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr is not at the end of the list...
while (nodePtr != NULL)
{
// Save a pointer to the next node.
nextNode = nodePtr->next;
// Delete the current node.
delete nodePtr;
// Position nodePtr at the next node.
nodePtr = nextNode;
}
}
//IN MAIN
void readTextFile(City &list, Data Info);
void searchCity(City list);
void deleteCity(City list);
int main()
{
City list;
Data Info;
readTextFile(list, Info);
// Display the values in the list.
list.displayList();
searchCity(list);
deleteCity(list);
return 0;
}
void readTextFile(City &list, Data Info)
{
ifstream inputFile;
string state;
int year;
string city;
inputFile.open("cities.txt");
if(inputFile.fail())
{
cout << "Unable to open text file. Closing program. " <<endl;
exit(100);
}
while(inputFile >> state)
{
Info.setState(state);
inputFile >> year;
Info.setYear(year);
inputFile.ignore(20,' ');
getline(inputFile, city);
Info.setCity(city);
list.insertNode(Info);
}
inputFile.close();
}
void searchCity(City list)
{
string userAnswer;
while(userAnswer != "QUIT")
{
cout <<endl;
cout << "Enter City Name or 'QUIT' to stop: ";
getline(cin, userAnswer);
list.searchList(userAnswer);
}
}
Here is a Test run ...
Output:
STATE YEAR CITY
MD 1797 Baltimore
MA 1822 Boston
IL 1837 Chicago
OH 1834 Columbus
TX 1856 Dallas
MI 1815 Detroit
TX 1837 Huston
IN 1832 Indianapolis
FL 1822 Jacksonville
CA 1850 Los Angeles
TN 1826 Memphis
WI 1846 Milwaukee
NY 1898 New York
PA 1701 Philadelphia
AZ 1881 Phoenix
TX 1837 San Antonio
CA 1850 San Diego
CA 1850 San Francisco
CA 1850 San Jose
DC 1788 Washington
Enter City Name or 'QUIT' to stop: Detroit
MI 1815 Detroit
Enter City Name or 'QUIT' to stop: QUIT
HW#6(8183,0x7fff74b1c300) malloc: *** error for object 0x4: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
(lldb)