Distinct word and count from file input in C++
<p>This is the requirement: whenever a new word is encountered, the program should allocate an instance of the node from dynamic memory to contain the word and its count and insert it into a linked list so that the list is always sorted. If the word encountered already exists in the list, then the count for that word should be incremented.</p>

<p>I did search everywhere and the proper solution is using std::map, but I dont want to use that because I haven't learnt that so far. Is it alright to use List or Vector and create a struct or class to manipulate each node ?</p>

<p>This is my proper code</p>

<pre><code>class Node {
string word;
int count;

public:
    Node() {
        word = "";
        count = 1;
    }
    Node(const Node &amp;other) : word(other.word), count(other.count) {
        // copy constructor 
    }
    ~Node() {} // Destructor

    void printWord() const {
        cout &lt;&lt; count &lt;&lt; " " &lt;&lt; word &lt;&lt; endl;
    }
    void loadWord(ifstream &amp;fin) { 
        fin &gt;&gt; word;
    }
    void setWord(const string &amp;word) {
        this-&gt;word = word;
    }
    const string&amp; getWord() const {
        return word;
    }
    void incrementCount() {
        count++;
    }
};

void load(list&lt;Node&gt; &amp;nodes, const char *file);
void print(const list&lt;Node&gt; &amp;nodes);
bool isExist(const list&lt;Node&gt; &amp;nodes, const string &amp;word, Node &amp;node);
void error(const string &amp;message, const char *file);
const Node&amp; getNode(const list&lt;Node&gt; &amp;nodes, const string &amp;word);

int main(int argc, char *argv[]) {

    list&lt;Node&gt; nodes;

    if (argc != 2) {
        cout &lt;&lt; "Error syntax : require an input file\n";
        return 0;
    }
    load(nodes, argv[1]);
    print(nodes);

    return 0;   
}

void print(const list&lt;Node&gt; &amp;nodes) {

    list&lt;Node&gt;::const_iterator itr;

    for (itr = nodes.begin(); itr != nodes.end(); itr++) {
        itr-&gt;printWord();
    }
cout &lt;&lt; '\n';
}

void load(list&lt;Node&gt; &amp;nodes, const char *file) { 

    ifstream fin;
    Node node;
    string temp;

fin.open(file);

if (!fin) 
    error("Cannot open file ", file); // exit

while (!fin.eof()) {
    if (fin.good()) {
        fin &gt;&gt; temp;
        if (!isExist(nodes, temp, node)) {
            node.setWord(temp);
            nodes.push_back(node);
        } else {
            // increase word count here
        }

    } else if (!fin.eof()) 
        error("Unable to read data from ", file);
}
fin.close();
}

bool isExist(const list&lt;Node&gt; &amp;nodes, const string &amp;word, Node &amp;node) {
list&lt;Node&gt;::const_iterator itr;
for (itr = nodes.begin(); itr != nodes.end(); itr++) {
    if(word.compare(itr-&gt;getWord()) == 0) {
        return true;
    }
}
return false;
}

const Node&amp; getNode(const list&lt;Node&gt; &amp;nodes, const string &amp;word) {
    list&lt;Node&gt;::const_iterator itr;
    for (itr = nodes.begin(); itr != nodes.end(); itr++) {
        if(word.compare(itr-&gt;getWord()) == 0) {
            return *itr;
        }
    }
    return NULL; // This is fail what should I do to return a NULL value when not found
}

void error(const string &amp;message, const char *file) {
cerr &lt;&lt; message &lt;&lt; file &lt;&lt; '\n';
exit(0);
}
</code></pre>

<p>The code doesn't work, I just tried to generate my solution to solve the question by applying my Java knowledge but it seems diffirent to control object in c++. Could someone examine my code and suggest me a better way ? </p>

<p>Thanks.</p>
