At every
voting station the voters vote by choosing A, B or C on a ballot paper. The voting officer must enter the votes into
the program so that they can be counted. X is entered when all the votes at a specific voting station have been
entered.
The program :
The three totals and the number of spoilt votes are initialised to 0. Use the following integer variables
votesForA, votesForB, votesForC, spoiltVotes
Use a for loop, going from 1 to the number of voting stations.
Inside this loop is a while loop. A prompting message appears on the screen, asking the voter for which
candidate he or she wants to vote. The choice of the voter is then input.
Inside the while loop is a switch statement to increment the correct total. The default option is used
to count the number of spoilt votes.
The while loop is exited when X is entered for the choice.
When the for loop is exited, the three totals and the number of spoilt votes are displayed.
#include<iostream>
#include<conio.h>
using namespace std;
const int x=5;
int votesForA=0;
int votesForB=0;
int votesForC=0;
int spoiltvotes=0;
char v;
int main()
{
int i;
for (i=1;i<=x ;i++ )
cout<<"\n Enter your vote:\t";
cin>>v;
while( v<=5)
{
switch(v)
{
case 1: votesForA++;
break;
case 2:votesForB++;
break;
case 3:votesForC++;
break;
default:spoiltvotes++;
}
cout<<"\n Input your vote:\t";
cin>>v;
}
cout<<"Number of votes for candidate\n\tA:"<<votesForA;
cout<<"Number of votes for candidate B:";
cout<<votesForB<<"\n\tNumber of votes for candidateC:"<<votesForC;
cout<<"\n\nNumber of spoilt votes:\t"<<spoiltvotes;
getch();
return 0;
}
Comments
Leave a comment