hello i have a problem with structures

hello please help me.
im trying to make this code more dynmic i mean that
the array is not constant, i want that the user can input
lets say cout << "how many movies did you seen";
cin >> arraysize;

truct movies_t {
string title;
int year;
} films [arraysize];

but that's impossible becouse the arraysize has no value
so it give's me compiler error.

this is the exemple from the tutorial on ths site
about arrays and structures

i tried to combine the structure with pointer with no result,
please help me.

// array of structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#define N_MOVIES 3

struct movies_t {
string title;
int year;
} films [N_MOVIES];

void printmovie (movies_t movie);

int main ()
{
string mystr;
int n;

for (n=0; n<N_MOVIES; n++)
{
cout << "Enter title: ";
getline (cin,films[n].title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}

cout << "\nYou have entered these movies:\n";
for (n=0; n<N_MOVIES; n++)
printmovie (films[n]);
return 0;
}

void printmovie (movies_t movie)
{
cout << movie.title;
cout << " (" << movie.year << ")\n";
}

best regards,
Evgeny Bershadski.
In C++, array sizes can only be dynamic if the array is dynamically allocated (by the use of new). But you should use a std::vector for something like this (it keeps a dynamically allocated array under the covers but does the housekeeping for you).
Topic archived. No new replies allowed.