[size=3][b]PROGRAM 1
[red]Suppose a new member of the city council has to be chosen from three candidates and suppose there are 4 voting stations. We need a C++ program that will count the votes for every candidate and display the result. At every voting station the voters have to be asked one after the other for which candidate ( indicated by A or B or C) he or she wants to vote. X is entered when the voters at the specific voting station have voted.
The program has the following structure
Comments
#include
#include
//using namespace std;
const int vs=4;
int votesA, votesB, votesC, spoiltvotes;//TOTALS ALREADY INITIALISED-GLOBAL
char vote;
void main( )
{
clrscr();
// loop over the voting stations
for ( int i=1;i<=vs ;i++ )
{
// loop over voters
cout<<"
Enter your vote: ";
cin>>vote;
while( vote!='X')
{
switch(vote)
{
case 'A': votesA++;break;
case 'B':votesB++;break;
case 'C':votesC++;break;
default:spoiltvotes++;
}
cout<<"
Enter your vote: ";
cin>>vote;
}
}
// display the results in a neat presentable format
cout<<"
Number of votes for candidate
A:"<<votesA<<"
B:"<<votesB<<"
C:"<<votesC<<"
Number of spoilt votes: "<<spoiltvotes;
cout<<"
Incase you liked the process, you can always mail me at scrabble_92@yahoo.com";
getch();
}
/*OUTPUT:
Enter your vote: B
Enter your vote: B
Enter your vote: B
Enter your vote: A
Enter your vote: C
Enter your vote: X
Enter your vote: A
Enter your vote: B
Enter your vote: C
Enter your vote: D
Enter your vote: Y
Enter your vote: X
Enter your vote: B
Enter your vote: A
Enter your vote: C
Enter your vote: C
Enter your vote: B
Enter your vote: X
Enter your vote: A
Enter your vote: B
Enter your vote: X
Number of votes for candidate
A:4
B:7
C:4
Number of spoilt votes: 2
Incase you liked the process, you can always mail me at scrabble_92@yahoo.com
*/