Write a C++ 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
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
string LastNames[5];
int votes[5] = { 0,0,0,0,0 };
int summ = 0;
for (int i = 0; i < 5; i++)
{
cout << "Please, enter the last name of candidate " << i + 1<<": ";
cin >> LastNames[i];
cout << "Please, enter the number of votes received by "<< LastNames[i]<<": ";
cin >> votes[i];
summ+= votes[i];
}
cout << "\nResults of votes:";
int maxVotes = votes[0];
int numcand=0;
for (int i = 0; i < 5; i++)
{
cout << "Result of "<< LastNames[i] <<" is "<< votes[i]<<" votes "
<<" and "<<votes[i]*100/ summ <<" percents"<<endl;
if (maxVotes < votes[i])
{
maxVotes = votes[i];
numcand = i;
}
}
cout << "The winner is " << LastNames[numcand];
}
Comments
Leave a comment