I am trying to separate a program into separate modules but I cannot figure out these two errors. Any help would be greatly appreciated.
||=== Build: Debug in primaries (compiler: GNU GCC Compiler) ===| C:\Users\Heather\Documents\primaries\states.cpp||In function 'void readState()':| C:\Users\Heather\Documents\primaries\states.cpp|23|error: 'votesForCandidate' was not declared in this scope| C:\Users\Heather\Documents\primaries\states.cpp|28|error: 'candidate' was not declared in this scope| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
Primaries(main):
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
#include "states.h"
#include "candidates.h"
/**
* Generate the report on the national primary election.
*/
int main(int argc, char** argv)
{
readCandidatesList();
int numStates;
cin >> numStates;
for (int i = 0; i < numStates; ++i)
{
readState();
assignDelegatesToCandidates();
}
for (int i = 0; i < numCandidates; ++i)
{
printPrimaryReport(i);
}
return 0;
}
/**
* Print the report line for the indicated candidate
*/
void printPrimaryReport (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;
}
Candidates .cpp:
#include <iostream>
#include <string>
usingnamespace std;
#include "candidates.h"
#include "states.h"
/**
* 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 < numCandidatesInPrimary; ++i)
{
int candidateNum = findCandidate(candidate[i]);
int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes-1)) / totalVotes;
if (nDel > remainingDelegates)
nDel = remainingDelegates;
delegatesWon[candidateNum] += nDel;
remainingDelegates -= nDel;
}
}
Candidates.h:
#ifndef CANDIDATES_H_INCLUDED
#define CANDIDATES_H_INCLUDED
#include <iostream>
// Max # of candidates permitted by this program
constint maxCandidates = 10;
// Names of the candidates participating in this state's primary
string candidate[maxCandidates];
void printPrimaryReport (int candidateNum);
// Names of all candidates participating in the national election
std::string candidateNames[maxCandidates];
// How many delegates have been won by each candidate
int delegatesWon[maxCandidates];
// How many candidates in the national election?
int numCandidates;
// How many votes won by each candidate in this state's primary
int votesForCandidate[maxCandidates];
int findCandidate (std::string name);
void readCandidatesList ();
#endif // CANDIDATES_H_INCLUDED
states .cpp:
#include <iostream>
#include <string>
usingnamespace std;
#include "states.h"
/**
* read the info on one state's primaries
*/
void readState ()
{
totalVotes = 0;
cin >> numCandidatesInPrimary >> delegatesForThisState;
totalDelegates += delegatesForThisState; // "x += y" is a shorthand for "x = x + y"
string word, line;
getline (cin, line);
for (int i = 0; i < numCandidatesInPrimary; ++i)
{
cin >> votesForCandidate[i];
totalVotes = totalVotes + votesForCandidate[i];
cin >> word;
getline (cin, line);
candidate[i] = word + line;
}
}
States.h:
#ifndef STATES_H_INCLUDED
#define STATES_H_INCLUDED
#include <iostream>
// How many states participate in the election
int numStates;
void readState ();
// How many delegates in the election (over all states)
int totalDelegates = 0;
// How many candidates in the primary for the state being processed
int numCandidatesInPrimary;
// How many votes were cast in the primary for this state
int totalVotes;
// How many delegates are assigned to the state being processed
int delegatesForThisState;
void assignDelegatesToCandidates ();
#endif // STATES_H_INCLUDED