Passing Object by Reference

Hi,

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);



Here is the snippet of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
#include "StringVector.h"

using namespace 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;
	const int SINGLE_COLUMNS = 10;
	const int 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 */
		else if (! 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 */
}
void ReadFile(StringVector& Word, int *wordCnt, int *noOfChars)
hahaha... how easy was that. Just one character in the right spot. So close yet so far.

It worked and thank you.

Not sure what I was on when I did the

return StringVector ReadFile(StringVector Word, int *wordCnt, int *noOfChars);
Last edited on
Topic archived. No new replies allowed.