I am trying to read a file that contains words. I have set up a class which acts like a vector and adds, removes..etc.The idea is to read in the words from a file and store each word in the vector (array).
I have it working in the main function but now would like to clean up the code by creating specific functions. First one is the ReadFile. The object allWords is empty after the call to the ReadFile function. I have an idea it is because allWords and Words (ReadFile) are really to different objects. How do I setup as a pass by reference?
Also if you see anything I could do something better in please say something
Idea for this assignment is to read in words in a file with delimiters and provides stats. Need to create own vector.
I have tried:
void ReadFile(StringVector *Word, int *wordCnt, int *noOfChars);
and
return StringVector ReadFile(StringVector Word, int *wordCnt, int *noOfChars);
#include <iostream>
#include <fstream>
#include <string>
#include "StringVector.h"
usingnamespace std; /** \namespace std */
void ReadFile(StringVector Word, int *wordCnt, int *noOfChars);
bool LegalChars(char ch);
char ToLowerCase(char ch);
int main()
{
StringVector allWord, singleLetterWord, smallWord, bigWord;
string tempWord;
int wordCount = 0; /**< stores the number of words read in from the file */
int average = 0;
int numChars = 0;
constint SINGLE_COLUMNS = 10;
constint WORD_COLUMNS = 5;
// === START read in a file
ReadFile(allWord, &wordCount, &numChars);
...
}
void ReadFile(StringVector Word, int *wordCnt, int *noOfChars)
{
char tempChar;
string tempWord;
ifstream infile( "input4.txt" ); /**< open file */
while (! infile.eof() ) /**< Continue till end of file */
{
infile.get(tempChar); /**< reads in char at a time */
if (LegalChars(tempChar)) /**< if an alphanumeric then save */
tempWord = tempWord + ToLowerCase(tempChar); /**< append char to existing word */
elseif (! LegalChars(tempChar) && (tempWord.length() > 0)) /**< if a delimiter char and a word */
{ /**< has been stored */
wordCnt++; /**< increment word count */
if (Word.Find(tempWord) == -1) /**< search if word already been stored */
Word.PushBack(tempWord);
/* if word is not single letter then record number
not characters */
if (tempWord.length() > 1)
noOfChars += tempWord.length();
tempWord.erase(); /**< clear string */
}
}
infile.close(); /**< close file */
}