Answer to Question #306143 in C++ for ANKUR

Question #306143

An election is contested by 5 candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the votes cast for each candidate. Create custom exception to find invalid age of voter

1
Expert's answer
2022-03-04T12:31:23-0500
#include <iostream>
#include <string>

using namespace std;

void Menu()
{
	cout << "\nA - first candidate"
		<< "\nB - second candidate"
		<< "\nC - third candidate"
		<< "\nD - fourth candidate"
		<< "\nE - fifth candidate"
		<< "\n0 - exit program" << endl;
	cout << "Your choice: ";
}

int main()
{
	char ch;
	int candidates[5] = {0,0,0,0,0};
	do
	{
		Menu();
		cin >> ch;
		switch (ch)
		{
			case 'A':case 'a':
			{
				candidates[0]++;
				break;
			}
			case 'B':case 'b':
			{
				candidates[1]++;
				break;
			}
			case 'C':case 'c':
			{
				candidates[2]++;
				break;
			}
			case 'D':case 'd':
			{
				candidates[3]++;
				break;
			}
			case 'E':case 'e':
			{
				candidates[4]++;
				break;
			}
		}
	} while (ch != '0');
	cout << "\nResults of votes:";
	cout << "\nFirst candidate " << candidates[0]
		<< "\nSecond candidate " << candidates[1]
		<< "\nThird candidate " << candidates[2]
		<< "\nFourth candidate " << candidates[3]
		<< "\nFifth candidate " << candidates[4];
}

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