Trying to display genre

I'm trying to display the genre of this. It creates the file and add the input file but doesn't display it. It just closes the program without the output. I've tried so many ways to fix it to display the genre of the genre but can't seem to get any ideas.


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
#pragma once

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

class Album
{
	public:
		int albumID;
		string albumName;
		int releaseDate;
		int numArtists;
		
		Album()
		{
			albumID = 0;
			albumName = "und";
			releaseDate = 0;
			numArtists =0;
		}
	
	
};
class Genre
{
	public:
	int genreID;
	string genre;
	Genre()
	{
		genreID = 0;
		genre = "und";
	}
	
	
};

class Artist
{
	public:
		int artistID;
		string artistName;
		Artist()
		{
			artistID = 0;
			artistName = "und";
		}
};

struct A_B_Int
{
	int albumID;
	int artistID;
	int genreID;
	A_B_Int()
	{
		albumID = 0;
		artistID = 0;
		genreID = 0;
	}
	
};

class Config
{
	public:
		string albumFileName;
		string artistFileName;
		string ABIFileName;
		string genreFileName;
		string configFileName;
		string temp;
		
		ifstream inputFile;
		
		int albumCount, artistCount, B_A_IntCount, genreCount;
		
		Config()
		{
			albumFileName = "albumfile.dat";
			artistFileName = "artistfile.dat";
			ABIFileName = "ABIfile.dat";
			genreFileName = "genrefile.dat";
			configFileName = "config.dat";
			
			inputFile.open (configFileName);
				if (inputFile)
				{
					getline(inputFile, temp);
					albumCount = stoi(temp);
					getline(inputFile, temp);
					artistCount = stoi(temp);
					getline(inputFile, temp);
					B_A_IntCount = stoi(temp);
					getline(inputFile, temp);
					genreCount = stoi(temp);
					
				}
				else
				{
					cout << "Error opening file.  Creating a new configuration... " << endl;
					albumCount = 0;
					artistCount = 0;
					B_A_IntCount = 0;
					genreCount = 0;
				}
				inputFile.close();
		}
		
};

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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  #include <iostream>
#include <fstream>
#include <iomanip>
#include "library.h"
using namespace std;

//Function prototypes
char displayMenu();  
void displayLibrary(Config &c);
void addAlbum(Config &c);

int main()
{
	char choice;
	bool notValid = true;
	bool notDone = true;
	
	Config config;
	
	cout << "Album Count: " << config.albumCount << endl;
	cout << "Artist Count: " << config.artistCount << endl;
	cout << "Album-Artist Intersection Count: " << config.B_A_IntCount << endl;
	cout<<"Genre"<<config.genreCount<<endl;
	
	
	do
	{
		choice = displayMenu();
		
		switch (toupper(choice))
		{
			
			case 'A':
				displayLibrary(config);
				
				break;
			
			case 'B':
				//cout << "You Chose 'B'!" << endl;
				
				break;
			
			case 'C':  //Add album option
				addAlbum(config);
				break;
			
			/*case 'D':   Don't need since artist is added with album...
				cout << "You Chose 'D'!" << endl;
				break; */
			
			case 'X':
				cout << "Thank you for playing\nGoodbye!" << endl;
				notDone = false;
				break;
			default:
				cout << "Choice was not A, B, C, or X \nPlease try again..." << endl;
		}
		
		
		
	} while(notDone); // could also be while(choice != 'X' && choice !='x')
	
	
	
	
	return 0;
}

char displayMenu()
{
	char menuChoice;
	
	cout << "Enter the letter for the menu option you want:" << endl;
	cout << "A.  Display Library" << endl;
	cout << "B.  Search Library" << endl;
	cout << "C.  Add Album" << endl;
	//cout << "D.  Add Artist" << endl;  No longer need, since we're adding artist along with album...
	cout << "X.  Quit Program" << endl;
				
	cin >> menuChoice;
		
	cin.ignore(256, 10); //ignore up to 256 "Extra" characters or until "\n"
	//cin.ignore();
	return menuChoice;
}

