Problem with for-loop variable value

Hello,
I'm following the example at http://www.cplusplus.com/doc/tutorial/structures/ but I seem to have hit a strange problem.

If I run the following code:

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
#include <iostream>
#include <string>
#include <sstream>
using namespace 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.

Thank you in advance!
That is because you've put a ; after the for loop in that very line. Remove it and you'll be fine...
Caprico,
Indeed you're right! Thanks for pointing this out - I appreciate it!
You're welcome! :)
Topic archived. No new replies allowed.