How do you get rid of unterminated #ifndef error? main.cpp #include <iostream> #
ID: 3563028 • Letter: H
Question
How do you get rid of unterminated #ifndef error?
main.cpp
#include <iostream>
#include "candidates.h"
#include "state.h"
using namespace std;
void printCandidateReport();
/**
* 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;
}
/**
* 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)
{
void readState();
assignDelegatesToCandidates();
}
for (int i = 0; i < nCandidates; ++i)
{
printCandidateReport(i);
}
return 0;
}
candidates.h
#include "state.h"
using namespace std;
// Max # of candidates permitted by this program
extern const int maxCandidates = 10;
// Names of the candidates participating in this state's primary
extern string candidate[maxCandidates];
// Names of all candidates participating in the national election
extern std::string candidateNames[maxCandidates];
// How many candidates in the national election?
extern int nCandidates;
// How many candidates in the primary for the state being processed
extern int nCandidatesInPrimary;
extern int findCandidate (std::string name);
extern void readCandidates ();
extern void assignDelegatesToCandidates ();
extern int findCandidate (std::string name);
state.h
#ifndef STATE_H
#define STATE_H
#include <iostream>
#include "candidates.h"
// How many delegates are assigned to the state being processed
extern int delegatesForThisState;
// How many delgates have been won by each candidate
extern int delegatesWon[maxCandidates];
// How many states participate in the election
extern 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
extern int totalVotes;
// How many votes wone by each candiate in this state's primary
extern int votesForCandidate[maxCandidates];
candidates.cpp
#include <iostream>
#include <fstream>
#include "candidates.h"
#include "state.h"
using namespace std;
void readCandidates ()
{
cin >> nCandidates;
string line;
getline (cin, line);
for (int i = 0; i < nCandidates; ++i)
{
getline (cin, candidateNames[i]);
delegatesWon[i] = 0;
}
}
/**
* 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;
}
state.cpp
#include <iostream>
#include <fstream>
#include "candidates.h"
#include "state.h"
/**
* 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;
}
}
Explanation / Answer
state.h
#ifndef STATE_H
#define STATE_H
#include <iostream>
#include "candidates.h"
// How many delegates are assigned to the state being processed
extern int delegatesForThisState;
// How many delgates have been won by each candidate
extern int delegatesWon[maxCandidates];
// How many states participate in the election
extern 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
extern int totalVotes;
// How many votes wone by each candiate in this state's primary
extern int votesForCandidate[maxCandidates];
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.