Write a C++ program to conduct an election of a
mayor.Declare a class ELECTION With the following
specification:
Data member:
Name 25 character
Age Integer
symbol 1 character
Member functions:
To accept data for 20 contestant
To accept symbol as voting from 100 voters.
To declare the winner and the loser.
Answer / v.ashwin kumar
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#define ESC 27
#define BKSP 8
#define NWL 13
class candidate {
public:
char symbol;
char name[21];
int votes;
void input();
void votescr(){
cout<<"\nPress "<<symbol<<" to vote for "<<name<<"!\n";
}
void vote();
void result(){
cout<<"\n"<<name<<" ("<<symbol<<") : "<<votes<<" votes!\n";
}
};
void candidate::input(){
cout<<"\nEnter the name of the candidate :\n";
// cin.get(name,20);
int i,x,y; char ch;
for(i=0;i<20;i++){
x=wherex(),y=wherey();
ch=getch(); cout<<ch;
if(ch!=NWL&&ch!=BKSP) name[i]=ch;
if(ch==BKSP&&i>0){gotoxy(x-1,y);cout<<" ";gotoxy(x-1,y);i-=2;}
if(ch==NWL) break;
}
name[i]='\0';
cout<<"\n\nEnter the symbol associated for voting : ";
symbol=getch(); cout<<symbol<<"\n";
cout<<"\nEnter the number of initial votes (0 to begin) : ";
cin>>votes;
}
void candidate::vote(){
cout<<symbol<<"\n\nAre you sure you wish to vote for "<<name<<" (y/n) : ";
char ch;
do{ch=getch();}while(ch!='y'&&ch!='Y'&&ch!='n'&&ch!='N');
if(ch=='y'||ch=='Y'){
cout<<ch<<"\n\nYou have voted for "<<name<<"!";
votes++;
}
getch();
}
void main(){
// declarations
char ch,v=ESC;
int i,j,k,n;
candidate c[10];
fstream f;
start:
clrscr();
cout<<"VOTE COUNTING PROGRAM FOR ELECTIONS - Kaustubh Karkare\n\n\n";
cout<<"Press the letter associated with the action you wish to perform:\n\n";
cout<<"I - Initialize Base Program Values,\n\n";
cout<<"V - Begin the Voting Process,\n\n";
cout<<"R - See the results of the voting process until now,\n\n";
cout<<"X - Exit the program!\n\n\n";
do{ch=getch();}while(ch!='i'&&ch!='I'&&ch!='v'&&ch!='V'&&ch!='r'&&ch!='R'&&ch!='x'&&ch!='X');
if(ch=='i'||ch=='I'){
f.open("records",ios::out|ios::binary);
if(!f) cout<<"Error!";
else {
cout<<"WARNING : All previously created records shall be lost upon proceeding ...\n\n";
cout<<"Enter '#' if you are sure you wish to continue ... ";
cin>>ch;
if (ch!='#'){ cout<<"\nInitialization process cancelled! Press any key to continue!"; getch(); goto start;}
cout<<"\nInitializing base program values ...\n\n";
cout<<"Enter the number of candidates (maximum 10) : ";
do{cin>>n;}while(n<1||n>10);
f.write((char*)&n,sizeof(n));
for(i=0;i<n;i++){
cout<<"\nDetails of Candidate-"<<i+1<<"...\n";
c[i].input();
f.write((char*)&c[i],sizeof(c[i]));
}//for
cout<<"\n\nInitialization process complete!\n\n";
}//else
f.close();
getch();
goto start;
}//if
else if(ch=='v'||ch=='V'){
cout<<"Press the symbol/letter associated with the candidate you wish to vote for.\n\n";
cout<<"Press ESC to stop the voting cycle.\n\n";
cout<<"Beginning the voting process ... press any key to continue!\n\n\n";
getch();
do{
f.open("records",ios::in|ios::out|ios::binary);
if(!f){cout<<"\n\nError!";break;}
else {
f.read((char*)&n,sizeof(n));
for(i=0;i<n;i++) f.read((char*)&c[i],sizeof(c[i]));
clrscr();
cout<<"VOTE COUNTING PROGRAM FOR ELECTIONS - V.ASHWIN KUMAR\n\n\n";
for(i=0;i<n;i++)c[i].votescr();
cout<<"\n\nPress the symbol/letter associated with the candidate you wish to vote for ... ";
retry: v=getch();
if(v==ESC){f.close();break;}
for(j=0;j<n;j++){ if(c[j].symbol==v){c[j].vote();break;} }
if(j==n)goto retry;
f.seekp(0);
f.write((char*)&n,sizeof(n));
for(i=0;i<n;i++) f.write((char*)&c[i],sizeof(c[i]));
}//else
f.close();
} while (v!=ESC);
cout<<"\n\nVoting Process Terminated!\n\n";
getch();
goto start;
}//if
else if(ch=='r'||ch=='R'){
f.open("records",ios::in|ios::binary);
if(!f){cout<<"\n\nError!"; getch();}
else {
f.read((char*)&n,sizeof(n));
cout<<"The results of the voting process till now are ...\n\n";
for(i=0;i<n;i++){
f.read((char*)&c[i],sizeof(c[i]));
c[i].result();
}//for
f.close();
getch();
}//else
goto start;
}//else
else {
cout<<"\n\nClosing the Program! Press any key to continue ...";
getch();
}//else
}//main
Is This Answer Correct ? | 12 Yes | 8 No |
What is a friend function & its advantage?
what is different between oops and c++
Write a macro for swapping integers
What do you mean by inline function?
write a program that takes input in digits and display the result in words from 1 to 1000
difine hierarchical inheritance.
What is debug class?what is trace class? What differences are between them? With examples.
What is a mixin class?
in the following, A D B G E C F Each of the seven digits from 0,1,2,3,4,5,6,7,8,9 is: a)Represented by a different letter in abov fig: b)Positioned in the fig abov so tht A*B*C,B*G*E,D*E*F are equal. wch does g represents? C
What are main features of oop?
DIFFRENCE BETWEEN STRUCTURED PROGRAMING AND OBJCET ORIENTED PROGRAMING.
What is abstrac class?where is it use?