vector subscript out of range

My code is building but every time I try to run it I get an error message telling me that the debug assertion has failed on line 1795 and the expression is: vector subscript not found.
I'm not very familiar with programming and have no idea what the issue could be, I'd really appreciate some help.

the header file:

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
#ifndef MOVIE_h
#define MOVIE_H

#include <string>

using namespace std;

class Movie {
private:
	int id;
	string title;
	int year;
	double rating;


public:
	Movie(int id, string title, int year, double rating);
	~Movie();
	int getId() const;
	string getTitle() const;
	int getYear() const;
	double getRating() const;
	void setId(int id);
	void setTitle(string title);
	void setYear(int year);
	void setRating(double rating);

	friend bool operator<(const Movie& lhs, const Movie& rhs);
	friend ostream& operator<<(ostream& ostr, const Movie& m);

};

#endif 
Last edited on
the tester file:

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
#include "Movie.h"
#include <iostream>
#include<sstream>
#include <fstream>
#include<string>
#include<iomanip>
#include <vector>

vector<Movie*> movies;
vector<Movie*> betterMovies;
vector<Movie*> topMovies;
vector<Movie*>findBetterMovies(Movie *m);

void loadData();
void findTopMovie();
void printMovies(vector<Movie*>list);
void freeMemory();

using namespace std;

int main()
{
	loadData();

	cout << "-----------------------------------------" << endl;
	cout << "|           Print all movies            |" << endl;	
	cout << "-----------------------------------------" << endl;

	printMovies(movies);

	findTopMovie();

	cout << "-----------------------------------------" << endl;
	cout << "|             Find Top Movies           |" << endl;
	cout << "-----------------------------------------" << endl;

	printMovies(topMovies);

	Movie* Quench = new Movie(1029517, "Quench", 2007, 8.1);

	vector<Movie*> betterMovies = findBetterMovies(Quench);

	cout << "-----------------------------------------" << endl;
	cout << "|     Find Better Movies then Quench    |" << endl;
	cout << "-----------------------------------------" << endl;

	printMovies(betterMovies);

	cout << "-----------------------------------------" << endl;
	cout << "|           Clean up memory             |" << endl;
	cout << "-----------------------------------------" << endl;

	freeMemory();

	return 0;
}


void loadData()
{
	ifstream inFile;
	string fileName = "movies.csv";
	string data;


	inFile.open(fileName);

	while (getline(inFile, data))
	{
		stringstream dataInLine(data);	

		string title, year, rating, id;			

		getline(dataInLine, id, ',');
		getline(dataInLine, title, ',');	
		getline(dataInLine, year, ',');
		getline(dataInLine, rating, '\n');

		int myear = stoi(year);
		double mrating = stod(rating);	
		int mid = stoi(id);

		Movie* mv = new Movie(mid, title, myear, mrating);
		movies.push_back(mv);

	}
	inFile.close();

	cout << "Data Loading is completed" << endl;


}

void printMovies(vector<Movie*>list)		
{
	int i = 0;
	while (i < (int)list.size()) {
		cout << *list[i] << endl; i++;
	}

}

void findTopMovie()						
{
	double top = movies[0]->getRating();

	for (int i = 0; i < (int)movies.size(); i++)
	{

		if (movies[i]->getRating() > top)

		{
			top = movies[i]->getRating();
		}
			
	}
	for (int i = 0; i < (int)movies.size(); i++) {

		if (top == movies[i]->getRating())
		{
			topMovies.push_back(movies[i]);
		}
	}

}

vector<Movie*>findBetterMovies(Movie *m)
{
	vector<Movie*> greatestMovie;	
	for (int i = 0; i < movies.size(); i++)
	{
		if (m->getYear() > movies[i]->getYear())
		{
			greatestMovie.push_back(movies[i]);
		}
		else if (m->getYear() == movies[i]->getYear())
		{
			if (m->getRating() < movies[i]->getRating())
			{
				greatestMovie.push_back(movies[i]);
			}
		}

		return greatestMovie;
	}
}

void freeMemory()
{								
	for (int i = 0; i < (int)movies.size(); i++)
	{
		delete movies[i];
	}
}
Last edited on
the "movie" file:

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
#include "Movie.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;



Movie::Movie(int id, string title, int year, double rating)
{
	this->id = id;
	this->title = title;
	this->year = year;
	this->rating = rating;
}

int Movie::getId() const
{
	return id;
}

void Movie::setId(int id)
{
	this->id = id;
}

string Movie::getTitle() const
{
	return title;
}

int Movie::getYear() const
{
	return year;
}

double Movie::getRating() const
{
	return rating;
}



void Movie::setTitle(string Title)
{
	this->title = Title;
}

void Movie::setYear(int Year)
{
	this->year = Year;
}

void Movie::setRating(double Rating)
{
	this->rating = Rating;
}


ostream& operator << (ostream& ostr, const Movie& m) {
	ostr << m.id << " , " << m.title << " , "
		<< m.year << " , " << m.rating << endl;
	return ostr;
}

Movie::~Movie()
{
	cout << "Destructor is called for the object at " << this << endl;
}

bool operator<(const Movie& lhs, const Movie& rhs)
{
	if (lhs.year < rhs.year) 
		return true;
	
	else if (lhs.rating < rhs.rating && lhs.year < rhs.year)
		return true;
	else 
		return false;
	

}
Last edited on
"vector subscript out of range" means that you're trying to read an element in a vector that doesn't exist.

For example, if the vector is of size one, trying to read the second element is trying to read an element that doesn't exist.

While I'm here, you're making this far more complicated that it needs to be. Don't use a vector of movie-pointers. Just have a vector of movies. Do not, DO NOT, DO NOT, do you own manual memory management with new and delete.

this->
Every one of these needs to be removed. If you ever write code so bad that it becomes actually necessary, still don't. Fix the other bad code instead.
Yea, I know a lot of this is rediculous. It's part of the requirements for the project. And I've been trying to see what vector I messed up on. Thought maybe a second pair of eyes would help.
Last edited on
Your file reading code doesn't check for errors. How do you know it works? If I had to guess, it doesn't work, because the file "movies.csv" isn't isn't in the right place. That would make this line
double top = movies[0]->getRating();
fail

Although if you run it under a debugger, the debugger will simply tell you the exact line. Five minutes learning to use the debugger will save you hours and hours. Would already have saved you the time you've spent on this thread.
Last edited on
That was the problem. Thank you!
Topic archived. No new replies allowed.