#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
#define N_MOVIES 3
struct movies_t {
string title;
int year;
} mine, yours, films[N_MOVIES];
void printmovie (movies_t movie);
int main()
{
string mystr;
mine.title = "2001, A Space Odyssey";
mine.year = 1968;
int n;
for (n=0; n<N_MOVIES; n++);
{
cout << "Enter title of movie " << n << ": ";
getline (cin,films[n].title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}
cout << "My favourite movie is:\n";
printmovie(mine); // Can pass all parts of the structure in one go
cout << "And yours are:\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";
}
Then I get a segmentation fault. This is because the value of "n" at line 25 immediately starts at "3" rather than "0" as I express it in the "for" loop.
The output is "Enter title of movie 3: Segmentation fault"
Can anyone explain to me why "n" does not start at 0? Sorry if this is a basic question.