Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;

}

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions