Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.
sample output:
Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.73
Total 9000
The winner of the election is Johnson.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int max = 5, votes[max], max_vote = 0, total = 0, width = 20;
float percentage;
string names[max];
for(int i = 0; i < max; i++){
cout<<"Enter the name of candidate "<<i + 1<<endl;
cin>>names[i];
cout<<"Enter the number of votes that "<<names[i]<<" received.\n";
cin>>votes[i];
total += votes[i];
if(votes[i] > votes[max_vote]){
max_vote = i;
}
}
cout<<setw(width)<<left<<"Candidate"<<setw(width)<<"Votes Received"<<setw(width)<<"%of Total Votes"<<endl;
for(int i = 0; i < max; i++){
percentage = (float)votes[i]/(float)total * 100;
cout<<setw(width)<<left<<names[i]<<setw(width)<<votes[i]<<setw(width)<<percentage<<endl;
}
cout<<setw(width)<<"Total"<<setw(width)<<total<<endl;
cout<<"\nThe winner of the election is "<<names[max_vote];
return 0;
}
Comments
Leave a comment