Yuk. You're making two passes thru the code when one is enough.
Seeing as you still have work to do, I'll help you over this hump, and you can finish it be doing the proper reporting of stats at the end.
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
int const wordLength = 21;
int const Num = 100;
int const fileSize = 255;
struct words
{
char word[wordLength];
int count;
};
int storeFile( char [], words []);
//void wordSearchSetup(char[], int, words[]);
int wordSearch(char word[], int array_size, words * array);
int main ()
{
int count;
char fileName[fileSize];
count = 0 ;
cout << "Please enter the name of the file you wish to open: "<< endl;
cin.getline(fileName,fileSize);
if (!cin.good() ) {
cout << "Error reading cin..." << endl ;
return -1 ;
}
words array[Num];
count = storeFile(fileName, array);
cin.ignore();
}
int storeFile (char fileName[], words array[] )
{
//int count = 0;
int i = 0;
ifstream inFile;
char line [Num];
int array_size = 0 ; // Count of unique words in array
inFile.open(fileName);
while (inFile.getline(line,Num))
{
array_size = wordSearch(line, array_size, array);
//strncpy(array[i].word, line, wordLength);
//count++;
i++; // Words read
}
inFile.close();
//wordSearchSetup( fileName, count, array);
return count;
}
/*
void wordSearchSetup(char fileName[], int count, words array[])
{
char line[Num];
//int i = 0;
int size;
ifstream inFile;
inFile.open(fileName);
while (inFile.getline(line,Num))
size = wordSearch(line, count, array);
cout << size << endl;
}
*/
int wordSearch( char line[], int array_size, words * array)
{
int i = array_size ; // Start search at the end of the array
while (i && array_size > 0) // Don't do is there are no elements in array
{
if (strcmp(line, array[i-1].word) == 0) // do the words match?
{
array[i-1].count++; // Yes, bump the count and return
return array_size ; // Array size did not change
}
i-- ; // index backwards into array
}
// If we got here, we found a unique word. Add it at the end and return
strcpy(array[array_size].word, line) ;
array[array_size].count = 1 ;
return array_size+1;
}