Hi I need help!! My program is supposed to count word occurences from a text file and the output is supposed to look like this

a 2
count 1
hello 1
test 2
this 1
words 1

but instead it does this :

a 2
a 2
count 1
hello 1
test 2
test 2
this 1
words 1


s there a way to take the second "a" out of the output file without messing up the count of the other words?
I placed all of the words into 2 separate arrays and then compared them. Here is my code.

/* word occurences

*/

#include <iostream>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <fstream>



using namespace std;
struct wordCount
{		
     string word;		
     int count;	
}; 
class wordFrequency
{
    public:
        //wordFrequency();
        int countFile(int counter);
        void openFile(ifstream &inFile, ofstream &outFile);
        void periods(string arr[], int n);
        void bubbleSort(string arr[], int n);    
        
    private:
        string wordsM[];
        //struct wordFreq[];
};

int main()
{
    wordFrequency word;
    //Declare variables
    int length;
    int counter;
    
    
    length = word.countFile(counter);
    string wordsM[length];
    struct wordCount wordFreq[length]; //make array of structs
    
    //Declare stream variables
    ifstream inFile;
    ofstream outFile;
    
    
    word.openFile(inFile, outFile);
    
    
    
    while (!inFile.eof()) //while not at end of file
    {
          
          for(int i=0; i < length; i++) 
          {
                    inFile >> wordsM[i];  //read words from file into array
                    word.periods(wordsM, length);
          
          
          wordFreq[i].word = wordsM[i]; //place words from file into 2nd array
          wordFreq[i].count = 0;
          }
          

    }
    word.bubbleSort(wordsM , length); //function to alphabetize words
    for (int i = 0; i < length;i++)
    {
        for (int j = 0; j < length; j++)
        {
            if (wordsM[i] == wordFreq[j].word) 
            {
               //compare the words in the 2 arrays, if there is a match 
               //increment count for that word
               wordFreq[i].count++;
            }
        
        }
        
        outFile << setw(15) << wordsM[i] << setw(4) << wordFreq[i].count <<endl;
    }
    
    inFile.close();
    outFile.close();
    system ("Pause");
    return 0;
}
int wordFrequency::countFile(int counter)
{
    ifstream inFile;
    int counts = 0;
    string str;


    inFile.open("Words.txt");
    while (!inFile.eof())
    {
          inFile >> str;
          counts++;
    }
    
     return counts;
}
void wordFrequency::openFile(ifstream &inFile, ofstream &outFile)
{
    //open input file
    inFile.open("Words.txt");
    
    if (!inFile)
    {
                cout << "Cannot open input file. Program terminates!" << endl;
    }
    
    //open output file
    outFile.open("WordsOutput.txt"); 
    
    
    outFile << left << setw(15) << "WORD" << setw(4)
                    <<"OCCURRENCES\n\n"<< endl;
   
}
void wordFrequency::periods(string arr[], int n)
{
	int i;
	size_t found;
	for ( i = 0; i < n; i++)
	{
          // find periods and replace them with spaces so when we compare 
          //strings there will be no periods in the way
          found=arr[i].find('.');
          if (found!=string::npos)
          {
          arr[i].replace(arr[i].find('.'),1,"");
          }
           //find commas also
          found = arr[i].find(',');
          if (found!=string::npos)
          {
          arr[i].replace(arr[i].find(','),1,"");
          }
        
     }
} 
void wordFrequency::bubbleSort(string arr[], int n)
{
	int i, j;
	string temp;
	
	for ( i = 1; i < n; i++)
	{
        for (j = 0; j < n - i; j++)
        {
            if (arr[j] > arr[j+1])
            {
                       temp = arr[j];
                       arr[j] = arr[j+1];
                       arr[j+1] = temp;
            }
        }
     }
}

Here is how you can compare an element, against every other element.. while we are at it, we'll erase any matching words in order to get a good word count:

string temp;

//This array will contain single occurances of words from wordsM[ ]
string unique_words[length];

//This array will serve as a counter, and will directly corrospond to unique_words[ ]
int unique_counter[length];

//Array initialization (clean the trash out of the array)
for(int i=0; i<length; i++)
{
     unique_counter[i] = 0;
}

//Pick a word
for(int i=0; i<length; i++)
{
     temp = wordsM[i];

     //Compare it with every other word
     for(int j=0; j<length; j++)
     {
          if(temp.compare(wordsM[j]) && !wordsM[j].empty())
          {
               unique_words[i] = wordsM[j];
               wordsM[j].clear();
               unique_counter[i]++;
          }
     }
}

Now you have a counter that corrosponds to an array of individual words. I gave you a really good idea of how to get this done.. see if you can make it work in your code.

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.