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 spoilt ballots.
CODING:
#include<iostream.h>
#include<conio.h>
class el
{
int arry[5],spoilt;
int ch;
public:
void set();
void count(int);
void disp(int);
};
void el::set()
{
int i;
for(i=0;i<5;i++)
{
arry[i]=0;
spoilt=0;
}
}
void el::count(int n)
{
switch(n)
{
case 1:
arry[1]++;
break;
case 2:
arry[2]++;
break;
case 3:
arry[3]++;
break;
case 4:
arry[4]++;
break;
case 5:
arry[5]++;
break;
default:
spoilt++;
break;
}
}
void el::disp(int n)
{
if(n<=5)
cout<<arry[n];
else
cout<<spoilt;
}
void main()
{
int ch;
char a='y';
el ob1;
clrscr();
cout<<"S E L E C T C H O I C E";
cout<<"\n1.A";
cout<<"\n2.B";
cout<<"\n3.C";
cout<<"\n4.D";
cout<<"\n5.E";
ob1.set();
do{
cout<<"\nEnter Number:";
cin>>ch;
ob1.count(ch);
ob1.disp(ch);
cout<<"\nY=VOTES||N=EXITE:";
cin>>a;
}
while(a=='y' || a=='Y');
getch();
}
Comments
Post a Comment