void displayLibrary(Config &c)
{
	ifstream inputFile;
	string temp;
	int artistDisplayed = 0;
	int genresDisplayed = 0;
	
	Album albums[c.albumCount];
	Artist artists[c.artistCount];
	A_B_Int abint[c.B_A_IntCount];
	Genre genre[c.genreCount];
	// Read in albums:
	/* int albumID;
		string albumName;
		int releaseDate;
	*/
	inputFile.open(c.albumFileName);
	if (!inputFile)
	{
		cout << "Error opening album file." << endl;
	}
	else
	{
		for (int i = 0; i < c.albumCount; i++)
		{
			getline(inputFile, temp);
			albums[i].albumID = stoi(temp);
			getline(inputFile, temp);
			albums[i].albumName = temp;
			getline(inputFile, temp);
			albums[i].releaseDate = stoi(temp);
			getline(inputFile, temp);
			albums[i].numArtists = stoi(temp);
		}
	}
	inputFile.close();
	
	// Read in artists:
	/*	int artistID;
		string artistName; */
	inputFile.open(c.artistFileName);
	if (!inputFile)
	{
		cout << "Error opening artist file." << endl;
	}
	else
	{
		for (int i = 0; i < c.artistCount; i++)
		{
			getline(inputFile, temp);
			artists[i].artistID = stoi(temp);
			getline(inputFile, temp);
			artists[i].artistName = temp;
			
		}
	}
	inputFile.close();	
	
	inputFile.open(c.genreFileName);
	if(!inputFile)
	{
		cout<<"Error opening genre file."<<endl;
		
	}
	else
	{
		for(int i = 0; i < c.genreCount; i++)
		{
			getline(inputFile, temp);
			genre[i].genreID = stoi(temp);
			getline(inputFile, temp);
			genre[i].genre = temp;
		}
	}
	inputFile.close();		
	// Read in b_a_Intercepts:
	/*	int albumID;
		int artistID; */
	inputFile.open(c.ABIFileName);
	if (!inputFile)
	{
		cout << "Error opening album-artist intercept file." << endl;
	}
	else
	{
		for (int i = 0; i < c.B_A_IntCount; i++)
		{
			getline(inputFile, temp);
			abint[i].albumID = stoi(temp);
			getline(inputFile, temp);
			abint[i].artistID = stoi(temp);
			getline(inputFile,temp);
			abint[i].genreID = stoi(temp);
			
		}
	}
	inputFile.close();	
	
	

	// Display library
	cout << setw(15) << left << "Album Name" << "Artist(s)" << endl;
	for (int i = 0; i < c.albumCount; i++)
	{ 

	
	
		cout << setw(15) << left << albums[i].albumName;
		cout << setw(5) << albums[i].releaseDate;
		
		for (int j = 0; j < c.B_A_IntCount; j++)
		{
			if(abint[j].albumID == albums[i].albumID)
			{	
				//artistID = abint[j].artistID;
				cout << artists[abint[j].artistID].artistName;
				artistDisplayed++;
				if (genresDisplayed <= albums[i].numArtists)
				{
					cout << ", ";
				}
				else
					j = c.B_A_IntCount;
				
			}
		}
		cout << endl;
		
		
	}
}

