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 |
Difference between realloc() and free?
A file pointer always contains the __________ of the file
Why do we use class in oops?
What is the difference between a mixin and inheritance?
4. What do you mean by a prototype? Define analysis prototype
Hi friends I have experience of 6 months in website design and maintanence. Now i am looking for other IT jobs.. to switch platform. please post any interview you know in chennai.
What are the two different types of polymorphism?
What are the different forms of polymorphism??
Which language is not a true object oriented programming language?
what does exactly the linker do?
What is the difference between inheritance and polymorphism?
Name a typical usage of polymorphism