Trying to add search



I'm trying to add search to my console program but I can't seem to find a way to do it right. I've tried it several ways. I was wondering if anyone could please help me?

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
  #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 Artist
{
	public:
		int artistID;
		string artistName;
		Artist()
		{
			artistID = 0;
			artistName = "und";
		}
};

struct A_B_Int
{
	int albumID;
	int artistID;

	A_B_Int()
	{
		albumID = 0;
		artistID = 0;
		
	}
	
};

class Config
{
	public:
		string albumFileName;
		string artistFileName;
		string ABIFileName;
		string genreFileName;
		string configFileName;
		string temp;
		
		ifstream inputFile;
		
		int albumCount, artistCount, B_A_IntCount;
		
		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);
					
					
				}
				else
				{
					cout << "Error opening file.  Creating a new configuration... " << endl;
					albumCount = 0;
					artistCount = 0;
					B_A_IntCount = 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

#include <iostream>
#include <fstream>
#include <iomanip>
#include "library.h"
using namespace std;

//Function prototypes
char displayMenu();  
void displayLibrary(Config &c);
void addAlbum(Config &c);
void searchLibrary(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;
	
	
	
	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;
	
	Album albums[c.albumCount];
	Artist artists[c.artistCount];
	A_B_Int abint[c.B_A_IntCount];
	
	// 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.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);
			
			
		}
	}
	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 (artistDisplayed <= albums[i].numArtists)
				{
					cout << ", ";
				}
				else
					j = c.B_A_IntCount;
				
			}
		}
		cout << endl;
		
		
	}
}

void addAlbum(Config &c)
{
	ofstream outputFile;
	
	string albumName, artistName;
	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;
	cin.ignore(256,10);
	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 <<c.artistCount<<endl;
	outputFile.close();
		
	
	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.close();
	
}
void searchLibrary(Cnfig &c)
{

Hello novice12,

You should compile your program before posting it. There are many errors that need to be addressed first.

In the header file you have:
1
2
3
4
5
#include <fstream>
#include <iostream>
#include <string>

using namespace std;  // <--- Best not to use. And never in a header file. 

These header files should not be needed here and "string" should be in "main". As for the last line take time to read this: http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

In place of #pragma once a header guard would be better as not everyone can use this line.
1
2
3
4
5
6
#ifndef LIBARY_H
#define LIBARY_H

//Code here.

#endif // !LIBARY_H 


The function "searchLibrary" has a misspelled word and is missing the closing brace }.

The program uses an input file. please post it or at least a fair sample so everyone is using the same information. It helps with understanding how the file is read too.

In the class"Config" you have made all the variables "public". This defeats the point of the class. You might as well use a struct. Actually when I look back at all the classes they are the same with all the variables as "public". Either reread your book or have a look at: http://www.cplusplus.com/doc/tutorial/classes/

As I was looking over the program I noticed functions like:
1
2
3
4
displayMenu();
displayLibrary(Config &c);
addAlbum(Config &c);
searchLibrary(Config &c);

These are regular functions, but should be class functions. It would make it better when dealing with "private" class variables.

I need some more time to understand what you are doing with the input and output files. I have the feeling that what you are doiing could get very confusing.

BTW if you are going to use "std::toupper()" include the header file "<cctype>".

Hope that helps,

Andy
Topic archived. No new replies allowed.