void addAlbum(Config &c)
{
	ofstream outputFile;
	
	string albumName, artistName, genre;
	int albumID;
	int releaseDate;
	int numArtists;
	int genreID;
	
	cout << "Please enter the album's name: " ;
	getline(cin, albumName);
	cout << "Please enter the album ID (current autonumber is: " << (c.albumCount+1) << "): ";
	cin >> albumID;
	cout << "Please enter the album's release date (YYYY): ";
	cin >> releaseDate;
	cout <<"genre";
	cin.ignore(256,10);
	getline(cin, genre);
	cout << "How many artists are on the album (minumum 1, if unknown)?  ";
	cin >> numArtists;
	
	outputFile.open(c.albumFileName, std::ios_base::app);
	outputFile << albumID << endl;
	outputFile << albumName << endl;
	outputFile << releaseDate << endl;
	outputFile << numArtists << endl;
	outputFile.close();
	c.albumCount++;
	outputFile.open(c.genreFileName, std::ios_base::app);
	outputFile <<c.artistCount<<endl;
	outputFile <<genre<<endl;
	outputFile.close();
	c.genreCount++;
		
	
	cin.ignore(256, 10);
	for (int authCount = 0; authCount < numArtists; authCount++)
	{
		
		cout << "Enter the artist's name: " ;
		getline(cin, artistName);
		//Add artist to artist table
		outputFile.open(c.artistFileName, std::ios_base::app);
		outputFile << c.artistCount << endl;
		outputFile << artistName << endl;
		outputFile.close();
		
		//Add the artist/album interception
		outputFile.open(c.ABIFileName, std::ios_base::app);
		outputFile << albumID << endl;
		outputFile << c.artistCount << endl;
		outputFile.close();
		
		c.B_A_IntCount++;
		
		c.artistCount++;
	}
	
	
	
	// Update Config File:
	outputFile.open(c.configFileName);
	outputFile << c.albumCount << endl;
	outputFile << c.artistCount << endl;
	outputFile << c.B_A_IntCount << endl;
	outputFile<<c.genreCount<<endl;
	outputFile.close();
	
}
So have you tried running the code in the debugger, and setting a breakpoint at say where you read the file in?

but doesn't display it

Well, it’s possible to make your program compile, but it raises 7 important warnings: just reading them would help you a lot.

Your error description is too concise and you didn’t provide any examples of your data, so it’s hard to guess where the main problem could be. However, in my opinion you could at least improve your program in three ways:

1) You shouldn’t declare your variables in a bunch at the beginning of functions, because it can leads to hard to detect bugs.
For example, in addAlbum(), the variable ‘genreID’ is declared, but later neglected.
Is it possible that it was supposed to contain some data which later had to be written to some file and, skipping it, the ‘fields’ in that file will move in the wrong sequence?

Anyway, if you hadn’t declare ‘artistName’ so far from where you use it, you'd probably noticed you close() ‘outputFile’ inside a loop, so that it closes at the first iteration - it means later you attempt to write to a closed stream.

Please, have a look here:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rnr-top


2) Do not write to much before testing your code.
Try to compile it often. Do not neglect warnings just because they don’t stop compilation.
You functions are long and, in my opinion, they don’t belong where they are.
I think they should be methods of a class that manages all those information.
If you store your information into local objects, you’ll need to read them again every time you need them.
(Well, it actually could be a design choice, if you loaded in memory only the relevant data)

You could consider splitting long functions into smaller ones, e.g. a function for every writing operation - I mean one for updating artist file, one for updating album file…


3) Album albums[c.albumCount];
Variable length arrays are not welcome in C++. What about std::vectors?


Also:
- do try to be a bit more consistent in your spacing and indentation;
- do avoid “using namespace std” (only experienced programmers should use it);
- let us test your code against your data files;
- do consider exiting from functions returning error codes in case of failures.

In the next two posts I’m going to provide an example of your code written in a slightly different style (just for comparison) and unified into a single file. It deals with some minor issues, but not with the biggest ones mentioned above.
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
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>


struct Album {
    int albumID {};
    std::string albumName { "und" };
    int releaseDate {};
    int numArtists {};

    Album() = default;
};


struct Genre {
    int genreID {};
    std::string genre;

    Genre() = default;
};


struct Artist {
    int artistID {};
    std::string artistName;

    Artist() = default;
};


struct A_B_Int {
    int albumID  {};
    int artistID {};
    int genreID  {};

    A_B_Int() = default;
};


class Config {
public:
    std::string albumFileName = "albumfile.dat";
    std::string artistFileName = "artistfile.dat";
    std::string ABIFileName = "ABIfile.dat";
    std::string genreFileName = "genrefile.dat";
    std::string configFileName = "config.dat";

    int albumCount;
    int artistCount;
    int B_A_IntCount;
    int genreCount;

    Config();
};


