linked list cout crash

Hey, this little program I wrote keep crashing. I posted this already in the beginner forum but noone's come up with anything. Here's a copypasta of the original post.

Howdy,
This little program I've written for my class keeps crashing when trying to output elements of the list. Below is the code, with a comment at the point which is causing it to crash. Any ideas? I haven't been able to figure this one out. Thanks ^_^

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
// String lab
// written by Nathan McGinn

#include <iostream>
#include <list>
#include <string>

#define LIST_SIZE 5

using namespace std;

void main()
{
	list<string> myList;
	list<string>::iterator iterator;
	string userInput;

	for (int x = 0; x < LIST_SIZE; x++)
	{
		myList.push_back(0);
	}

	iterator = myList.begin();
	cout << "Please enter 5 strings to be sorted: ";
	for (int x = 0; x < LIST_SIZE; x++)
	{
		cin >> userInput;
		if (x == 0)
			myList.insert(myList.begin(), userInput);
		else
		{
			if (userInput < myList.front())
			{
				myList.push_front(userInput);
				myList.pop_back();
			}
			else if (userInput > myList.front())
			{
				myList.insert(iterator, userInput);
				myList.pop_back();
			}
			else
			{
				myList.push_front(userInput);
				myList.pop_back();
			}
		}
		iterator++;
		
	}
	cout << "Your list of 5 strings is: " << endl;
	for (list<string>::iterator i = myList.begin(); i != myList.end(); i++)
	{
		cout << myList.front(); // Crashes at this point
	}


	system("pause");
}


Also here's a screenshot of the error it gives when it crashes:
http://i51.tinypic.com/4g12l1.jpg
Don't use void main()!!

It's on the creator of C++'s website, void main() has never been valid in C or C++.

http://www2.research.att.com/~bs/bs_faq2.html#void-main
Take a look at this too.
http://cplusplus.com/reference/algorithm/lower_bound/

I don't understand why you are using pop_back at all. You just need to figure out where to insert each new string. You shouldn't be deleting anything within the list. lower_bound will help you determine that. All you need to do is determine the insertion point and insert each string. Or you can follow the advice in the other thread and use list::sort when finished inserting all. This is really very simple.
maybe i should have been clearer on the objective of this assignment. I need to sort each entry as it is entered, not after they have all been entered.

the intention of using pop_back was to eliminate empty elements of the list, because as i understood the list needs to be initialized somehow so i used empty characters.
Topic archived. No new replies allowed.