Linked List Unhandled Exception

I'm making a simple linked list class, but when I display the current values, it will display the values but then the compiler will abruptly break and the unhandled exception error produces..

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>

using namespace std;
class NodeType
{
private:
	int info;                 	// data
	NodeType * next;// pointer to next node in the list
	friend class list;
};

class list
{
private:
	NodeType *head;
public:
	list(); //default constructor
	void add(int data);
	void addFront(int data);
	void display() const;
	~list();

};
// default constructor
list::list()
{
	head = NULL;
}
void list::add(int data)
{
	NodeType*ptr = new NodeType;
	ptr->info = data;
	if (head == NULL) //if empty list
	{
		//need to add to the front 
		head = ptr;
		ptr->next = NULL;
	}
	else
	{
		NodeType*current = head;
		while (current->next != NULL) // as long as next node is not null
		{
			current = current->next; // keep traversing till you reach the end 
		}
		current->next = ptr;
	}
	ptr = NULL;
}
void list::addFront(int data)
{
	NodeType*ptr = new NodeType;
	ptr->info = data;
	ptr->next = NULL;
	if (head == NULL) //list is empty
	{
		head = ptr;
	}
	else
	{
		head = ptr;
		ptr->next = head->next;
	}
}
void list::display() const
{
	NodeType*current = head;
	while (current)
	{
		cout << current->info << " ";
		current = current->next;
	}

}
list::~list()
{
	NodeType * current = head;
	while (current)
	{
		NodeType *temp = current;
		current = current->next;
		delete  temp;
	}
	current = NULL;
	head = 0;
}

int main()
{
	//going to add 2 values
	list one;
	int addinfo;

	cout << "Enter a value you would like to add: " << endl;
	cin >> addinfo;
	one.add(addinfo);

	cout << "Enter another value you would like to add: " << endl;
	cin >> addinfo;
	one.add(addinfo);

	//display the current info
	one.display();


	system("PAUSE");
	return 0;

}
Immediately after line 46, what value does ptr->next hold?
1
2
3
4
5
		while (current->next != NULL) // as long as next node is not null
		{
			current = current->next; // keep traversing till you reach the end 
		}
		current->next = ptr; //Hmm, what's the value of "next"? 


Like cire said, when will this loop exit? "next" has to not satisfy the condition there. What is curent->next going to be after it exits?
Ah I see, so then would I have to do:

current->next = ptr

ptr->next = NULL?
Personally, I would put the ptr->next = NULL; between line 32 and 33, then delete line 37.

But, yes. You could do it that way.
Got it to work! Thank you! One random question though:

i'm implementing a do while loop for a menu, and the user if he/she enters Q, it will quit

But for some reason, when I press Q, my program will say "invalid option" and quit, instead of just quitting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int main()
{
	//going to add 2 values
	list one;
	int addinfo;
	char choice;
	do
	{
		cout << "Welcome to the linked list tutorial!" << endl;
		for (int i = 0; i < 40; i++)
		{
			cout << "*";
		}
		cout << endl;
		cout << "A) To insert a value\nB) To print out the current values\nQ) Quit" << endl;
		cin >> choice;
		choice = toupper(choice);
		if (choice == 'A')
		{
			cout << "Enter a value to add to the list: " << endl;
			cin >> addinfo;
			one.add(addinfo);
		}
		else if (choice == 'B')
		{
			cout << "Printing current values..." << endl;
			one.display();
		}
		else 
			cout << "Invalid!";

	} while(choice != 'Q');
	


	system("PAUSE");
	return 0;

}
You only treat 'A' and 'B' as valid choices. Maybe you should consider treating 'Q' like one.
What do you mean? Isn't that what while(choice!='Q') does? isn't this condition saying, as long as the choice doesn't equal Q, redo the menu, if it does equal Q, exit the loop?
What happens if the user enters 'Q'? It will print "Invalid" and then exit the loop. It's doesn't impede functionality, it just prints "Invalid" in one case when it shouldn't.
Topic archived. No new replies allowed.