Help on searching in a sequential access file

I'm just learning programming and I created a program to hold a CD collection. What I'm curious about is searching the file. I have a function to enter the artists and CD title, and a function to display the file, but what if I wanted to search the file by artist for a cd title? Any help would be 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
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//function prototypes
void saveCd();
void displayCds();

int main()
{
	int menuOption = 0;

	do //begin loop
	{
		//display menu and get option
		cout << endl;
		cout << "1 Enter CD information" << endl;
		cout << "2 Display CD information" << endl;
		cout << "3 End the program" << endl;
		cout << "Enter menu option: ";
		cin >> menuOption;
		cin.ignore(100, '\n');
		cout << endl;

		//call appropriate function
		//or display error message
		if (menuOption ==1)
			saveCd();
		else
			if (menuOption ==2) 
				displayCds();
		//end if
	//end if
	} while (menuOption != 3);

	return 0;
}	//end of main

//*****function definitions*****
void saveCd()
{
	//writes records to a sequential access file
	string cdName = "";
	string artistName = "";

	//create file object and open the file
	ofstream outFile;
	outFile.open("cds.txt", ios::app);

	//determine whether the file is opened
	if (outFile.is_open())
	{
		//get the CD name
		cout << "CD name (-1 to stop): ";
		getline(cin, cdName);
		while (cdName != "-1")
		{
			//get the artist's name
			cout << "Artists name: ";
			getline (cin, artistName);
			//write the record
			outFile << cdName << '#'
				<< artistName << endl;
			//get another CD name
			cout << "CD name (-1 to stop): ";
			getline(cin, cdName);
		}	//end while

		//close the file
		outFile.close();
		
	}
	else
		cout << "File could not be opened." << endl;
	//end if
}	//end of saveCd function

void displayCds()
{
	//displays the records stored in the cds.txt file
	string cdName = "";
	string artistName = "";

	//create file object and open the file
	ifstream inFile;
	inFile.open("cds.txt", ios::in);

	//determine whether the file was opened
	if (inFile.is_open())
	{
		//read a record
		getline(inFile, cdName, '#');
		getline(inFile, artistName);

		while (!inFile.eof())
		{
			//display the record
			cout << cdName << ", " <<
				artistName << endl;
			//read another record
			getline(inFile, cdName, '#');
			getline(inFile, artistName);
		}	//end while

		//close the file
		inFile.close();
	}
	else
		cout << "File could not be opened." << endl;
	//end if
}	//end of displayCds function 
You could base a search function on your displayCds() function. In your while loop, compare the artistName against the name entered by the user. When they match, display the contents.
Hi. I don't know if i'm allowed to do this. But I tried out this code. I'm also trying to learn to do search. I tried what kooth said but, it doesn't really work for me. It can only detect the first record but not the remaing few. Can someone tell me where I went wrong? Cos I also need to learn this for my assignment. Thanks!

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
void searchCD()
{
	string cdName = "";
	string artistName = "";
	string scdName = "";


	ifstream inFile;
	inFile.open("cds.txt", ios::in);


	if (inFile.is_open())
	{
		cout << "CD name: ";
		getline (cin, scdName);

		getline(inFile, cdName, '#');
		getline(inFile, artistName);

		while (scdName == cdName)
		{

			cout << cdName << ", " <<
				artistName << endl;

			getline(inFile, cdName, '#');
			getline(inFile, artistName);
		}

		//close the file
		inFile.close();
	}
	else
		cout << "File could not be opened." << endl;
	system("PAUSE");
}

Topic archived. No new replies allowed.