Question: Redo the Election Results programming example of Chapter 18 so that it uses the STL class list to process candidates data. The program (in main.cpp

Redo the Election Results programming example of Chapter 18 so that it uses the STL class list to process candidates data.

The program (in main.cpp) should:

Read data from candData.txt and voteData.txt

Print out the number of votes each candidate received in each region

Print the winners name and votes received in the following format:

Winner: Sheila Bower, Votes Received: 493

Print the total number of votes in the following format:

Total votes polled: 2216

An example of the program is shown below:

 --------------------Election Results-------------------- Votes Candidate Name Region1 Region2 Region3 Region4 Total ------------------ ------- ------- ------- ------- ------ Sheila Bower 23 70 133 267 493 Danny Dillion 25 71 156 97 349 Lisa Fisher 110 158 0 0 268 Greg Goldy 75 34 134 0 243 Peter Lamba 285 56 0 46 387 Mickey Miller 112 141 156 67 476 Winner: Sheila Bower, Votes Received: 493 

#include

#include

#include

#include

const int NUM_REGIONS = 4;

const int MAX_CANDIDATES = 100;

struct candidateType {

string name;

int votes[NUM_REGIONS];

int totalVotes;

};

void getCandidateData(list &candidates) {

ifstream inFile;

int count = 0;

inFile.open("candData.txt");

if (!inFile) {

cout

return;

}

while (count > candidates[count].name) {

for (int region = 0; region

inFile >> candidates[count].votes[region];

}

count++;

}

inFile.close();

}

void getVotes(list &candidates) {

ifstream inFile;

int count = 0;

string name;

int votes;

inFile.open("voteData.txt");

if (!inFile) {

cout

return;

}

while (count > name) {

inFile >> votes;

for (list::iterator it = candidates.begin(); it != candidates.end(); ++it) {

if (it->name == name) {

it->totalVotes += votes;

break;

}

}

count++;

}

inFile.close();

}

void printResult(list &candidates) {

cout

cout

cout

int totalVotes = 0;

for (list::iterator it = candidates.begin(); it != candidates.end(); ++it) {

cout name

for (int region = 0; region

cout votes[region]

}

cout totalVotes

totalVotes += it->totalVotes;

}

cout

cout

}

int main() {

list candidates;

getCandidateData(candidates);

getVotes(candidates);

candidates.sort([](const candidateType &a, const candidateType &b)

{

return a.votes > b.votes;

});

//iterate through candidates list to get winner and number of votes received

auto it = candidates.begin();

candidateType winner = *it;

cout

//calculate and print total votes

int totalVotes = 0;

for (const auto &candidate : candidates) {

totalVotes += candidate.votes;

}

cout

return 0;

}

Redo the Election Results programming example of Chapter 18 so that it

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!