Config::Config()
{
    std::ifstream inputFile(configFileName);
    if ( !inputFile )
    {
        std::cout << "Error opening file. Creating a new configuration...\n";
        albumCount = 0;
        artistCount = 0;
        B_A_IntCount = 0;
        genreCount = 0;
        return;
    }

    std::string temp;
    getline(inputFile, temp);
    albumCount = std::stoi(temp);

    getline(inputFile, temp);
    artistCount = std::stoi(temp);

    getline(inputFile, temp);
    B_A_IntCount = std::stoi(temp);

    getline(inputFile, temp);
    genreCount = std::stoi(temp);
}


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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//Function prototypes
char displayMenu();
void displayLibrary(Config &c);
void addAlbum(Config &c);


int main()
{
    Config config;
    std::cout << "Album Count: " << config.albumCount
              << "\nArtist Count: " << config.artistCount
              << "\nAlbum-Artist Intersection Count: " << config.B_A_IntCount
              << "\nGenre" << config.genreCount << '\n';

    char choice;
    do {
        choice = displayMenu();

        switch (::toupper(choice))
        {

        case 'A':
            displayLibrary(config);
            break;

        case 'B':
            //std::cout << "You Chose 'B'!" << '\n';
            break;

        case 'C':
            addAlbum(config);
            break;

        // Don't needed since artist is added with album...
//        case 'D':
//            std::cout << "You Chose 'D'!" << '\n';
//            break;

        case 'X':
            std::cout << "Thank you for playing\nGoodbye!\n";
            break;

        default:
            std::cout << "Choice was not A, B, C, or X \nPlease try again...\n";
        }
    } while(choice != 'X' && choice != 'x');

    return 0;
}


char displayMenu()
{

    std::cout << "Enter the letter for the menu option you want:"
                 "\nA.  Display Library"
                 "\nB.  Search Library"
                 "\nC.  Add Album"
    // No longer needed, since we're adding artist along together with album:
    //           "\nD.  Add Artist"
                 "\nX.  Quit Program\n";

    char menuChoice;
    std::cin >> menuChoice;

    std::cin.ignore(256, '\n'); // ignore up to 256 "Extra" characters or until "\n"
    //std::cin.ignore();
    return menuChoice;
}


void displayLibrary(Config &c)
{
    // Read in albums:
    /* int albumID;
        std::string albumName;
        int releaseDate;
    */
    std::ifstream inputFile(c.albumFileName);
    Album albums[c.albumCount];
    if (!inputFile)
    {
        std::cout << "Error opening album file.\n";
    }
    else
    {
        for (int i = 0; i < c.albumCount; ++i)
        {
            std::string temp;
            getline(inputFile, temp);
            albums[i].albumID = std::stoi(temp);
            getline(inputFile, albums[i].albumName);
            getline(inputFile, temp);
            albums[i].releaseDate = std::stoi(temp);
            getline(inputFile, temp);
            albums[i].numArtists = std::stoi(temp);
        }
    }
    inputFile.close();

    // Read in artists:
    /*	int artistID;
        std::string artistName; */
    inputFile.open(c.artistFileName);
    Artist artists[c.artistCount];
    if (!inputFile)
    {
        std::cout << "Error opening artist file.\n";
    }
    else
    {
        for (int i = 0; i < c.artistCount; i++)
        {
            std::string temp;
            getline(inputFile, temp);
            artists[i].artistID = std::stoi(temp);
            getline(inputFile, temp);
            artists[i].artistName = temp;

        }
    }
    inputFile.close();

    inputFile.open(c.genreFileName);
    Genre genre[c.genreCount];
    if(!inputFile)
    {
        std::cout << "Error opening genre file.\n";
    }
    else
    {
        for(int i = 0; i < c.genreCount; i++)
        {
            std::string temp;
            getline(inputFile, temp);
            genre[i].genreID = std::stoi(temp);
            getline(inputFile, genre[i].genre);
        }
    }
    inputFile.close();

    // Read in b_a_Intercepts:
    /*	int albumID;
        int artistID; */
    inputFile.open(c.ABIFileName);
    A_B_Int abint[c.B_A_IntCount];
    if (!inputFile)
    {
        std::cout << "Error opening album-artist intercept file.\n";
    }
    else
    {
        for (int i = 0; i < c.B_A_IntCount; i++)
        {
            std::string temp;
            getline(inputFile, temp);
            abint[i].albumID = stoi(temp);
            getline(inputFile, temp);
            abint[i].artistID = stoi(temp);
            getline(inputFile,temp);
            abint[i].genreID = stoi(temp);
        }
    }
    inputFile.close();

    // Display library
    std::cout << std::setw(15) << std::left << "Album Name" << "Artist(s)\n";

    int artistDisplayed = 0;
    int genresDisplayed = 0;
    for (int i = 0; i < c.albumCount; i++)
    {
        std::cout << std::setw(15) << std::left << albums[i].albumName
                  << std::setw(5) << albums[i].releaseDate;

        for (int j = 0; j < c.B_A_IntCount; ++j)
        {
            if(abint[j].albumID == albums[i].albumID)
            {
                //artistID = abint[j].artistID;
                std::cout << artists[abint[j].artistID].artistName;
                ++artistDisplayed;
                if (genresDisplayed <= albums[i].numArtists)
                {
                    std::cout << ", ";
                }
                else {
                    j = c.B_A_IntCount;
                }
            }
        }
        std::cout << '\n';
    }
}


