Trying Separate Program Separate Modules Cannot Figure Two Errors Help Would Greatly Appre Q12150853

I am trying to separate a program into separate modules but Icannot figure out these two errors. Any help would be greatlyappreciated.

||=== Build: Debug in primaries (compiler: GNU GCCCompiler) ===| C:UsersHeatherDocumentsprimariesstates.cpp||Infunction ‘void readState()’:|C:UsersHeatherDocumentsprimariesstates.cpp|23|error:’votesForCandidate’ was not declared in this scope|C:UsersHeatherDocumentsprimariesstates.cpp|28|error:’candidate’ was not declared in this scope| ||=== Build failed: 2error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

Primaries(main):

#include <iostream>
#include <fstream>
#include <string>

using namespace 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 +2rounds up
if (delegatesWon[candidateNum] >= requiredToWin)
cout << “* “;
else
cout << ” “;
cout << delegatesWon[candidateNum] << ” ” <<candidateNames[candidateNum] << endl;
}

Candidates .cpp:

/**
* For the most recently read primary, determine how many delegateshave
* 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
const int maxCandidates = 10;

// Names of the candidates participating in this state’sprimary
string candidate[maxCandidates];

void printPrimaryReport (int candidateNum);

// Names of all candidates participating in the nationalelection
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’sprimary
int votesForCandidate[maxCandidates];

int findCandidate (std::string name);

void readCandidatesList ();

#endif // CANDIDATES_H_INCLUDED

states .cpp:

#include <iostream>
#include <string>

using namespace 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 shorthandfor “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 beingprocessed
int numCandidatesInPrimary;

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

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

void assignDelegatesToCandidates ();

#endif // STATES_H_INCLUDED

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *