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
Main.cpp
#include "states.h"
#include "candidates.h"
#include <iostream>
usingnamespace std;
// Max # of candidates permitted by this program
constint 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>
externconstint 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>
usingnamespace 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>
externconstint 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>
usingnamespace 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.