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 Total votes polled: 2216

task:

out of 10.00

Election summary printed

1 out of 2 checks passed. Review the results below for more details.

Checks

Code PatternComplete

Did not hard code the program output

Test CaseIncomplete

Election data 1

Input

 

Output

 

Results

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

Candidate list sorted with the sort method of the STL class list

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

Total votes polled: 2216

10.00 out of 10.00

STL header file included

1 out of 1 checks passed. Great work!

Checks

Code PatternComplete

Include list header file

7.50 out of 10.00

STL methods used

3 out of 4 checks passed. Review the results below for more details.

Checks

Code PatternComplete

Variable of type list declared

Code PatternIncomplete

Candidate list sorted with the sort method of the STL class list

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

Total votes polled: 2216

main.cpp:

#include

#include

#include

#include

struct candidateType {

string name;

int region1, region2, region3, region4;

int total;

};

int main() {

list candidates;

ifstream candDataFile("candData.txt");

while (candDataFile.good()) {

candidateType candidate;

candDataFile >> candidate.name;

// candidate.total = candidate.region1 + candidate.region2 + candidate.region3 + candidate.region4;

candidates.push_back(candidate);

}

candDataFile.close();

ifstream voteDataFile("voteData.txt");

while (voteDataFile.good()) {

string region;

int votes;

voteDataFile >> region >> votes;

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

if (region == "Region1") {

it->region1 = votes;

} else if (region == "Region2") {

it->region2 = votes;

} else if (region == "Region3") {

it->region3 = votes;

} else if (region == "Region4") {

it->region4 = votes;

}

it->total = it->region1 + it->region2 + it->region3 + it->region4;

}

}

voteDataFile.close();

cout << "--------------------Election Results--------------------" << endl;

cout << "Votes\tCandidate Name\tRegion1\tRegion2\tRegion3\tRegion4\tTotal" << endl;

cout << "------------------\t-------\t-------\t-------\t-------\t------" << endl;

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

cout << it->name << "\t\t" << it->region1 << "\t\t" << it->region2 << "\t\t" << it->region3 << "\t\t" << it->region4 << "\t\t" << it->total << endl;

}

candidates.sort([](const candidateType &a, const candidateType &b) { return a.total > b.total; });

cout << "Winner: " << candidates.front().name << ", Votes Received: " << candidates.front().total << endl;

int totalVotes = 0;

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

totalVotes += it->total;

}

cout << "Total votes polled: " << totalVotes << endl;

return 0;

}

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

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions