Thursday, October 20, 2016

Electronic Polling System in C++ Language

Problem

Implement (a part of) electronic Pakistani election Scenario where following political parties are participating in election: PPP, PML and PTI.  Create a class PollingStation where it stores the vote’s count, against each party, cast by the Pakistani legal registered voters.  Your program must query the voter for its ID card number before he/she cast the vote and check it against voter list. (Suppose voter list is already with you). If voter is not found in the voter’s list, your program throws the exception IllegalVoter() and discard that voter. The Application again starts working in normal state and terminates only when user wants to quit it. At the end of the day (or whenever user wants) it generates report about the vote count against each party including header that contains date, day and location.

Code

// Poling station.cpp : Defines the entry point for the console application.
//
                      //HEADER FILES
#include "stdafx.h"   //ESSENTIAL TO WORK ON VISUAL STUDIO
#include<fstream>     //FOR FILE
#include<conio.h>     //FOR GETCH FUNCTION
#include<string>      //fOR STRINGS USAGE
#include<iostream>    //FOR COUT AND CIN
using namespace std;
                      //CLASS BEGINS
class PollingStation
{
private:            //VARIABLES DECLARED PRIVATLY, ONLY ACCESSABLE IN THIS CLASS
int votes_count_ppp;
int votes_count_pml;
int votes_count_pti;
int total_votes,hr,mn,sc,flag,choice,offset;
string line,day,location;

public:    //EXCEPTION CLASS
class IllegalVoter
{     };
     //CONSTRUCTOR OF POLLING STATION CLASS
    PollingStation(): votes_count_ppp(0), votes_count_pml(0), votes_count_pti(0),total_votes(0),
                 hr(0),mn(0),sc(0),flag(0),line(""),choice(0)
{ }
          //DESTRUCTOR OF POLLING STATION CLASS
~PollingStation()
{
cout<<"polling Station Destructor";
}
          //GETING THE DATA FROM USER(DAY,DTAE & LOCATION)
void start()
    {                  //GET DAY
         system("cls");
         cout<<"Enter Day: ";
         cin>>day;
                      //GET LOCATION
         system("cls");
         cout<<"\nEnter Location: ";
         cin>>location;
                       //GET TIME
         system("cls");
         cout<<"\nEnter time: "<<endl;
         cout<<"Enter Hour: ";
         cin>>hr;
         cout<<"Enter Minutes: ";
         cin>>mn;
    }
          //CHECKING WHETHER THE PERSON IS ILLEGIBEL FOR VOTE
    void checkdata(string search)
    {
         ifstream Myfile;    //OPENING THE FILE FOR READING
         Myfile.open ("VOTERS_LIST.txt");
         {
         if(Myfile.is_open())
         {                   //READING UNTIL END OF FILE
             while(!Myfile.eof())
             {
                 getline(Myfile,line);       //CHECKIMG
                 if ((offset = line.find(search)) != string::npos)
                 {
                      flag = 1;
                 }
             }
             Myfile.close();
         }   //IF FILE IS NOT OPENED
         else
             cout<<"Unable to open this file."<<endl;
         }
         if(flag == 0)     //THROWING EXCEPTION
                 throw IllegalVoter();
    }
          //PERSON WILL CAST HIS/HER VOTE
void cast_vote()
{
         system("cls");
         cout<<"CAST YOUR VOTE:"<<endl;
         cout<<"1. PPP"<<endl;
         cout<<"2. PML"<<endl;
         cout<<"3. PTI"<<endl;
             
         cout<<"\nEnter Your Choice: ";
         cin>>choice;
                     //IF USER HAS VOTED FOR PPP
         if(choice == 1)
         {
              votes_count_ppp++;
              total_votes++;
         }           //IF USER HAS VOTED FOR PML
         else if(choice == 2)
         {
              votes_count_pml++;
              total_votes++;
         }           //IF USER HAS VOTED FOR PTI
         else if (choice == 3)
         {
              votes_count_pti++;
              total_votes++;
         }          //IF USER HAS ENTERED WRONG CHOICE
         else
         {
             system("cls");
             cout<<"Invalid Key."<<endl;
             cout<<"Press Any Key To Continue."<<endl;
             getch();
         }
}
     //DISPLYING RESULTS
void display()
{
         system("cls");
         cout<<"\n\n********************************************************************************";
         cout<<"Time = "<<hr<<" : "<<mn<<"\t\t\tDay = "<<day<<"\t\tLocation = "<<location<<endl;
         cout<<"\n********************************************************************************";
       
         cout<<"Total number of votes voted for:"<<endl<<endl;
         cout<<"\tPPP = "<<votes_count_ppp<<endl;
         cout<<"\tPML = "<<votes_count_pml<<endl;
         cout<<"\tPTI = "<<votes_count_pti<<endl<<endl;
         cout<<"\tTotal = "<<total_votes<<endl;
     }
};

int main()
{
PollingStation p;   //OBJECT OF POLLING STATION CLASS
    char another;       //VARIABLES
    string id;        
   
    p.start();          //CALLING THE FUNTION TO GET DATA
                       
    do{                 //LOOP UNTIL USER WANTS
        try{
              system("cls");   //USER WILL ENTER HIS/HER ID CARD NUMBER
              cout<<"Enter ID Card Number: ";
     cin>>id;
     p.checkdata(id);     //CHECKING
     p.cast_vote();       //CASTING VOTE(IF ILLEGIBLE)
           }
        catch(PollingStation::IllegalVoter)  //IF NOT ILLEGIBLE THEN
          {
              cout<<"NO ENTRY WITH THIS ID"<<endl;
              cout<<"Press any key to continue...";
              getch();
          }
         
        system("cls");         //ASKING USER WHETHER HE/SHE WANTS TO ENTER DATA
        cout<<"Enter another phone number(y/n)? ";
        another = getch();
      }while(another == 'Y' || another == 'y'); //LOOP TERMINATION CONDITION
   
   
    ofstream file;   //OPENING FILE FOR WRITTING
    file.open("POLLING_RESULTS.DAT", ios::binary);
    p.display();     //DISPLYING THE DATA & WRITTING TO THE FILE
    file.write(reinterpret_cast<char*>(&p),sizeof(p));  //SO THAT IT COULD BE DSPLAYED ANYTIME
    file.close();                                       //USER WANTS & THEN CLOSING THE FILE
   
    cout<<"\nFile Written"<<endl;
    cout<<"Press any key to exit"<<endl;
getch();
return 0;
}

Output

electronic polling system in c++ language

1 comment :

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact