Answer to Question #314532 in C++ for User 12345

Question #314532

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


1
Expert's answer
2022-03-19T15:01:28-0400
#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];
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog