Click here to Skip to main content
15,399,959 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Read the text file word by word.

  • Create a class called WordCounter that

    • Has 2 data members, e.g., string word, int count.
    • Has appropriate getters and setters
    • At least one constructor
    • Uses this keyword to help distinguish between data members and local variables
  • Create a collection of words in such a way that your program can distinguish between different words, e.g., store each different word in an object of WordCounter class.
  • Every time a word appears your program is required to count the occurrence of that word in the file.
  • Finally print a report with each word and number of times it occurred in the text file provided.
  • You can only use #include<iostream>, #include<fstream>, #include<string>, include<vector> libraries.

Your program should at least have the following methods:

  • bool isFound(vector<wordcounter> &v, string word) which takes a vector of WordCounter objects and a word to make a search of that word in that vector. If the word is found, return true else false.
  • void incrementWordCount(????) function that counts the occurrences of the word. It is your responsibility to find out which arguments should be provided to this function.
  • void printReport(vector<wordcounter> &v) which should simply have a cout statement that tells how many times a word has appeared in the file.

What I have tried:

Was able to do it without classes:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

bool isFound(vector<string> v, string word)

    for(int i=0; i<v.size(); i++){

        ="" if(v[i]="=" word){

            ="" return="" true;
            ="" }

    return="" false;

void="" readwordforword(vector<string="">& words, vector<int>& wordCount, ifstream& file) // helper function to read the file word by word
    string word;



        bool flag = isFound(words, word); // if word is in words
        if(!flag) // if word is not in words


            int index;
            for(index=0; index<words.size(); index++)
            ="" {
            ="" if(words[index]="=word)
            " break;
            ="" }
            ="" wordcount[index]++;


void printReport( vector<string> words, vector<int> counts)
    int total = 0;
    // printing the counts and words
    for (int i = 0; i< words.size(); i++)
        cout << words[i] << " :" << counts[i] << endl;
        total = total + counts[i];

int main()
    vector<string> words;
    vector<int> wordCount;
    ifstream inFile;"data.txt");

Updated 1-Feb-22 5:54am

1 solution

Here's the beginning of a class for you :
class WordCounter
   WordCounter( const char * w )
       : word( w )
       count = 0;

   std::string word;
   int count;
In your main function you can make an instance of a collection of these. You have used a vector previously and that's a good choice for this :
int main()
   std::vector< WordCounter > words;

Hopefully that's enough to get you started.
CPallini 1-Feb-22 2:04am
Why did you qualify 'public' then class variables?
Rick York 1-Feb-22 3:29am
Habit - I usually have separate specifiers for members and methods because usually members are protected or private.
CPallini 1-Feb-22 3:41am
That's OK, but why 'public', in this case?
I believe 'private' would have been a better choice (at least for 'word').
Rick York 1-Feb-22 11:17am
Yes, I agree and that's what I would have done normally. It seems this poster is a beginner and I didn't want to get into access control with them at this point.
merano99 1-Feb-22 18:41pm
But that would be part of the task anyway: "Has appropriate getters and setters"
CPallini 1-Feb-22 13:14pm
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900