Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify your solution to 1. reading an arbitrary number of names and votes from a file 2. recognizing more than 1 winning candidate - several

Modify your solution to

1. reading an arbitrary number of names and votes from a file

2. recognizing more than 1 winning candidate - several candidates with the greatest number of votes

#include #include #include #include #include #include

using namespace std;

string readName(); int readVotes(const string& name); string trim(const string& original); int compTotalVotes(const vector& votes); void compPercentVotes(const vector& votes, vector& percents); int findWinner(const vector& votes); void writeCandidates(ostream& outs, const vector& names, const vector& votes); int seqSearch(const vector& names, string candidate_name);

int main() { try { const int n_candidates = 5; vector names(n_candidates); vector votes(n_candidates); for (int index = 0; index < n_candidates; index++) { string candidate_name; do { candidate_name = readName();

} while (seqSearch(names, candidate_name) != -1);

names.at(index) = candidate_name; int candidate_votes = readVotes(candidate_name); votes.at(index) = candidate_votes; cin.get(); } writeCandidates(cout, names, votes); }

catch (exception ex)

{ cerr << ex.what() << endl; }

system("pause");

return 0;

}

void writeCandidates(ostream& outs, const vector& names, const vector& votes)

{

const int filler = 5; vector percents(names.size()); int total_votes = compTotalVotes(votes); int fieldWidthVotes = max(string("Votes Received").length(), to_string(total_votes).length()) + filler; int fieldWidthPercents = max(string("% of Total Votes").length(), to_string(100.00).length()) + filler; compPercentVotes(votes, percents); int winner_index = findWinner(votes); int fieldWidthName = string("Candidate").length();

for (int index = 0; index < names.size(); index++)

{ string candidate_name; candidate_name = names.at(index); if (candidate_name.length() > fieldWidthName) fieldWidthName = candidate_name.length();

}

outs << setw(fieldWidthName) << left << "Candidate" << setw(fieldWidthVotes) << right << "Votes Received" << setw(fieldWidthPercents) << right << "% of Total Votes" << endl; for (int index = 0; index < names.size(); index++)

{

string candidate_name; candidate_name = names.at(index); if (candidate_name.length() > fieldWidthName) fieldWidthName = candidate_name.length(); int candidate_votes; candidate_votes = votes.at(index); float candidate_percents; candidate_percents = percents.at(index); outs << setw(fieldWidthName) << left << candidate_name << setw(fieldWidthVotes) << right << candidate_votes << setw(fieldWidthPercents) << right << fixed << setprecision(2) << 100 * candidate_percents << endl;

}

outs << setw(fieldWidthName) << left << "Total" << setw(fieldWidthVotes) << right << total_votes << endl;

string winner_name;

winner_name = names.at(winner_index);

outs << "The winner of the election is " << winner_name << endl;

}

void compPercentVotes(const vector& votes, vector& percents)

{

float total_votes = static_cast(compTotalVotes(votes));

for (int index = 0; index < votes.size(); index++)

{

int candidate_votes;

candidate_votes = votes.at(index);

float percent = total_votes > 0 ? candidate_votes / total_votes : 0.0F;

percents.at(index) = percent;

}

}

int compTotalVotes(const vector& votes)

{

int total_votes = 0;

for (int index = 0; index < votes.size(); index++)

{

int candidate_votes;

candidate_votes = votes.at(index);

total_votes += candidate_votes;

}

return total_votes;

}

int findWinner(const vector& votes)

{

int winner_index = 0;

int winner_votes;

winner_votes = votes.at(winner_index);

for (int index = 1; index < votes.size(); index++)

{

int candidate_votes;

candidate_votes = votes.at(index);

if (candidate_votes > winner_votes)

{

winner_index = index;

winner_votes = candidate_votes;

}

}

return winner_index;

}

int readVotes(const string& name)

{

int votes;

do

{

cout << "Enter " << name << "'s votes: ";

cin >> votes;

if (!cin.good())

{

cin.clear();

cin.ignore(numeric_limits::max(), ' ');

votes = -1;

}

} while (votes < 0);

return votes;

}

string readName()

{

string name;

do

{

cout << "Enter candidate's name: ";

getline(cin, name, ' ');

name = trim(name);

} while (name.length() == 0);

return name;

}

string trim(const string& original)

{

int right_index = original.length() - 1;

int left_index = 0;

while (left_index <= right_index && isspace(original[left_index]))

left_index++;

while (left_index <= right_index && isspace(original[right_index]))

right_index--;

unsigned length = right_index - left_index + 1;

return original.substr(left_index, length);

}

int seqSearch(const vector& names, string candidate_name)

{

int gf = -1;

for (int kk = 0; kk

{

if (names.at(kk) == candidate_name)

{

gf = kk;

break;

}

}

return gf;

}

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_2

Step: 3

blur-text-image_3

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

Build It For The Real World A Database Workbook

Authors: Wilson, Susan, Hoferek, Mary J.

1st Edition

0073197599, 9780073197593

More Books

Students also viewed these Databases questions