Program inserting junk into list.

I'm guessing my insert function isn't working correctly. It seems to be just inserting junk into my list whenever I print it out. I'm not getting any errors/warnings. Output generates something like: 91927492

Expected output: 12345678910
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

//--------------------- MAIN -------------------------------
#include "ListType.h"
#include <iostream>
#include "UListType.h"

using namespace std;

int main() {
	UListType<int> ulist;

	for (int i = 0; i < 11; ++i) {
		ulist.insert(i);
	}

	cout << ulist << endl;
	system("pause");

	return 0;
}


//--------------------- HEADER FILES---------------------------
template <class T>
class UListType:public ListType <T> {
public:
	bool insert(const T&);

};

template <class T> //insert function
bool UListType<T>::insert(const T& item) {
	bool result = false;
	if (count == 0) {
		head = new node <T>;
		head->info = item;
		head->link = NULL;
		++count;
		result = true;
	}
	return result;
}

Last edited on
I'm guessing my insert function isn't working correctly.

No need to guess. It isn't working correctly. When is the only time work is done in the insert function? Why does it return a value if you never check the value returned?

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
	UListType<int> ulist;

	for (int i = 0; i < 11; ++i) {
		if (ulist.insert(i)) cout << i << " successfully inserted.\n";
		else cout << i << " was not successfully inserted.\n";
	}

	cout << ulist << endl;
	system("pause");

	return 0;
}
Last edited on
Will try this when I get home, thanks so much :)
Thank you for your input cire, after doing your test it seems the first item inserted into the list succeeded (the 0) then every other input afterwards failed. The confusing part is this is the insert function my professor wrote xD and its not working of course
Topic archived. No new replies allowed.