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 Posted / 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 |
Post New Answer View All Answers
can inline function declare in private part of class?
What are the data types in oop?
What is coupling in oops?
what are the different types of qualifier in java?
What is a superclass in oop?
What is overriding vs overloading?
What do you mean by Encapsulation?
What is interface in oop?
How to improve object oriented design skills?
What is the full form of oops?
What exactly is polymorphism?
Is oop better than procedural?
How Do you Code Composition and Aggregation in C++ ?
What is polymorphism explain its types?
What is polymorphism and why is it important?