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.





Write a C++ program to conduct an election of a mayor.Declare a class ELECTION With the following ..

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

Post New Answer

More OOPS Interview Questions

how much classes are used in c++

5 Answers  


What is polymorphism programming?

0 Answers  


The IT giant Tirnop has recently crossed a head count of 150000 and earnings of $7 billion. As one of the forerunners in the technology front, Tirnop continues to lead the way in products and services in India. At Tirnop, all programmers are equal in every respect. They receive identical salaries and also write code at the same rate. Suppose 14 such programmers take 14 minutes to write 14 lines of code in total. How long will in take 5 programmers to write 5 lines of code in total ?

6 Answers   TCS,


What is the use of unnamed namespaces in OOPS? The only advantage I know is that they dont need the scope resolution operator while accessing them. I want to know some other advantages of unnamed namespaces...

2 Answers  


how does a main() in C++ is different from main() in C?

7 Answers  


Will I be able to get a picture in D drive to the c++ program? If so, help me out?

0 Answers  


What are functions in oop?

0 Answers  


What are different types of JVM's? for example we use dalvik jvm for android then what about the remaining operating systems?

0 Answers   IBM,


where is memory for struct allocated? where is memory for class-object allocated? I replied for struct in stack and for class-object in heap. THen he asked if class has struct member variable what happens.class on heap and what about struct in that class? couldnt ans :( :-?

2 Answers   Infosys, Microsoft,


What do we mean by a hidden argument in C++?

1 Answers  


Difference between new operator and operator new

2 Answers  


difference between structure and union.

2 Answers   ADP, Convergys,


Categories