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


    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]);
5 Years
Discussion Span
Last Post by vijayan121

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.