How do you do that? This code reads in the book. It reads in fine. Now I just have to alter it so it doesn't read in repeat words in the book. How would I do that?

void readInStory(){
    ifstream inStream; // declare an input stream for my use
    int wordRow = 0; // Row for the current word

    allocateArrayStory(analysis);

    inStream.open( "TheSecretAgentByJosephConrad.txt");
    assert(! inStream.fail() ); // make sure file open was OK
    // Keep repeating while input from the file yields a word
    while (wordRow < MaxNumberOfWords && inStream >> analysis[wordRow]){
        for (int i = 0; i < strlen(analysis[wordRow]); i++)
            analysis[wordRow][i] = tolower(analysis[wordRow][i]);
        analysis[wordRow] = (analysis[wordRow]);
    wordRow++;
    }
}

Recommended Answers

All 2 Replies

for each word that is read in check it with the list of words you have already read in.

Now I just have to alter it so it doesn't read in repeat words in the book.

The simplest way would be to use a std::set<> or std::unordered_set<> to filter out the non-uniqe words.

#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <vector>
#include <iterator>

// http://en.cppreference.com/w/cpp/container/vector
std::vector< std::string > unique_words( const char* path2file )
{
    std::ifstream file( path2file ) ;

    // http://en.cppreference.com/w/cpp/iterator/istream_iterator
    std::istream_iterator< std::string > begin(file), end ;

    // http://en.cppreference.com/w/cpp/container/set
    std::set< std::string > unique( begin, end ) ;

    return std::vector< std::string >( unique.begin(), unique.end() ) ;
}

Note: This snippet is merely illustrative; it does not take care of punctuation in the text.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.