modules


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)) ===|

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

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

Candidates .cpp:

#include <iostream>
#include <string>


using namespace 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
const int 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>


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 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 
Last edited on
candidates.cpp seems to be missing any include files.
Sorry, I must have missed it when copying it into the forum. It should be there now.
Topic archived. No new replies allowed.