You are here:

C++/ADTs help

Advertisement


Question
QUESTION: Alright so I made the functions from the  calls inside "primaryElection" and my program compiles and everything so I know that I have written the ADTs right however I am unsure on what goes inside those functions that I created such as:

void readCandidates(istream& input)
{
?
}

Do you think you could guide me to solving this problem by writing comments or pseudo code?

--------------------------------------------------------
#include "candidates.h"
#include "candidateCollections.h"
#include "states.h"

#include <fstream>


using namespace std;


void primaryElection (istream& input, ostream& output);


int main(int argc, char** argv)
{
// Main routine - if command parameters give two filenames,
//  read from the first and write to the second. If not,
//  read from standard input and write to standard output
if (argc == 3)
  {
    ifstream in (argv[1]);
    ofstream out (argv[2]);
    primaryElection (in, out);
  }
else
  primaryElection (cin, cout);

return 0;
}


void readCandidates(istream& input)
{
// Read the candidate names
string line;
getline (input, line);
while (line != "")
  {
    Candidate c (line);
    CandidateCollection myCandidateCollection;
    myCandidateCollection.addCandidate (c);
    getline (input, line);
  }
}
void readState(istream& input)
{

}
void assignDelegatesToCandidates()
{

}
void printCandidateReport(ostream& output, int i)
{
//Print candidates with amount of delegates won
}
void primaryElection (istream& input, ostream& output)
{

readCandidates(input);
int nStates;
input >> nStates;
for (int i = 0; i < nStates; ++i)
  {
    readState(input);
    assignDelegatesToCandidates();
  }
  int nCandidates;
for (int i = 0; i < nCandidates; ++i)
  {
    printCandidateReport(output, i);
  }
}

----------------------------------------------
#ifndef CANDIDATECOLLECTIONS_H
#define CANDIDATECOLLECTIONS_H

#include <iostream>
#include <vector>
#include "candidates.h"



class CandidateCollection
{
 std::vector<Candidate> candidates;
public:

 void addCandidate (const Candidate& candidate);

 int numberOfCandidates() const {return candidates.size();}

 Candidate& get (int index) {return candidates[index];}

 /**
  * Find the candidate with the indicated name. Returns the array index for
  * the candidate if found, -1 if it cannot be found.
  */
 int findCandidate (std::string name);

 /**
  * Read a list of candidate names from an input stream,
  * one per line, stopping at an empty line
  */
 void read (std::istream& input);
};


#endif
---------------------------------------------------------
#ifndef CANDIDATES_H
#define CANDIDATES_H

#include <iostream>
#include <string>

/**
* Information on the political candidates in the primaries.
*/

class Candidate {
 std::string name;
 int delegatesWon;

public:
 Candidate (std::string candidateName);

 std::string getName() {return name;}
 void setName(std::string nam) {name = nam;}

 int getNumberOfDelegatesWon() {return delegatesWon;}
 void setNumberOfDelegatesWon(int nDelegates) {delegatesWon = nDelegates;}
 void addDelegatesWon(int nDelegates) {delegatesWon += nDelegates;}

 /**
  * Print the report line for the indicated candidate
  */
 void report (std::ostream& out, int totalDelegatesAllStates);
};

#endif

-----------------------------------------------------------------
#ifndef STATES_H
#define STATES_H

#include <iostream>
#include <string>
#include <vector>
#include "candidates.h"

/**
* Information on the state primaries
*/


class StatePrimary {
public:
 StatePrimary(int numDelegatesForState);


 // Record the fact that this candidate participated
 // in the election and how many votes he/she received.
 void addCandidate (std::string name, int nVotes);


 // Compute the candidates won by each candidate in this state.
 // (Call this after all candidates have been added,
 // and before requesting the delegates won by any candidate.)
 void assignDelegatesToCandidates();


 // get number of delegates won by this candidate. (If the candidate
 // did not participate, return 0.)
 int getNumberOfDelegates (std::string name);


 // ==============================================
 // All information below this line is hidden
private:
 int numDelegates;

 struct CandidateInfo {
   std::string name;
   int votesInThisState;
   int delegatesWonInThisState;
 };
 std::vector<CandidateInfo> stateCandidates;

 void sortByVotes();
};


#endif

ANSWER: There is a logical error in the function you indicated.

void readCandidates(istream& input)
{
// Read the candidate names
string line;
getline (input, line); // read the first line
while (line != "") // as long as the line read is not empty
         // assumes that the file ends with a blank line
 {
   Candidate c (line); // create a candidate whose name has been just read

   CandidateCollection myCandidateCollection; // create a CandidateCollection

   myCandidateCollection.addCandidate (c); // add the candidate to the CandidateCollection

   getline (input, line); // read the next line
   
   // *** logical error *** myCandidateCollection gets destroyed here; its lifetime is over
   // next time through the loop, create another CandidateCollection
   // which will get destroyed at the end of that loop; and so on.
 }
}

You need to rewrite this. Perhaps make myCandidateCollection a global variable.
 



---------- FOLLOW-UP ----------

QUESTION: How would I rewrite it to make it a global variable?

Answer
#include "candidates.h"
#include "candidateCollections.h"
#include "states.h"

#include <fstream>


using namespace std;


void primaryElection (istream& input, ostream& output);

CandidateCollection myCandidateCollection; // *** global with static storage duration

int main(int argc, char** argv)
{

  // ...

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.