Reading from Multiple Files

Hello, I am having a bit of trouble with reading code from multiple files.

I have my main.cpp program which contains the int main() stuff, then I have a candidates.h file where I declared all of the functions I'm using in the main.cpp file. Then lastly, I have a candidates.cpp file which writes out the functions being used in the program.

I have the #include "candidates.h" in both the main.cpp and candidates.cpp but when I run it, it says that the functions are undeclared and I don't know why its doing that. I thought I did everything right.
Please show us the exact error message you get. You've got to realize that with a description like that it's pretty much impossible to tell what the actual problem is.
error: 'readCandidates' undeclared (first use this function)

and basically lisitng every function I used
I'm going to take a shot in the dark and say this is because even though you are declaring your functions in "candidates.cpp", you are not linking it to your main program so it's not getting compiled. Which IDE are you using? This will let us help you with a step by step.
I'm not exactly sure what an IDE is but if I'm right, I'm Using Code Blocks
@Computergeek It says the functions aren't declared - that's clearly a compiler error, not a linker one.

Please show us the contents of main.cpp and candidates.h
Last edited on
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
//main.cpp


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


using namespace std;



/**
 * 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;
}



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
#include <iostream>
#include "candidate.h"


using namespace std;

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

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

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

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

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

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

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

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

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

// 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];

/**
 * 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;
    }
}



/**
 * 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;
}


/**
 * 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;
    }
}


/**
 * 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;
    }
}




Okay, so I figured out most of it just now. It was cause I didn't put the Header File inside of the project, itself. After that, it eliminated most of the errors but I still have one. The error is:

error: 'nCandidates' undeclared (first use this function)

 

and the candidate.h is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#ifndef CANDIDATE_H_INCLUDED
#define CANDIDATE_H_INCLUDED

int findCandidate (std::string name);

void assignDelegatesToCandidates ();

int findCandidate (std::string name);

void printCandidateReport (int candidateNum);

void readCandidates ();

void readState ();


#endif // CANDIDATE_H_INCLUDED 

What line does it sow the error on? Also, you may not want to declare nCandidates as extern.
The error is on line 25 of the main.cpp

I forgot to take the extern out, but I just put it in there to see if that would fix it.

With or without the extern, I still get the same error.
Topic archived. No new replies allowed.