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 sorry for any confusion; not knowing much about c++ and not having english as a primary language creates more than a couple problems.
Edited to include code - just skeleton program to work from.
Code:
#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;
}