Bubble sorting - stuck please help

Hi I really need help I am stuck on an assignment where I need to sort a list that is in a .txt file by name and then by year using bubble sorting. I figured if I could get one part dont then I should be able to get the second but I can not seem to get this to work. Right now I am trying to get the date/year portion to work. Any help would be great.


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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  // preprocessor directives
#include <iostream> // cin, cout
#include <iomanip> // used for formatting text output
#include <fstream> // file i/o
#include <sstream> // string stream for parsing csv
#include <string> // c++ string header
#include <cstdlib> // used for atoi function (convert c-string to integer)
using namespace std;


// fixed size of library database
#define LIBRARY_SIZE 10


// function prototypes
int display_menu( void );
void load_file( struct movie [] );
void display_list( struct movie [] );
void sort_list_by_title( struct movie [] );
void sort_list_by_year( struct movie [] );
void search_list( struct movie [] );


// structure definitions
struct movie {
	string title;
	int year;
};


// main function
int main()
{
	// declare variables
	struct movie movie_library[ LIBRARY_SIZE ];
	bool exit = false;

	// main program loop
	while ( exit == false ) {
		system("cls");

		switch ( display_menu() )
		{
			case 0:
				exit = true;
				break;

			case 1:
				load_file( movie_library );
				break;

			case 2:
				display_list( movie_library );
				break;

			case 3:
				sort_list_by_title( movie_library );
				break;

			case 4:
				sort_list_by_year( movie_library );
				break;

			case 5:
				search_list( movie_library );
				break;

			default:
				cout << "Not a thing!" << endl;
				break;
		}

		system("pause");
	}

	return 0;
}


int display_menu()
{
	int choice;

	cout << "1 - Load file" << endl;
	cout << "2 - Display list" << endl;
	cout << "3 - Sort list by title" << endl;
	cout << "4 - Sort list by year" << endl;
	cout << "5 - Search list for title" << endl;
	cout << "0 - Exit" << endl;
	
	cout << endl << "> ";
	cin >> choice;

	system("cls");

	return choice;
}


void load_file( struct movie movie_library[] )
{
	fstream movie_file;
	string line, token;
	stringstream ss;

	movie_file.open("movie_library.txt", ios::in);


	// open file for reading (csv file)
	if ( movie_file.is_open() )
	{
		// parse file and initialize array of structures
		int i = 0;

		// to initialize our array of structures, we must know what column holds which values

		while ( getline(movie_file, line) ) // gets each line of file
		{
			ss = stringstream(line); // create stringstream of line to get tokens

			for ( int j = 1; getline(ss, token, ','); j++ ) // gets each token in the line
			{
				if ( j == 1 ) // first column
					movie_library[ i ].title = token;
				else if ( j == 2 ) // second column
					movie_library[ i ].year = atoi(token.c_str());
			}

			i++;
		}

		cout << "File loaded." << endl;
	}
	else
	{
		cout << "Could not load file." << endl;
	}
}


void display_list( struct movie movie_library[] )
{
	// set output to left justify
	cout.setf(ios::left);

	// use setw(30) to fix column width
	cout << setw(30) << "Title" << setw(30) << "Year" << endl << endl;

	// output values
	for ( int i = 0; i < LIBRARY_SIZE; i++ ) {
		cout << setw(30) << movie_library[i].title << setw(30) << movie_library[i].year << endl;
	}

	cout << endl;

	// unset output flags
	cout.unsetf(ios::left);
}


void sort_list_by_title( struct movie movie_library[] )
{

}

//Bubble sort function
void sort_list_by_year( struct movie movie_library[] )

{ 
    int temp;     
    for (int i = 0; i < LIBRARY_SIZE - 1; i++)       
        for (int j = 0; j < LIBRARY_SIZE - 1; j++)       
        {           
            if (year[j] > year[j+1])           
            {               
                temp = year[j]; 
                year[j] = year[j+1];
                year[j+1] = temp;
             }      
         }
 }



void search_list( struct movie movie_library[] )
{
	string search_key;
	bool found = false;

	// get search key
	cout << "Please enter the movie title you are searching for: ";
	cout << "> ";
	cin.ignore(256, '\n'); // flushes new line characters from input stream
	getline(cin, search_key);


	// implement linear search here
}
Ok I managed to get my sorting function complete however when I sort by title I get the first 3 titles and then wierd characters following that. any thoughts?
Topic archived. No new replies allowed.