Question
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
using namespace std;
string readName(); int readVotes(const string& name); string trim(const string& original); int compTotalVotes(const vector
int main() { try { const int n_candidates = 5; vector
} 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
{
const int filler = 5; vector
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
{
float total_votes = static_cast
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
{
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
{
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
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
{
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started