Problem with sort()

I am trying to write a program that can sort a list of movies and print them to the console. However, I have a problem with my sort function. When I try to run, a new tab appears and shows a bunch of error messages with, and one of the messages is "required from here".

Here is my program:

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
//I've already include necessary libs
using namespace std;

struct Movie
{
    string title;
    string director;
    string genre;
    string year;
    string duration;
} m;

void readMovie(ifstream &inputFile, vector<Movie> &myMovies);

void printMovie(vector<Movie> &myMovies);

bool compareByTitle(Movie lhs, Movie rhs);

int main()
{
    ifstream inputFile;

    vector<Movie> myMovies;

    readMovie(inputFile, myMovies);

    sort(myMovies.begin(), myMovies.end(), compareByTitle);

    printMovie(myMovies);

    return 0;
}

void readMovie(ifstream &inputFile, vector<Movie> &myMovies)
{
    string line;
    inputFile.open("Movie_entries.txt");
    if (!inputFile)
    {
        cout << "Unable to open the file!";
    }
    while (getline(inputFile, line))   // reads a line from the file
    {

        stringstream lineStream(line);   // transforms the line into a stream
        cout << line << endl;

        // get fields from the string stream; fields are separated by comma
        getline(lineStream, m.title, ',');
        getline(lineStream, m.director, ',');
        getline(lineStream, m.genre, ',');
        getline(lineStream, m.year, ',');
        getline(lineStream, m.duration, ',');
    }
    inputFile.close();
}

void printMovie(vector<Movie> &myMovies)
{
    cout << "\tTitle: " << m.title << endl;
    cout << "\tDirector: " << m.director << endl;
    cout << "\tGenre: " << m.genre << endl;
    cout << "\tyear: " << m.year << endl;
    cout << "\tduration: " << m.duration << endl << endl;
}

bool compareByTitle(Movie lhs, Movie rhs)
{
    return lhs.title < rhs.title;
}
What error message do you get?

After adding all the missing includes ...
1
2
3
4
5
6
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector> 
... it compiles fine, but when I run it says "Unable to open the file!" because I don't have the Movie_entries.txt file.
Last edited on
When I hit build and run, a new tab named stl_algo.h appeared with a bunch of error messages.

I think there are some notable errors:

1973 error: '__i' was not declared in this scope
41 required from here
1974 error: '_RandomAccessIteratoas' was not declared in this scope


1973 and 1974 are lines from stl_algo.h. 41 is where my sort() function is.
Topic archived. No new replies allowed.