Getting tokenized words into a 2D array

The assignment is to take words from a txt file, line by line (using getline), and tokenize each word, keeping count of the total number of words seen, the number of unique words, the count of each individual word, and the number of lines. I am having trouble getting the tokenized words into a 2D array.

From the instructions:
"You may assume the lines comprise words (contiguous lower-case letters [a-z]) separated by spaces, terminated
with a period. You may ignore the possibility of other punctuation marks, including possessives or contractions,
like in "Jim's house". Lines before the last one in the file will have a newline ('\n') after the period. In your data
files, omit the '\n' on the last line. You may assume that a line will be no longer than 100 characters, an
individual word will be no longer than 15 letters and there will be no more than 100 unique words in the file.
However, there may be more than a hundred total words in the file."

and

"Hints:

Use a 2-dimensional array of char, 100 rows by 16 columns (why not 15?), to hold the unique words, and a 1-
dimensional array of ints with 100 elements to hold the associated counts. For each word, scan through the
occupied lines in the array for a match (use strcmp()), and if you find a match, increment the associated count,
otherwise (you got past the last word), add the word to the table and set its count to 1."


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
  int main(int argc, char *argv[])
{	
	int lineCount=0, wordcount=0;
	ifstream inFile;
	char line[100];
//	  char inputfile[30] = argv[1];
	inFile.open("string1.txt");
	while(!inFile.eof())
	{	
		 numofLines(inFile, wordcount, lineCount, line);
		 cout << line<<endl;
		 totalwordcount(line, wordcount);		 	 
	}
	cout <<endl<< lineCount<<endl<<wordcount;
	inFile.close();
return 0;
}

void numofLines(ifstream &infile, int &wordcount, int &lineCount, char (&line)[100])
{   
	infile.getline(line,100);
	lineCount++;
}

void totalwordcount (char line[100], int &wordcount) 
{	
	char *token;
	char *p = token;
	token = strtok(line,DELIMS);
	//uniquewords(token);
		while(token != NULL)
		{	
			token = strtok(NULL, DELIMS);
			wordcount++;	 	 	 	 	 
		}	 	 

}
Topic archived. No new replies allowed.