How do you increment a linked class variable in a loop?

The point of my project is to make a list of variables that are taken from another class, WordCount and print them to an output file. WordCount simply stores a word and a counter for the number of occurences of a word. DynamicWordList is just a list class that stores WordCount variables. But I dont know how to increment the variable name so that i can declare numerous WordCount variables within the same loop.

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
int main()
{ 
  char array[40], save[40];
  DynamicWordList l1;
  WordCount word[0];
  ifstream inFile;
  inFile.open("lab8_text.txt");
  int x = 0, z = 0, m = 0;
  while (inFile >> array)
  {
        int test = 0;  
        for(int y = 0; y < strlen(array); y++)
        {
                if (isalpha(array[y]))
                {
                       if(isupper(array[y]))
                       {
                            array[y] = tolower(array[y]);
                       } 
                       save[z] = array[y];
                       z++;
                       }
                }
                save[z] = '\0';
                for(int j = 0; j < l1.getSize(); j++)
                {
                        if(word[j].isEqual(save) && strlen(save) > 0)
                        {
                              word[j].addOccur();
                              test++;
                        }
                }
                if(test == 0 && strlen(save) > 0)
                {
                        WordCount word[m]; // ***need to increment here***
                        word[m].setWord(save); // crashes 
                        l1.insert(word[m]); // also crashes
                        m++;
                }
        x++;
  }
  // here is where i would manipulate the DynamicWordList
  inFile.close();
  system("PAUSE");
}


Is my logic wrong? should i be going about this a different way or is there actually a way to increment the WordCount variable?
Last edited on
One solution is to use one of the standard container classes, such as vector

See http://www.cplusplus.com/reference/stl/ for documentaion about them.

so you would change
 
  WordCount word[0];

to say
 
  vector<WordCount> word;

To add a new entry to the vector just use the push_back method.
The docs explain it in much more detail than I can here:-)



Topic archived. No new replies allowed.