Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

officer say me - i am offered to a smoking , then what can you say

2044


What is solid in oops?

1038


What is the point of polymorphism?

989


What is polymorphism explain?

1189


What are the three parts of a simple empty class?

1971


What is static modifier?

1122


What is the benefit of oop?

999


Can enum be null?

963


What are benefits of oop?

1081


Who invented oop?

1061


What does and I oop mean?

1099


How to improve object oriented design skills?

945


write a C++ program for booking using constructor and destructor.

2494


what is graphics

2402


Can we create object of interface?

1063