Issues with searching for a keyword in a file

Hi!

I am having issues with my search function to find a song for a play list maker I am creating. I have looked at the tutorials but I do not think I understand it.

Any help at all would be appreciated!

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 void Keyword() 
{
	Playlist.open("Playlist.txt", ios::in);
	string stream;
	string line;
	string songName;
	bool found = true;

	cout << "Please enter in the name of a song.";
	cin >> songName;
	while (getline(Playlist , line))
	{
		found = true;
		if(found = (line.find(songName)))
		{
		cout << songName << "Has been found: \n";
		}
		
	}
cout << songName << " not found" << endl;
}
line 14 - if you're checking for equality, use == instead of the assignment operator =
I am afraid that did not do it, but I think I am messing up on line 14 somewhere.

It is skipping to line 20 and says my name is not found.
Something like this perhaps
1
2
3
4
    if (line.find(songName) != string::npos)
    {
        cout << songName << "Has been found: \n";
    }


see return value of string::find
http://www.cplusplus.com/reference/string/string/find/
I think I am on to something. When I type something in, I still do not get any results. But I am also thinking that I am needing to include the song length as well sense the getline is trying to find the exact match of my search.
Is this correct?

IE

Enter song name: Ryan
Enter song Length: 2

I am thinking that I need to include the 2 as well in my search? Do I understand correctly?

Please give an example of the contents of "Playlist.txt" which you are trying to match that name "Ryan".
Sure thing!


Ryan
Will


Sorry for the confusion, I am just using simple names so I can get the search right.

EDIT:
If you need the whole code:

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

#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
#include <fstream>


using namespace std;
fstream Playlist("Playlist.txt", ios::out | ios::in);


/* musicTimeVerify(double songTime)
{
if (songTime <= 0)
{
cout << "Incorrect Time format. Please try again!";
}
else
return 1;
}
*/
void Songplaylist(double songTime, int numSong, string musicName)
{
	cout << "How many songs are you entering?: ";
	cin >> numSong;
	Playlist.open("Playlist.txt", ios::out);
	for (int i = 1; i <= numSong; i++)
	{
		cout << "Please enter your music name and THEN the song length in minutes" << endl;
		cout << "Song name (Enter an _ for spaces):";
		cin >> musicName;
		Playlist<< musicName;
		Playlist << endl;
		/*cout << "Song Length (in minutes please): ";
		cin >> songTime;

		Playlist << " : " << songTime << endl;
		//	musicTimeVerify(songTime);
		*/
	}
	cout << "Now closing the playlist..." << endl;
	Playlist.close();
}
void SaveData()
{
	string line;
	ifstream Playlist("Playlist.txt");

	if (Playlist.is_open())
	{
		while (getline(Playlist, line))
		{
			cout << line << endl;
		}
	}
	cout << "Reached the end of File" << endl;
	Playlist.close();
}

void Keyword() 
{
	Playlist.open("Playlist.txt", ios::in);
	string stream;
	string line;
	string songName;
	bool found = true;

	cout << "Please enter in the name of a song.";
	cin >> songName;
	if (line.find(songName) != string::npos)
	{
		cout << songName << "Has been found: \n";
	}
cout << songName << " not found" << endl;
}

int main()
{
	char choice = 'Y';
	int numSong = 0;
	string token;
	string musicName;
	double songTime = 0;
	int selection = 0;
	bool cont = true;
	cout << "_______________________________________________________________________________" << endl;
	cout << "Welcome to the Music Playlist program. \nPlease enter in a number for the following:" << endl;
	cout << "1.View playlist" << endl;
	cout << "2.Search for a Song" << endl;
	cout << "3.Delete playlist" << endl;
	cout << "4.Create a new playlist" << endl;
	cout << "5.Exit the program" << endl;
	cout << "_______________________________________________________________________________" << endl;
	while (cont == true)
	{
		cout << "Selection: ";
		cin >> selection;
		switch (selection)
		{
		case(1):
			SaveData();
			break;
		case(2):
			Keyword();
			break;
		case(3):
			Playlist.close();
			if (remove("Playlist.txt") != 0)
				perror("Error in deleting File...");
			else if (cout << "Successful Deletion of the Playlist");
			cout << endl;
			system("pause");
			break;
		case(4):
			cout << "A new playlist has been created." << endl;
			cout << "Would you like to edit the playlist now? Y/N:";
			cin >> choice;
			if (choice == 'y' || choice == 'Y')
			{
				cout << "You are now editing your new playlist." << endl;
				Songplaylist(songTime, numSong, musicName);
			}
			else
				break;
		case(5):
			cont = false;
			cout << "\nGood bye!\n";
			system("pause");
			break;
		}
	}
}
Last edited on
I think the file was not opened properly. Rather than using a global variable. I used a local ifstream inside the function:
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
void Keyword() 
{
    ifstream Playlist("Playlist.txt");
    if (!Playlist)
    {
        cout << "Playlist file not open\n";
    }
    
    string line;
    string songName;

    cout << "Please enter in the name of a song.";
    cin >> songName;
    bool found = false;
    while (getline(Playlist, line))
    {
        if (line.find(songName) != string::npos)
        {
            found = true;
            cout << songName << "Has been found: \n";
        }
    }
    
    if (!found)
        cout << songName << " not found" << endl;
        
}
Ah, I did not think about the file being opened improperly, I thought I had done so in Line 3 of my first post.

Thanks for the advice!

Marking as solved.
Ah, I did not think about the file being opened improperly, I thought I had done so in Line 3 of my first post.

When you posted the full code, at line 10 you had this:
 
fstream Playlist("Playlist.txt", ios::out | ios::in);

which would open the file for input/output at the start. Since that fstream is already open, trying to open it again in function Keyword() is not a good idea.

Though using a global constant for the file name might be a good idea
 
const std::string filename = "Playlist.txt";
so it can be used throughout the program, having global variables (such as the fstream Playlist) is not a good idea, because one is never certain in what state some other part of the program might have left it. Having uncertainty in a program design is not a good idea, so use local variables instead.
Topic archived. No new replies allowed.