Thread: Program has encountered a problem and needs to close

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Program has encountered a problem and needs to close

    I'm trying to get this linked list project to work, but when I try to run it I get this error:

    Lab6 has encountered a problem and needs to close
    Lab6 being my project.

    Code:
    ------------------node.h------------------
    
    struct Node
    {
    	int value;
    	Node *next;
    };
    
    class linkedList
    {
    public:
    	linkedList() {first=0; length=0;}
    	bool operator > (int a); //insert an integer at the end of the linked list using the operator new
    	void displayList(); //display linked list
    	bool Insert(int i, int a); // insert an integer by creating a new node in the location designated by i
    
    	~linkedList();
    private:
    	Node *first;
    	int length;
    };
    
    bool linkedList::operator > (int a)
    {
    	Node *newn, *current;
    	newn = new Node;
    	newn->value = a;
    	newn->next = 0;
    	current = first;
    	while (current->next != 0)
    		current = current->next;
    	current->next = newn;
    	length++;
    	
    	return true;
    }
    
    void linkedList::displayList()
    {
    	Node *current;
    	current = first;
    	for (int i=0; i<length; i++)
    	{
    		current = current->next;
    		std::cout << current->value;
    		if ((i+1) < length)
    			std::cout << ", ";
    	}
    }
    
    bool linkedList::Insert(int i, int a)
    {
    	if ((i > (length+1)) || (i < 1))
    		return false;
    	Node *newn, *current;
    	newn = new Node;
    	newn->value = a;
    	current = first;
    	for (int j=1; j<i; j++)
    		current = current->next;
    	newn->next = current->next;
    	current->next = newn;
    	length++;
    	
    	return true;
    }
    
    linkedList::~linkedList()
    {
    	Node *current = first;
    	while (current->next != 0)
    	{
    		Node *next = current->next;
    		delete current;
    		current = next;
    	}
    delete current;
    }
    
    
    
    ------------------usingnode.cpp------------------
    
    #include <iostream>
    #include "node.h"
    
    using namespace std;
    
    int main()
    {
    	linkedList L_int;
    	L_int.Insert(1,34);
    	L_int.Insert(1,23);
    	L_int.Insert(3,12);
    	L_int.Insert(2,50);
    	L_int.Insert(1,54);
    	L_int.Insert(7,11);
    
    	L_int.displayList();
    
    	return 0;
    }
    I don't get any errors while compiling, so I'm unsure what to do.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Its a run-time error. What you can do is run it with a debugger. Or put lines like
    Code:
    std::cout << "OK Here" << std::endl;
    and see at what point it crashes. For example
    Code:
    	L_int.Insert(1,54);
            std::cout << "OK Here" << std::endl;
    	L_int.Insert(7,11);
            std::cout << "OK Here" << std::endl;
    	L_int.displayList();
            std::cout << "OK Here" << std::endl;
    If you get one "OK Here", then you know that it crashes at L_int.Insert(7,11). If you get two, it crashes on L_int.displayList() and so on.
    If you cannot spot the problem yourself post again specifying where it crashes

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    It crashes at this insert function.

    Code:
    bool linkedList::Insert(int i, int a)
    {
    	if ((i > (length+1)) || (i < 1))
    		return false;
    	Node *newn, *current;
    	newn = new Node;
    	newn->value = a;
    	current = first;
    	for (int j=1; j<i; j++)
    		current = current->next;
    	newn->next = current->next;	                //crashes here
    	current->next = newn;
    	length++;
    	return true;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The first insert into the list, current is NULL. The line after the for-loop then tries to dereference a NULL pointer.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well now is the time to read all about the debugger which comes with your IDE.

    Rather than "There's a problem, sorry, gotta close"
    You get "Whoops, there's a problem and it has something to do with this line of code".

    Not only that, you can print any variable you please, and set breakpoints to stop the code at points you suspect the trouble is starting to happen.

    It's an essential and indispensable tool - learn how to use it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed