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
I am using a global variables artists, titles, and words as well as a global array bool inSong[100][10000] where 100 is number of songs and 10000 corresponds to 10000 unique words
- Needs to set inSong[s][w] to true if the word in position [w] is contained in the song [s] and false if the word is not in the song
I need to create a function int search(char word[], bool add)
- It needs to search through words currently in list
o If the word is not in the list and bool add is true, the word should be added to the end
o The function then returns a location in which the words were found or inserted
I am currently stuck on creating a global the global array and then setting up the function that would read lyric words (by using "startlyric" and "endlyric" qualifiers to denote beginning and end of the lyrics and input them into the array of 10000 unique words.
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
|
#include <iostream>
#include <fstream>
using namespace std;
string artists[100],titles[100];
int nSongs=0;
bool loadData(void) {
ifstream inFile;
string word,artist,title;
// must open file AND CHECK! before accessing data
inFile.open("lyrics.dat");
if (!inFile)
{
cout << "Cannot open lyrics.dat\n";
return false;
}
bool inSong[100][10000] //global array where 100 = song, 10000 = word
{
flag = false;
//set flag to true if the word in position inSong[word]
//of the word list is contained in song the song [s] and
//false if the word is not in the song
}
int search(char word[], bool add)
{
//search through words currently in list if word is not
//currently in, add word to next blank array space
//if word is in, return location in which the wordw was found
}
// do stuff here
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, bump the counter
nSongs++;
} else {
// error condition
}
}
// all done, close the file
inFile.close();
// indicate success
return true;
}
void searchKeys(void) {
}
int main(void) {
bool rc; // return code
// load data from lyrics file
rc = loadData();
cout << "Number of songs in file: " << nSongs << endl;
cout << "Artist list:\n";
for (int i=0;i<nSongs;i++)
cout << i << " " << artists[i] << endl;
// search for keys
if (rc)
searchKeys();
return 0;
}
|
This is just a skeleton program to work through, I am not looking for the code verbatim, but rather general tips and hints. I would prefer to figure it out on my own (if possible) and only resort to asking for specific code as a last resort.