Thread: Lyric search program - search function question

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    5

    Lyric search program - search function question

    I am working on creating a program that reads a file with a predetermined number of artists(100), titles(100), and unique lyric words(10000). I am working on a function that will search through words currently in the list and if the word is not currently in the list it would add it to the next available array location and then return a location in which in the word was found or inserted. My problem, however, is I do not really know how to go about starting this. Most of the online references I see recommend using something like:

    Code:
    {
    //Loop through the size of the string
    for(int i=0;i< str.length();i++)
     {
     //If the character is not a space
     if(str[i] != ' ')
     {
    //Reset the value of the array position to the newupper case letter str[i] = toupper(str[i]);
     }
     }
     return str;
    }
    But, I am a little confused about how to implement this for initially creating the list from 10,000 unique words in the data file. I am not looking for the code verbatim, but any tips/help would be greatly appreciated.
    Last edited by Jpaul8986; 07-28-2015 at 09:50 AM.

  2. #2
    Guest
    Guest
    Just to clarify:

    1. You have a list of 10K words in memory that you read in from a file?
    2. You want to be able to give your program a word and check if it's among those 10K in the list. If it isn't, it should be added to that list?

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    5
    1) I have a list of more than 10,000 words in a data file that I opened and read
    2) No, I wasn't sure if there was a way to have the function scan through the lyric list from the data file and assign each unique word it comes across to a blank element in the array utilizing "startlyric" and "endlyric" qualifies in the data file to denote start and end of separate songs. There are 10,000 unique words in the lyrics total, but the actual word count is somewhere around 52,000.

  4. #4
    Guest
    Guest
    Sorry, I'm still having trouble understanding the purpose of the program, what data you start with, and what data you want to compute as a result. If you could describe it in a more general sense, I think it would help others (and me) to get a better idea.

    (...) if the word is not currently in the list it would add it to the next available array location and then return a location in which in the word was found or inserted.
    E.g. where does the word come from, what array are we talking about and what determines if a location is the next available. I take it you want to store word positions (in a larger string), not the words themselves.
    Last edited by Guest; 07-28-2015 at 10:44 AM.

  5. #5
    Registered User
    Join Date
    Jul 2015
    Posts
    5
    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;
    }
    
    Last edited by Jpaul8986; 07-28-2015 at 10:57 AM. Reason: Included code

  6. #6
    Guest
    Guest
    Yes, it seems like a very challenging problem for someone new to the language. May I ask whether this is a school assignment or something you do primarily for yourself? C++ offers tools that make this problem easier to dissect, but I cannot suggest these to you, if you're not allowed to use them in your class (yet).

    I need to create a function int search(char word[], bool add)
    - It needs to search through words currently in list
    So the list is potentially 10,000 words long, but starts empty?

    If the word is not in the list and bool add is true, the word should be added to the end
    The end being one element after the last word added so far? So that it would start at 0, the become 1, 2, 3, with each added words?

    The function then returns a location in which the words were found or inserted
    To determine inside which song they were found, or with what intention?

    Edit: I haven't checked out the added code yet, will do so later!

  7. #7
    Registered User
    Join Date
    Jul 2015
    Posts
    5
    I am allowed to use "whatever tools or assistance I can find". It is for school (the professor lectures straight out of the book and offers no application examples or assistance developing code), but as I develop a better proficiency in c++ I would like to adapt it to a larger program idea I am working on personally. The list does start empty, but the list of words to sort through is 50,000+ and has to narrow it down into <= 10,000 unique words. So it would start off with say "the" being lyric[0], next word "a" being lyric[1], and continuing down the line. Yes, the intent is to return all song titles(or only one if applicable as I am using songs with lyrics in different languages - it is part of the parameter of the project) in which a keyword or keywords could be found.
    Last edited by Jpaul8986; 07-28-2015 at 11:25 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jpaul8986
    I am allowed to use "whatever tools or assistance I can find". It is for school (the professor lectures straight out of the book and offers no application examples or assistance developing code), but as I develop a better proficiency in c++ I would like to adapt it to a larger program idea I am working on personally.
    I suggest a detour: read Josuttis' The C++ Standard Library - A Tutorial and Reference, 2nd Edition. You will discover that the C++ standard library provides container classes that could be used here, e.g., sets and maps. Among other things, it also provides generic algorithms for things like searching and sorting.

    Going beyond this, at some point you might want persistent storage for the data. You could embark on another detour: learn about relational databases and SQL, then incorporate SQLite into your program, either using the C interface or a C++ wrapper.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Guest
    Guest
    Yes, what laserlight said. The only value of fiddling with arrays as you do is to exercise logical thinking (you teacher wants to torture you), but it's not good C++.

    There are containers and other facilities that would make your life much easier.
    Code:
    #include <set>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main() // placing void in parentheses is exceedingly uncommon in C++ btw ;)
    {
        vector<string> words1; // this container could flexibly hold new words
        set<string> words2; // this smart container only adds words it doesn't already contain
    }
    I cannot give you a run down on how to use these, but it's not that big a leap from raw arrays and avoids many of their headaches.

  10. #10
    Registered User
    Join Date
    Jul 2015
    Posts
    5
    Thanks, I will check it out. I will leave the SQL exploration until I have a firmer understanding of C++, but I do appreciate the advice! Unfortunately I don't have the freedom to choose whether I use an array or not. As for the void, I am just using what (little) advice the teacher has given for code. Sorry if that is inappropriate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Advanced Search -> Search Multiple Content Types
    By phantomotap in forum Tech Board
    Replies: 2
    Last Post: 05-21-2011, 07:28 AM
  3. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  4. Replies: 12
    Last Post: 01-28-2006, 07:40 AM
  5. Allowing my search function to search sub directories!
    By Queatrix in forum Windows Programming
    Replies: 10
    Last Post: 09-30-2005, 04:54 PM