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

Write a program that allows the user to enter the last names of five candidates

ID: 3769168 • Letter: W

Question

Write a program that allows the user to enter the last names of five candidates
in a local election and the votes received by each candidate. The program should
then output each candidate’s name, votes received by that candidate, and the
percentage of the total votes received by the candidate. Your program should also
output the winner of the election. A sample output is as follows:

Candidate Votes Received % of Total Votes
-------------- ---------------------- ----------------
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Sam 1800 9.33

Total       19300

The Winner of the Election is Duffy.


Also is it possible to not get a copy of the code from any of these sites or elsewhere online.

http://www.cplusplus.com/forum/beginner/47746
http://jozefg.ecs.fullerton.edu/public/CS901/delete2/P5_ch13_exc3_Dynamic_Arrays_Votes_error_handling_doc_G+/Election-2dArray/election.cpp
http://www.dreamincode.net/forums/topic/36915-candidate-program

Just looking for unique code that can't be found online at a bunch of sites already

Thanks much

Explanation / Answer

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


int main()
{
int counter = 0;
int sum = 0;
double percentage = 0;
int arraySize = 0;
int maxVotes = 0;
int counterMax = 0;
string *names;
int *votes;
cout << "Enter the number of candidates: ";
cin >> arraySize;
       while (!cin)
       {
           cin.clear();
           cin.ignore(200, ' ');
           cout << " Input failed. Wrong data type. Enter the number of candidates: ";
           cin >> arraySize;
       }
cout << endl;

names = new string[arraySize];
votes = new int[arraySize];

   for (counter = 0; counter < arraySize; counter++)
       {
           cout << "Enter the candidate name...";
           cin >> names[counter];
           cout << "Enter the number of votes received...:";
           cin >> votes[counter];
           while (!cin)
           {
               cin.clear();
               cin.ignore(200, ' ');
               cout << "Input data mismatch. Please enter the number of votes received";
               cout << " for " << names[counter] << ": ";
               cin >> votes[counter];
           }
           sum = sum + votes[counter];                      
           cout << endl;
           cout << "Candidate: " << names[counter] << "   Votes: " << votes[counter] << endl << " ";
          

       }

   cout << fixed << showpoint << setprecision(2);
   cout << " ";
   cout << "Election Results" << endl;
   cout << setw(15) << left << "Candidate        Votes Percentage" << endl;
   for (counter = 0; counter < arraySize; counter++)
       {
           cout << setw(15) << left << names[counter];
           cout << setw(6) << right << votes[counter];

           percentage = static_cast<double>(votes[counter]) / sum * 100;
           cout << setw(10) << right << percentage;
           cout << endl;
       }

   cout << setw(15) << left << "Total:";
   cout << setw(6) << right << sum << endl;
   for (counter = 0; counter < arraySize; counter++)
       {
           if (votes[counter] > maxVotes)
           {
               maxVotes = votes[counter];
               counterMax = counter;  
           }
       }
      
       cout << "The winner of the Election is: " << names[counterMax] << endl;

delete [] names;
delete [] votes;
*names = "";
*votes = NULL;


}

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