0

I have a program that reads from a text file the name of a movie and fields related to that movie. I want to include those movies as a struct but I don't know how to initialize automatically. This is my code to read from the txt file.

    string line;
    while (getline(z, line))
    {
        istringstream iss(line);
        int ane = line.find(";");
        string roa = line.erase(0, ane + 1);
        string actor = line.substr(0, ane);
        int mns = line.find_first_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM");
        string mn = roa.substr(mns);
        movies movie;
        construct(movie, actor);

    }

my struct:

struct movies {
    int rank;
    string title;
    string actor;
    float rating;
};

And I want to use a void function to do the initialization but it doesn't seem to work so I want to ask if it is possible to do it. Maybe by using vectors or pointers but I don't know how so I would appreciate it if you could tell me how it works.

  • 3
    are you looking for a dynamic array? `std::vector` ? – 463035818_is_not_an_ai Dec 27 '20 at 15:14
  • It doen't seem I fully understand your question well, but *references* are useful to doing initialization via void function and [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) is useful to hold unknown amount of input. – MikeCAT Dec 27 '20 at 15:14
  • Do I have to know the inputs for a dynamic array or can it just grab the inputs from the text file? If it can then that would be what I am looking for. – ozan esici Dec 27 '20 at 15:15
  • You will find a vector much much easier. But in either case you can grab the input from the file. – john Dec 27 '20 at 15:16
  • Can I create a ```std::vector``` called ```moviename``` and then use it in my struct ```movies moviename;``` – ozan esici Dec 27 '20 at 15:18
  • @ozanesici Each struct is for one movie. The vectors is for all the movies. When you read one movie add the input to the movie struct, then add the struct to the vector. – john Dec 27 '20 at 15:21
  • @ozanesici I'll write up an answer to show you roughly how it shuold look – john Dec 27 '20 at 15:22
  • oh I see. Alright I'll go and look into it. Thanks! – ozan esici Dec 27 '20 at 15:22
  • @john that would be helpful. – ozan esici Dec 27 '20 at 15:22
  • 1
    Does this answer your question? [c++ dynamic array of structures](https://stackoverflow.com/questions/14139076/c-dynamic-array-of-structures) – JHBonarius Dec 27 '20 at 15:26

2 Answers2

1

You can create a vector with the type movies in the main and then push_back() all the separate structs(movies) into the vector you created

atmacaa
  • 31
  • 5
  • I can do this but how do I then print or edit the vector? – ozan esici Dec 27 '20 at 15:36
  • 1
    you can use a basic for loop to print for example: for(int i = 0 ; I < myVector.size; i++) and inside this loop if you want to print or edit something you can write myVector[i].title and then print it or = it to something. You can also do it without a loop just by writing for instance myVector[5].title = "Starwars" ; – atmacaa Dec 27 '20 at 15:38
  • Ok if that works then there is no problems. – ozan esici Dec 27 '20 at 15:41
1

Something like this

struct movie {
    int rank;
    string title;
    string actor;
    float rating;
};

vector<movie> all_movies; // this vector will hold all the movies
string line;
while (getline(z, line))
{
    ...
    movie m;               // this is one movie that we've read from line
    m.rank = ...;          // set the rank
    m.title = ...;         // set the title
    m.actor = ...;         // set the actor
    m.rating = ...;       // set the rating
    all_movies.push_back(m); // add the movie to the vector
}
john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    Regarding RAII, I would rather use `push_back(movie{/*rank*/,/*title*/,...};` or better even `emplace_back`. But anyhow, this is a dupe IMHO. – JHBonarius Dec 27 '20 at 15:28
  • I don't quite know how this works but I assume it does the same task as the other push back. Though I want to know how you access or print the data in the vector. – ozan esici Dec 27 '20 at 15:34
  • @ozanesici You can access the data in the vector in the same way as you would with an array `movies[i].actor` for instance. You can also get the size of the vector with `movies.size()`. – john Dec 27 '20 at 16:21
  • @ozanesici And yes the code JHB showed you does the same thing as my code but in a slightly better way. So if you are comfortable with that syntax then use it. – john Dec 27 '20 at 16:23