segmentation fault

I keep getting a segmentation fault error when running this program, it also cycles through the menu 100 times then stops, I don't know why it won't find the memory to write to the file.

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  #include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>

using namespace std;
const int listSize = 21;

struct songInfo
{
	char title[30];
	char artist[30];
	int durationMin = 0;
	int durationSec = 0;
	char album[30];
};


void addSong(songInfo list[], int& listSize); 
void removeSong(songInfo list[], int& listSize);
void displayList(songInfo list[], int listSize);
void searchList(songInfo list[], int listSize);
void readList(ifstream& infile, songInfo list[], int listSize);
void printList(ofstream& outfile, songInfo list[], int listSize);

int main()
{
	songInfo trackListing[listSize];
	int listSize = 0; //KN trackListing starts with count = 0
	char selection;

	cout << endl;
	cout << "Travis' Song Tracker" << endl;
	do
 	{

		ofstream outfile("songs.txt");
		ifstream infile("songs.txt");


		if (!infile)
                	{
			cout << "Cannot open file: songs.txt" << endl;
			return 0;
                	}

		cout << "Main Menu:" << endl;
		cout << endl;
		cout << "(a)dd a song" << endl;
		cout << "(r)emove a song" << endl;
		cout << "(d)isplay track listings" << endl;
		cout << "(s)earch for a track" << endl;
		cout << "(q)uit" << endl;
		cout << endl;
		cout << "Select (a, r, d, s, q): ";
		cin >> selection;
		cout << endl;

		if (selection != 'a' && selection != 'r' && selection != 'd' && selection != 's' && selection != 'q')
		{
			cout << "Select (a, r, d, s, q): ";
			cin >> selection;
			cout << endl;
		}

		switch (selection)
		{
			
		case 'a':
			readList(infile, trackListing, listSize);
			addSong(trackListing, listSize);//KN
			printList(outfile, trackListing, listSize);
			break;
		case 'd':
			readList(infile, trackListing, listSize);
			displayList(trackListing, listSize);
			break;
		case 'r':
			removeSong(trackListing, listSize);//KN
			printList(outfile, trackListing, listSize);
			break;
		case 's':
			readList(infile, trackListing, listSize);
			searchList(trackListing, listSize);
			break;
		}
	} while (selection != 'q');

	cout << "Goodbye" << endl;
	cout << endl;

	return 0;
}

void addSong(songInfo list[], int& listSize)
{
	int i;
	cout << "Please add a Song" << endl;
	cout << endl;
	/*KN
	No need for this as listSize is going to keep the count of elements
	in the list array
	cout << "Please enter a Song Number in the list: ";
	cin >> i;
	cout << endl;
	*/

	cout << "Please enter a Song Title: ";
	cin.ignore();
	std::cin.get(list[listSize].title, 30);
	cout << endl;

	cout << "Please enter an Artist Name: ";
	cin.ignore();
	std::cin.get(list[listSize].artist, 30);
	cout << endl;

	cout << "Please enter the Track Duration: ";
	cout << endl;
	cout << "(Seperate mins & secs with a space.";
	cout << endl;
	cout << "ex: if 3:40, enter 3 40): ";
	cin >> list[listSize].durationMin >> list[listSize].durationSec;
	cout << endl;

	cout << "Please enter the Album Title: ";
	cin.ignore();
	std::cin.get(list[listSize].album, 30);
	cout << endl;
	cout << "Your Song is added to the playlist!";
	cout << endl;
	cout << endl;
	listSize++;
}

void readList(ifstream& infile, songInfo list[], int listSize)
{
	int i;
	infile.open("songs.txt");
	while (infile)
	{
		for (i = 1; i<listSize; i++)
		{
			infile.get(list[i].title, 30);
			infile.get(list[i].artist, 30);
			infile >> list[i].durationMin >> list[i].durationSec;
			infile.get(list[i].album, 30);
		}
	}
	infile.close();
}
void printList(ofstream& outfile, songInfo list[], int listSize)
{
	infile.open("songs.txt");
	int i;
	for (i = 1; i<listSize; i++)
	{
		outfile << left << setw(2) << i << left << setw(32) << list[i].title << ';' << left << setw(32) << list[i].artist << ';' << right << setw(4) << list[i].durationMin << ';' << left << setw(5) << list[i].durationSec << ';' << left << setw(32) << list[i].album << ';' << endl;
	}
	out.close();
}

void removeSong(songInfo list[], int &listSize) //KN
{
	int i = 1;
	int e;
	cout << "Remove a Song" << endl;
	cout << "Enter Track Number to Confirm Deletion: ";
	cin >> e;
	cout << endl;

	for (i = 1; i<listSize; i++)
	{
		if (e == i)
		{
			for (i = e; i<listSize; i++)
			{
				list[i] = list[i + 1];
			}
		}
	}
}
void displayList(songInfo list[], int listSize)
{
	int i;
	cout << left << setw(14) << '#' << left << setw(21) << "Song Name" << left << setw(21) << "Artist Name" << left << setw(20) << "Duration" << left << setw(22) << "Album Title" << endl;
	cout << left << setw(110) << setfill('-') << '-' << endl;
	cout << setfill(' ');
	for (i = 1; i<listSize; i++)
	{
		cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
	}
	cout << left << setw(110) << setfill('-') << '-' << endl;
	cout << setfill(' ');
}
void searchList(songInfo list[], int listSize)
{
	char aOrB;
	int i = 1;
	char art[30];
	char alb[30];

	cout << "Search by (a)rtist or al(b)um? (a or b): ";
	cin >> aOrB;
	cout << endl;

	if (aOrB != 'a' && aOrB != 'b')
	{
		cout << "(a or b): ";
		cin >> aOrB;
		cout << endl;
	}
	if (aOrB == 'a')
	{
		cout << "Artist Name: ";
		cin.ignore();
		cin.get(art, 30);

		for (i = 1; i<listSize; i++)
		{
			if (!strcmp(art, list[i].artist))
			{
				cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
			}
		}
	}
	else
	{
		cout << "Album Name: ";
		cin.ignore();
		cin.get(alb, 30);

		for (i = 1; i<listSize; i++)
		{
			if (!strcmp(alb, list[i].album))
			{
				cout << left << setw(2) << i << left << setw(32) << list[i].title << left << setw(32) << list[i].artist << right << setw(4) << list[i].durationMin << ':' << left << setw(5) << list[i].durationSec << left << setw(32) << list[i].album << endl;
			}
			else
				cout << "Album not found" << endl;
		}
	}
}
Last edited on
songInfo trackListing[listSize];

any chance this ^^^ thing that is 21 in size is way, way too small for 100 things (or did I fail to understand your problem?)

set it to 200 instead of 21, see if that helps. Also check your input for > 29 chars etc. It has to be an index out of bounds somewhere.
Last edited on
> ofstream outfile("songs.txt");
> ifstream infile("songs.txt");
Having the same file open for both reading and writing isn't going to be good.
You also close the file in the same loop (when the default dtor happens at the end of the scope).

You also open and close the file AGAIN in functions like readList and printList.

Your removeSong doesn't decrement listSize

> for (i = 1; i<listSize; i++)
Do you know that arrays start at subscript 0 right?

where do you see 29 chars?
if I try to use fstream for read and write it messes up the whole code
30 chars all over your struct, 29+zero end of string letter... if you have a 30 or larger input it will go out of bounds.


fstream works just find. If it messed up, you have an error... that we can't see because you didn't show it..
Last edited on
Should I try fstream at the top declaration and remove the other infile.close and outfile.close?
I'd perhaps start with removing these from main
1
2
	ofstream outfile("songs.txt");
		ifstream infile("songs.txt");


Removing the stream parameters from these
1
2
void readList(ifstream& infile, songInfo list[], int listSize);
void printList(ofstream& outfile, songInfo list[], int listSize);


Making the implementation of those two functions declare a local stream to just read the file, or write the file as appropriates.

Topic archived. No new replies allowed.