Segmentation fault

Hello, I'm new to cpp and to the forums. Thanks in advance for any help.

I'm writing a program that's supposed to sort several albums as well as the songs within these albums alphabetically. I'm not to the point of sorting yet, I'm still trying to get the data stored in my struct correctly. This data is read in from a text file. The first two lines are read in as album name and album year and saved to strings in a struct. The songs are then read into an array of strings in the same struct. The end of each album is demarcated by a row of ='s. The next album begins on the next line. There are 5 albums with a maximum of 30 songs in each album.

I'm getting a segmentation fault about halfway through the third album. Any insight you guys may provide is appreciated.

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
struct Albums
{
        string albumName;
        string year;
        string songs[30];
};

int main()
{
        Albums structtest[5];
        ifstream file;
        file.open("albums-songs.txt");

        int i = 0;
        int j = 0;
        int dm = 1;

for(j = 0; j < 5; j++)
{
        getline(file, structtest[j].albumName);
        getline(file, structtest[j].year);


        cout << structtest[j].albumName << endl;
        cout << structtest[j].year << endl;


        while(dm)
        {
                getline(file, structtest[j].songs[i]);
                cout << structtest[j].songs[i] << endl;
                dm = structtest[j].songs[i].find("=");
                i++;
        }
        dm = 1;
        i = 0;
}
        file.close();
}
I bet i is going over 29.

What does the output show?
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
50
51
52
53
54
55
56
57
58
59
60
61
62
The Beatles White Album
Year:  1968
  1. Back in the U.S.S.R.
  2. Dear Prudence
  3. Glass Onion
  4. Ob-La-Di, Ob-La-Da
  5. Wild Honey Pie
  6. The Continuing Story of Bungalow Bill
  7. While My Guitar Gently Weeps
  8. Happiness Is a Warm Gun
  9. Martha My Dear
 10. I'm So Tired
 11. Blackbird
 12. Piggies
 13. Rocky Raccoon
 14. Don't Pass Me By
 15. Why Don't We Do It in the Road?
 16. I Will
 17. Julia
 18. Birthday
 19. Yer Blues
 20. Mother Nature's Son
 21. Everybody's Got Something to Hide Except Me and My Monkey
 22. Sexy Sadie
 23. Helter Skelter
 24. Long, Long, Long
 25. Revolution 1
 26. Honey Pie
 27. Savoy Truffle
 28. Cry Baby Cry
 29. Revolution 9
 30. Good Night
===============================
Abbey Road
Year:  1969
  1. Come Together
  2. Something
  3. Maxwell's Silver Hammer
  4. Oh! Darling
  5. Octopus's Garden
  6. I Want You (She's So Heavy)
  7. Here Comes the Sun
  8. Because
  9. You Never Give Me Your Money
 10. Sun King
 11. Mean Mr. Mustard
 12. Polythene Pam
 13. She Came in Through the Bathroom Window
 14. Golden Slumbers
 15. Carry That Weight
 16. The End
 17. Her Majesty
===============================
Magical Mystery Tour
Year:  1967
  1. Magical Mystery Tour
  2. The Fool on the Hill
  3. Flying
  4. Blue Jay Way
  5. Your Mother Should Know
Segmentation fault

Here's some code to display the values of indexes while it's going through the paces.

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

struct Albums
{
        string albumName;
        string year;
        string songs[30];
};

int main()
{
        Albums structtest[5];
        ifstream file;
        file.open("albums-songs.txt");

        int i = 0;
        int j = 0;
        int dm = 1;

for(j = 0; j < 5; j++)
{
        getline(file, structtest[j].albumName);
        getline(file, structtest[j].year);

	cout << "j " << j << endl;

        cout << "albumname " << structtest[j].albumName << endl;
        cout << "year " << structtest[j].year << endl;


        while(dm)
        {
                getline(file, structtest[j].songs[i]);
                cout << "j " << j <<  "songs" << i << " " << structtest[j].songs[i] << endl;
                dm = structtest[j].songs[i].find("=");
                cout << "dm.find('=')" << dm << endl;
                i++;
        }
        dm = 1;
        i = 0;
}
	file.close();
}


and the output:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
j 0
albumname The Beatles White Album
year Year:  1968
j 0songs0  1. Back in the U.S.S.R.
dm.find('=')-1
j 0songs1  2. Dear Prudence
dm.find('=')-1
j 0songs2  3. Glass Onion
dm.find('=')-1
j 0songs3  4. Ob-La-Di, Ob-La-Da
dm.find('=')-1
j 0songs4  5. Wild Honey Pie
dm.find('=')-1
j 0songs5  6. The Continuing Story of Bungalow Bill
dm.find('=')-1
j 0songs6  7. While My Guitar Gently Weeps
dm.find('=')-1
j 0songs7  8. Happiness Is a Warm Gun
dm.find('=')-1
j 0songs8  9. Martha My Dear
dm.find('=')-1
j 0songs9 10. I'm So Tired
dm.find('=')-1
j 0songs10 11. Blackbird
dm.find('=')-1
j 0songs11 12. Piggies
dm.find('=')-1
j 0songs12 13. Rocky Raccoon
dm.find('=')-1
j 0songs13 14. Don't Pass Me By
dm.find('=')-1
j 0songs14 15. Why Don't We Do It in the Road?
dm.find('=')-1
j 0songs15 16. I Will
dm.find('=')-1
j 0songs16 17. Julia
dm.find('=')-1
j 0songs17 18. Birthday
dm.find('=')-1
j 0songs18 19. Yer Blues
dm.find('=')-1
j 0songs19 20. Mother Nature's Son
dm.find('=')-1
j 0songs20 21. Everybody's Got Something to Hide Except Me and My Monkey
dm.find('=')-1
j 0songs21 22. Sexy Sadie
dm.find('=')-1
j 0songs22 23. Helter Skelter
dm.find('=')-1
j 0songs23 24. Long, Long, Long
dm.find('=')-1
j 0songs24 25. Revolution 1
dm.find('=')-1
j 0songs25 26. Honey Pie
dm.find('=')-1
j 0songs26 27. Savoy Truffle
dm.find('=')-1
j 0songs27 28. Cry Baby Cry
dm.find('=')-1
j 0songs28 29. Revolution 9
dm.find('=')-1
j 0songs29 30. Good Night
dm.find('=')-1
j 0songs30 ===============================
dm.find('=')0
j 1
albumname Abbey Road
year Year:  1969
j 1songs0  1. Come Together
dm.find('=')-1
j 1songs1  2. Something
dm.find('=')-1
j 1songs2  3. Maxwell's Silver Hammer
dm.find('=')-1
j 1songs3  4. Oh! Darling
dm.find('=')-1
j 1songs4  5. Octopus's Garden
dm.find('=')-1
j 1songs5  6. I Want You (She's So Heavy)
dm.find('=')-1
j 1songs6  7. Here Comes the Sun
dm.find('=')-1
j 1songs7  8. Because
dm.find('=')-1
j 1songs8  9. You Never Give Me Your Money
dm.find('=')-1
j 1songs9 10. Sun King
dm.find('=')-1
j 1songs10 11. Mean Mr. Mustard
dm.find('=')-1
j 1songs11 12. Polythene Pam
dm.find('=')-1
j 1songs12 13. She Came in Through the Bathroom Window
dm.find('=')-1
j 1songs13 14. Golden Slumbers
dm.find('=')-1
j 1songs14 15. Carry That Weight
dm.find('=')-1
j 1songs15 16. The End
dm.find('=')-1
j 1songs16 17. Her Majesty
dm.find('=')-1
j 1songs17 ===============================
dm.find('=')0
j 2
albumname Magical Mystery Tour
year Year:  1967
j 2songs0  1. Magical Mystery Tour
dm.find('=')-1
j 2songs1  2. The Fool on the Hill
dm.find('=')-1
j 2songs2  3. Flying
dm.find('=')-1
j 2songs3  4. Blue Jay Way
dm.find('=')-1
j 2songs4  5. Your Mother Should Know
dm.find('=')-1
Segmentation fault
Here, i reaches 30 (should only go up to 29):

j 0songs30 ===============================
Topic archived. No new replies allowed.