线程1:SIGABRT错误C ++

时间:2015-06-11 23:28:08

标签: c++ function class linked-list

当我为程序使用xcode时,我遇到了一个复杂的错误。这个程序正在用c ++中的链表进行。该程序提示用户输入要在链接列表中搜索的城市,如果找到,则显示用户输入的城市,州和年份。 (城市,州和年份是从文本文件中读取的,但未在下面的代码中显示)

每当我进入"退出"在它提示我进入城市的程序中,它给出了线程1错误,我不知道如何解决它。

//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);
    }


}

这是一个测试运行......

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) 

0 个答案:

没有答案