Lyric search program question

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;

        }
   }
Where are you incrementing nSongs?
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++;
Well, mind reading costs extra: Please post all of your code and we'll try to help you.
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.

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
    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;

        }

        else if (word == "title")
        {
            inFile.ignore(1,' ');
            getline(inFile,title);
           //title[nSongs] = title;
        }

        else if (word == "startlyrics") {
            // get lyrics for song
            // all done
            nSongs++;
        } else {
            // error condition
        }
    }

Last edited on
What does your data look like?
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.
It seems you are getting a good grasp on C++. Keep on keepin' on!

Good luck!
Topic archived. No new replies allowed.