1
using namespace std;
struct Movie {
    string title;
    string director;
    string genre;
    string yearRelease;
    string duration;
};
int main(){
    cout << "Hi";
    ifstream fin;
    string line;

    vector <Movie> m;
    fin.open("Movie_entries.txt");
    while (getline(fin, line)) {
        cout << line << endl;
        stringstream lineStream(line);
        getline(lineStream, m.title, ',');
        getline(lineStream, m.director, ',');
        getline(lineStream, m.genre, ',');
        getline(lineStream, m.yearRelease, ',');
        getline(lineStream, m.duration, ',');
        m.push_back({title, director, genre, yearRelease, duration});
    }
}

I am trying to push back the struct into the vector to store my data and am having trouble in how exactly to do that. This is what I currently have.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Max Millin
  • 9
  • 1
  • 3
  • You need to make a movie object such as Movie firstMovie. Then get info for all things in struct into firstMovie, then push_back(firstMovie). Remember your vector can TAKE IN "of types movies", it doesn't mean you already have the object of Movie created.. – Omid CompSCI Jan 28 '17 at 02:56
  • Off topic: You look to be in an [excellent position to `emplace_back`](http://en.cppreference.com/w/cpp/container/vector/emplace_back) which jumps over the need for a temporary by doing almost exactly what you've tried with `push_back`. – user4581301 Jan 28 '17 at 03:05
  • @OmidCompSCI The `push_back` should work as is though, see [here](http://coliru.stacked-crooked.com/a/af8b615bb8c52a80), since `Movie` is an [aggregate](http://stackoverflow.com/q/4178175/3093378) and braces can be used for its initialization. Basically the compiler is allowed to convert `{...}` into `Movie{...}`. – vsoftco Jan 28 '17 at 03:08
  • @user4581301, `emplace_back` uses parentheses (at least with the default allocator), so it wouldn't work with the code as given. – chris Jan 28 '17 at 03:13
  • So what's the problem with this code? How do you know it doesn't work? – chris Jan 28 '17 at 03:15
  • @chris Those are weird: `getline(lineStream, m.title, ',');` OP needs to read into a `string` variable, something like `getline(lineStream, title, ',');` etc. – vsoftco Jan 28 '17 at 03:15
  • @chris no it wouldn't: "almost exactly". – user4581301 Jan 28 '17 at 03:17
  • @vsoftco, I knew that but you pointed out that it needs to be something like (lineStream, title, ',') rather than m.title, that is what really messed me up – Omid CompSCI Jan 28 '17 at 17:52

1 Answers1

4

You just need to create a struct variable; set attribute for it; then push that struct to the vector.

In C++, declare a struct variable with Movie aMovie; is good enough. No need for struct Movie aMovie;.

using namespace std;
    struct Movie {
        string title;
        string director;
        string genre;
        string yearRelease;
        string duration;
    };
int main(){
    cout << "Hi";
    ifstream fin;
    string line;

    vector <Movie> m;
    fin.open("Movie_entries.txt");
    while (getline(fin, line)) {
        cout << line << endl;
        stringstream lineStream(line);
        struct Movie aMovie;
        getline(lineStream, aMovie.title, ',');
        getline(lineStream, aMovie.director, ',');
        getline(lineStream, aMovie.genre, ',');
        getline(lineStream, aMovie.yearRelease, ',');
        getline(lineStream, aMovie.duration, ',');
        m.push_back(aMovie);
    }
}
cuongptnk
  • 472
  • 3
  • 15