write a c++ program enter last name of 5 candidates number votes received, then
ID: 3627717 • Letter: W
Question
write a c++ program enter last name of 5 candidates number votes received, then program should output each name, number of votes received, percentage of total votes received and announce the winner#include <string>#include<iostream>
using namespace std;
class ElectionResults
{
public:
string name;
int votes;
};
int main()
{
ElectionResults res[5];
int voteTotal = 0;
int winner = 0;
int byVotes = 0;
for(int i = 0; i < 5; i++)
{
cout << "Please Enter Last Name of Candidate " << i+1 << ": ";
cin >> res[i].name;
cout<<"Please Enter the Votes Received for Candidate " << i+1 << ": ";
cin >> res[i].votes;
voteTotal += res[i].votes;
if( res[i].votes > byVotes)
winner = i;
}
for(int i = 0; i < 5; i++)
{
cout << "Candidate: " << res[i].name <<" votes: " << res[i].votes << "percent :" <<(res[i].votes*100)/voteTotal << "%." <<endl;
}
cout << "Winner = " << res[winner].name<<endl;
cin >> voteTotal;
}
Explanation / Answer
// working now enjoy !!
#include <string>
#include<iostream>
using namespace std;
class ElectionResults
{
public:
string name;
int votes;
};
int main()
{
ElectionResults res[5];
int voteTotal = 0;
int winner = 0;
int byVotes = 0;
for(int i = 0; i < 5; i++)
{
cout << "Please Enter Last Name of Candidate " << i+1 << ": ";
cin >> res[i].name;
cout<<"Please Enter the Votes Received for Candidate " << i+1 << ": ";
cin >> res[i].votes;
voteTotal += res[i].votes;
if( res[i].votes > byVotes)
{
byVotes = res[i].votes;
winner = i;
}
}
for(int i = 0; i < 5; i++)
{
cout << "Candidate: " << res[i].name <<" votes: " << res[i].votes << "percent :" <<(res[i].votes*100)/voteTotal << "%." <<endl;
}
cout << "Winner = " << res[winner].name<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.