No using string objects :/

I'm having trouble with this assignment that does not allow me to use string objects. So everything below is represented as arrays(c-strings). I was suppose to read lines from a file and figure out the number of unique words. MAX is 100 and I am suppose to assume each line in the file is not over 100 characters. This works for the first few times then stops in the middle. The problem seems to be in this loop, specifically, it may have to do with the getline() as it seems to stop there. If you want the whole program, ask.
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
        char line[MAX];
	char token[MAX];
	char words[16][MAX];
	int occur[MAX] = {0};

  while (!inputFile.eof())
	{
		inputFile.getline( line, MAX);
		strcpy( token, line);
		p1 = strtok( token, DELIMS);
		if (p1 == NULL)
		{
			cout << "Error: No tokens.";
			return -1;
		}
		if (unique == 0)
		{
			strcpy(words[unique], p1);
			unique++;
		}
		while (p1 != NULL)
		{
			count++;;
			
			if (p1 == NULL)
			{
				break;
			}
			for (int i = 0; i < unique; i++)
			{
				if (strcmp(words[i], p1) == 0)
				{
					occur[i]++;
					match = true;
					break;
				}
			}
			if (!match)
			{
				strcpy(words[unique], p1);
				unique++;
			}
			match = false;
			p1 = strtok( NULL, DELIMS);
		}
		numLines++;
	}
Last edited on
Your use of strtok differs from
http://www.cplusplus.com/reference/cstring/strtok/
What should I be using then?
Sorry, your use of strtok seems fine after all. The structure of your code distracts.

You can have only 16 unique words. If you find more, you will be out of range.
(However, you do reserve occurrence counters for MAX unique words.)

Why not call getline already on line 6?
Why the copy on line 9?
Why break with error on empty line?
Why explicit add-first? It will be added anyway, because there are no matches at that point.
Line 21 has established that p1 != NULL. Why the 25-28?
Topic archived. No new replies allowed.