An election is contested by five 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 using an array variable count. In case a number is read outside the range of 1 to 5, the ballot should be considered as a ‘spoilt ballot’ and the program should also count the number of spoil ballots.
#include<iostream.h>
#include<conio.h>
class election
{
private:
int
count[6];
public:
election()
{
count[0]=0;
count[1]=0;
count[2]=0;
count[3]=0;
count[4]=0;
count[5]=0;
}
void
voting();
void
result();
};
void election::voting()
{
char
c='y';
int ch;
while(c=='y'||c=='Y')
{
cout<<"\n Enter your choice for voting according below
list";
cout<<"\n 1. Narendra ";
cout<<"\n 2. surendra ";
cout<<"\n 3. virendra";
cout<<"\n 4. mitendra";
cout<<"\n 5. Hitendra";
cin>>ch;
if(ch>=1 ||ch<=5)
{
count[ch]++;
}
else
{
count[0]++;
}
cout<<"\n Do you want to continue";
cin>>c;
}
}
void election::result()
{ int i;
cout<<"\n 1. Narendra "<<count[1];
cout<<"\n 2. surendra "<<count[2];
cout<<"\n 3. virendra"<<count[3];
cout<<"\n 4. mitendra"<<count[4];
cout<<"\n 5. Hitendra"<<count[5];
cout<<"\n Spoil Ballot"<<count[0];
}
void main()
{
int no;
election
e1;
cout<<"\n Enter 1 for voting";
cout<<"\n Enter 2 for result";
cout<<"\n Enter 3 for exit";
cout<<"\n Enter your choice";
cin>>no;
switch(no)
{
case
1:
e1.voting();
case
2:
e1.result();
case
3:
exit(0);
}
}