I have to create a program that reads a file with song lyrics and allows user to search for songs containing certain words.
- Reads the file lyrics.dat
- Needs to ask user for a word then display all songs containing the word along with title and artist
My problem is that my output display is only showing the first 2 song artists rather than reading out the 100; I need to be able to read in all 100 songs before creating a search function that will add the word into an array of unique words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
while (true)
{
inFile >> word;
if (!inFile) // stop loop at EOF
break;
if (word == "artist")
{
inFile.ignore(8,' '); // ignore space between "artist" and name
getline(inFile,artist); // read the artist
// copy artist into array
artists[nSongs] = artist;
}
}
Sorry I didn't include the entirety of that code because I am using else if for artist, title, and lyrics. After the final else if (but before the else error condition) I am using nSongs++;
Certainly, didn't want to post all of it and make in unmanageable to navigate. I should make it clear that in the file I use the word artist, title, startlyric and endlyric to delineate it for the program.
while (true) {
inFile >> word;
if (!inFile) // stop loop at EOF
break;
if (word == "artist")
{
inFile.ignore(8,' '); // ignore space between "artist" and name
getline(inFile,artist); // read the artist
// copy artist into array
artists[nSongs] = artist;
}
elseif (word == "title")
{
inFile.ignore(1,' ');
getline(inFile,title);
//title[nSongs] = title;
}
elseif (word == "startlyrics") {
// get lyrics for song
// all done
nSongs++;
} else {
// error condition
}
}
After looking at your replies I realize I made it a little more complicated that I needed to, so I just used a simple do-while loop until I get the full program function and then I will tweak as necessary. Originally though it would just display
0 Title Name1
1 Title Name2
return 0x0.
Thanks for the help though, I really appreciate it -- I am still trying to work my way through c++ as I do not have a whole lot of experience with it.