Splitting code into separate modules

I've been trying to separate this code into different files, 2 headers and 3 different cpp ones, but keep getting errors. One of the main ones is array bound is not an integer before ']' token. I'm trying to use a global variable in the 2 header files

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  Main.cpp
#include "states.h"
#include "candidates.h"

#include <iostream>

using namespace std;

// Max # of candidates permitted by this program
const int maxCandidates = 10;

/**
 * Generate the report on the national primary election.
 */
int main(int argc, char** argv)
{
  readCandidates();
  int nStates;
  cin >> nStates;
  for (int i = 0; i < nStates; ++i)
    {
      readState();
      assignDelegatesToCandidates();
    }
  for (int i = 0; i < nCandidates; ++i)
    {
      printCandidateReport(i);
    }
  return 0;
}
candidates.h
#ifndef CANDIDATES_H_INCLUDED
#define CANDIDATES_H_INCLUDED

#include <iostream>

extern const int maxCandidates;

// How many delegates in the election (over all states)
int totalDelegates = 0;

// How many candidates in the national election?
int nCandidates;

// How many delgates have been won by each candidate
int delegatesWon[maxCandidates];

// Names of all candidates participating in the national election
std::string candidateNames[maxCandidates];

int findCandidate (std::string name);


#endif // CANDIDATES_H_INCLUDED

candidates.cpp

#include "candidates.h"
#include <iostream>

using namespace std;


/**
 * read the list of candidate names, initializing their delegate counts to 0.
 */
void readCandidates ()
{
  cin >> nCandidates;
  string line;
  getline (cin, line);

  for (int i = 0; i < nCandidates; ++i)
    {
      getline (cin, candidateNames[i]);
      delegatesWon[i] = 0;
    }
}

/**
 * Find the candidate with the indicated name. Returns the array index
 * for the candidate if found, nCandidates if it cannot be found.
 */
int findCandidate (std::string name)
{
  int result = nCandidates;
  for (int i = 0; i < nCandidates && result == nCandidates; ++i)
    if (candidateNames[i] == name)
      result = i;
  return result;
}


/**
 * Print the report line for the indicated candidate
 */
void printCandidateReport (int candidateNum)
{
  int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
  if (delegatesWon[candidateNum] >= requiredToWin)
    cout << "* ";
  else
    cout << "  ";
  cout << delegatesWon[candidateNum] << " " << candidateNames[candidateNum] << endl;
}

states.h

#ifndef STATES_H_INCLUDED
#define STATES_H_INCLUDED

#include <iostream>

extern const int maxCandidates;

// Names of the candidates participating in this state's primary
std::string candidate[maxCandidates];

// How many delegates are assigned to the state being processed
int delegatesForThisState;

// How many candidates in the primary for the state being processed
int nCandidatesInPrimary;

// How many states participate in the election
int nStates;

// How many votes were cast in the primary for this state
int totalVotes;

// How many votes wone by each candiate in this state's primary
int votesForCandidate[maxCandidates];

#endif // STATES_H_INCLUDED

states.cpp

#include "states.h"
#include <iostream>

using namespace std;


/**
 * read the info on one state's primaries
 */
void readState ()
{
  totalVotes = 0;
  cin >> nCandidatesInPrimary >> delegatesForThisState;
  totalDelegates += delegatesForThisState;  // "x += y" is a shorthand for "x = x + y"
  string word, line;
  getline (cin, line);
  for (int i = 0; i < nCandidatesInPrimary; ++i)
    {
      cin >> votesForCandidate[i];
      totalVotes = totalVotes + votesForCandidate[i];

      cin >> word;
      getline (cin, line);
      candidate[i] = word + line;
    }
}


/**
 * For the most recently read primary, determine how many delegates have
 * been won by each candidate.
 */
void assignDelegatesToCandidates ()
{
  int remainingDelegates = delegatesForThisState;
  for (int i = 0; i < nCandidatesInPrimary; ++i)
    {
      int candidateNum = findCandidate(candidate[i]);
      int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes-1)) / totalVotes;
      if (nDel > remainingDelegates)
	nDel = remainingDelegates;
      delegatesWon[candidateNum] += nDel;
      remainingDelegates -= nDel;
    }
}

Your problems appear to be being caused by all those global variables. You really should rework your program to eliminate all but the const qualified global variables.

That would make it easier but unfortunately this is a hw assignment and I'm not allowed to change any of the code, it was all one huge file I had to split it up into multiple instead but keep getting errors after doing so.
This seems like a very very bad assignment, however your biggest problem, other than using horrible global variables, is that you are using non-extern qualified global variables in your header files. Global variables should be extern qualified everywhere except one source file. Is it required to declare the horrible global variables in your header file? If not then you can declare all the global variables in the source file that contains main() without the extern qualifier and then in the source file that actually uses the global variable declare it with the extern qualifier.

Topic archived. No new replies allowed.