void addAlbum(Config &c)
{
    int genreID;

    std::cout << "Please enter the album's name: " ;
    std::string albumName;
    getline(std::cin, albumName);

    std::cout << "Please enter the album ID (current autonumber is: "
              << (c.albumCount + 1) << "): ";
    int albumID;
    std::cin >> albumID;

    std::cout << "Please enter the album's release date (YYYY): ";
    int releaseDate;
    std::cin >> releaseDate;

    std::cout << "Please enter the album's genre: ";
    std::cin.ignore(256, '\n');
    std::string genre;
    getline(std::cin, genre);

    std::cout << "How many artists are on the album (minumum 1, if unknown)? ";
    int numArtists;
    std::cin >> numArtists;

    std::ofstream outputFile(c.albumFileName, std::ios_base::app);
    outputFile << albumID << '\n'
               << albumName << '\n'
               << releaseDate << '\n'
               << numArtists << '\n';
    outputFile.close();

    ++c.albumCount;
    outputFile.open(c.genreFileName, std::ios_base::app);
    outputFile << c.artistCount << '\n'
               << genre << '\n';
    outputFile.close();
    ++c.genreCount;

    std::cin.ignore(256, '\n');
    for (int authCount = 0; authCount < numArtists; ++authCount)
    {
        std::cout << "Enter the artist's name: ";
        std::string artistName;
        getline(std::cin, artistName);

        //Add artist to artist table
        outputFile.open(c.artistFileName, std::ios_base::app);
        outputFile << c.artistCount << '\n'
                   << artistName << '\n';
        outputFile.close();

        //Add the artist/album interception
        outputFile.open(c.ABIFileName, std::ios_base::app);
        outputFile << albumID << '\n'
                   << c.artistCount << '\n';
        outputFile.close();

        ++c.B_A_IntCount;
        ++c.artistCount;
    }

    // Update Config File:
    outputFile.open(c.configFileName);
    outputFile << c.albumCount << '\n'
               << c.artistCount << '\n'
               << c.B_A_IntCount << '\n'
               << c.genreCount << '\n';
}

Okay, now I see things in a different way. I didn't know certain things and those tips are good tips. I did try your code but it still doesn't display the genre and what I did next was tried to debug it. What came up was a Segmentation Fault at Genre genres[c.genreCount]; if I'm understanding correctly.
Enoizat wrote:
code ... deals with some minor issues, but not with the biggest ones
novice12 wrote:
I did try your code but it still doesn't display the genre

Indeed it does not.


Segmentation Fault at Genre genres[c.genreCount];

‘genreCount’ could be zero or negative. Display its value before initializing your array or trace it in the debugger.

Provide examples of your data to get better help.
So for my data output i get this for the genre

1
album
1999
1
2
album2
2000
1

This is for the album file.

1
album
1999
1
2
album2
2000
1

Then at the ABIfile im getting this

1
5126400
0
2
5126400
1

Topic archived. No new replies allowed.