Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify your solution to 1. reading an arbitrary number of names and votes from a

ID: 3802079 • Letter: M

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 candidates with the greatest number of votes

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

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

int main()
{
   try
   {
       const int n_candidates = 5;
       vector<string> names(n_candidates);
       vector<int> 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<string>& names, const vector<int>& votes)

{

   const int filler = 5;
   vector<float> 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<int>& votes, vector<float>& percents)

{

   float total_votes = static_cast<float>(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<int>& 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<int>& 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<unsigned>::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<string>& names, string candidate_name)

{

   int gf = -1;

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

   {

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

       {

           gf = kk;

           break;

       }

   }

   return gf;

}

Explanation / Answer

// Code Changes For First Part of the Program -------------------------------------------------------------------

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
//creating a stream
#include <fstream>

using namespace std;
string readName();
int readVotes(const string& name);
string trim(const string& original);
int compTotalVotes(const vector<int>& votes);
void compPercentVotes(const vector<int>& votes, vector<float>& percents);
int findWinner(const vector<int>& votes);
void writeCandidates(ostream& outs, const vector<string>& names, const vector<int>& votes);
int seqSearch(const vector<string>& names, string candidate_name);
int main()
{
try
{
vector<string> names = readName();
vector<int> votes = readVotes();
  
  
writeCandidates(cout, names, votes);
}
catch (exception ex)
{
cerr << ex.what() << endl;
}
system("pause");
return 0;
}
void writeCandidates(ostream& outs, const vector<string>& names, const vector<int>& votes)
{
const int filler = 5;
vector<float> 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<int>& votes, vector<float>& percents)
{
float total_votes = static_cast<float>(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<int>& 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<int>& 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()
{
vector<string> votes;
string name;
int vote;
ifstream infile;
   infile.open ("votes.txt");
// considering that votes.txt contains data as lines of "name vote"
// Assuming all inputs are valid
   //eg.
   //A 5
//B 15
while (infile >> name >> vote)
{
// process pair (a,b)
index = seqSearch(names, candidate_name);
votes.at(index) = vote;
}
  
return votes;
}
string readName()
{
int index = 0;
vector<string> names;
string name;
   ifstream infile;
   infile.open ("names.txt");
   // Assuming all inputs are valid
while(!infile.eof) // To get you all the lines.
{
getline(infile,name); // Saves the line in STRING.
names.at(index) = name;
index++;
}
   infile.close();
   return names;
}
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<string>& names, string candidate_name)
{
int gf = -1;
for (int kk = 0; kk<names.size(); kk++)
{
if (names.at(kk) == candidate_name)
{
gf = kk;
break;
}
}
return gf;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote