simple program crashing

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
bump, i really need help on this. anyone have ideas? I tried compiling in release mode and debug mode and they both crashed. I'm out of ideas.
From looking at the code, I think the code is crashing at your first for loop.
1
2
3
4
for (int x = 0; x < LIST_SIZE; x++)
	{
		myList.push_back(0);
	}

I think you're trying to initialize the list of strings. However, you are doing this with 0, which is an int. Instead of using 0, try using two empty quotes ("").

Also, with your if an else statements, you're popping off the back of the list with each iteration of x. When you try to print the list, you may only get one item, instead of 5 which is what you're shooting for.

Lastly, in the the last for loop, all you do is print the first item in the list every time. You already have declared an iterator. I find it easier just to use that one. Here is a quick way to print the items in the list.
1
2
3
4
5
6
7
cout << "Your list of 5 strings is: " << endl;
iterator=myList.begin();	
while (iterator!=myList.end())
	{
		cout << *iterator; //use the dereference operator to print what the iterator points to
		iterator++; 
	}


Try messing with the way to order the list. Besides that, if it's still crashing, try declaring main as an int function that returns 0 (I'm not sure if that's necessary).

Good luck!
Last edited on
I modified the code appropriately. You were right, the initialization was causing the crash. It's running now, I'm working on getting the actual sorting to work now. If you like I'll post the code here once it's completed. Thanks! :3
Alright, I can not get this to sort properly. I have the 1 case covered but I can not get it to sort given any case. Anyone know how to sort a linked list as entries are put in? Here's the modified code right now:

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

	iterator = myList.begin();
	cout << "Please enter 5 strings to be sorted: ";
	for (int x = 0; x < LIST_SIZE; x++)
	{
		cin >> userInput;
		iterator = myList.begin();
		if (x==0)
		{
			myList.push_front(userInput);
		}
		else
		{
			if (userInput <= *iterator)
			{
				myList.push_front(userInput);
				myList.pop_back();
			}
			else
			{
			//How do I sort this list as inputs are entered?
			}
		}		
	}

	cout << endl << "Your list of 5 strings is: " << endl;
	for (list<string>::iterator i = myList.begin(); i != myList.end(); i++)
	{
		cout << *i << endl;
	}


	system("pause");
}
First why are you using #define? Use const. Secondly the first for loop is unnecessary. Simply construct the list with LIST_SIZE items to begin with and the strings will be default initialized to "" via their constructor.
http://cplusplus.com/reference/stl/list/list/
You are making this way too difficult. Just insert the strings into the list using push_back and sort once at the end. List has a built in sort function. In fact the following link even has an example on a case insensitive string comparison but yours may not need to be that complicated.
http://cplusplus.com/reference/stl/list/sort/
Topic archived. No new replies allowed.