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.
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?
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;
}
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.