Skip to main content

A C program for electronic voting machine!

Hello guys,
          Today I would like to share with all of you, my first mini project which I had done a few years ago when I was a beginner to the Programming world.
          This is a simple C program which mainly illustrates the file handling operations in C. I have explained the code with the comments.
          I hope you like it. :)

//        CODE         BEGINS

/*C program for smart voter*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

#define VTR_MAX_CNT 5 /*      Maximum number of votes to be casted
Depends on the User           */

void cast_vote();     /*Declaring function to cast vote*/

void winner();          /*Function to declare the winner*/

/*          Variable to check the repetition of voter's ID,
                  if the entered Voter's ID exists
*/

char repeat[10][30];

int FLAG=0;     /*Boolean variable to note down the existence of
voter's ID*/

/*A variable to keep track of total number of votes casted*/
int tot_vte=0;

/*Party variables all initialized to zero*/

int PARTY1 = 0,PARTY2 = 0,PARTY3 = 0,PARTY4 = 0,PARTY5 = 0;

/*          The following variables are used in order to
            check the existence of voter's ID
*/

char str1[20],str2[20];
char a[20],b[10][20];
int n[10]={0};

int r=0;    /* This variable is used as an index for the repeat     variable*/

FILE *ptr;        /*File pointer of the file containing the
                        list of voter's ID of that zone*/


/*    Function main begins    */
int main()
{
int i=0,k;  /* Index variables*/
int terminate;

ptr=fopen("vtrid.txt","r");   /*    Opening the file which contains
Voters IDs  */

/*
      A do while loop to:
            1) Check the existence of the voter's ID.
            2) Check for repetition of the entered voter's ID.
            3) To call the function which casts vote
*/


do
{           /*    DO WHILE OPENS         */

//clrscr(); Use this function in case you’re using Turbo C

/*    A LABEL associated with a goto statement, in case the entered voter's ID does not exist or
            if it is entered wrongly
*/

RE_ENTER:

/*    Voter's ID is being entered   */

printf("\nEnter the Voter's ID\n");
gets(str1);




/*    Process of checking for the existance of file   */

if(ptr==NULL)
      {
      printf("File not found\n");
      exit(0);
      }
else
    {
    printf("File found\n");
      }


/*    Process of copying the contents of the
             file containing voter's ID
*/

while(!feof(ptr))
{
fscanf(ptr,"%s",str2);
strcpy(b[i],str2);
i++;
}

strcpy(a,str1);



/*Checking for existence of the entered voter's ID*/

for(i=0;i<VTR_MAX_CNT;i++)
      {
      n[i]=strcmp(a,b[i]);
      }

for(i=0;i<VTR_MAX_CNT;i++)
      {
      if(n[i]==0)
            {
            FLAG=1;           /*Entered voter's ID exists*/
            break;
            }
      else
            {
            FLAG=0;           /*Entered voter's ID either does not exist or
                              if it is incorrect*/
            }
      }

/*    IF THE ENTERED VOTER'S ID EXISTS    */

if(FLAG==1)
      {     /* Check for repetition of the entered voter's ID     */

    if(tot_vte>0)
        {

                for(k=0;k<VTR_MAX_CNT;k++)
                        {
                              if(strcmp(str1,repeat[k])==0)
                    {
                        printf("This voter's ID is repeated\a\t\t\a\t\t\a");goto RE_ENTER;
                    }

                        }

            }


      printf("Voter's ID found");
      cast_vote();         /*Casting vote*/

      }
else            /*If the entered Voter's ID is invalid      */
      {
printf("Voter's ID not found\n\a\a");

      }

i=0;
strcpy(repeat[r],str1);
r++;

fflush(stdin);


}/*   DO WHILE CLOSES*/

while(tot_vte<VTR_MAX_CNT);

/*  The do-while continues until
    all the votes are casted or,
      The program is terminated forcefully
*/

printf("Thank you very much for voting\n");
printf("The voting process in this zone is complete\n");
winner();

getch();
fclose(ptr);

return 0;
}



/*Defining Cast Vote function*/

void cast_vote()
{
int choice;
printf("\nPlease cast your precious votes to one of the following parties\n");
printf("1.PARTY1\n2.PARTY2\n3.PARTY3\n4.PARTY4\n5.PARTY5\n");
scanf("%d",&choice);

//Increment the value of the respective party variable
switch(choice)
{
case 1:printf("Your Precious vote has been casted to PARTY1\n");
      PARTY1++;
      tot_vte++;
      break;

case 2:printf("Your Precious vote has been casted to PARTY2\n");
      PARTY2++;
      tot_vte++;
      break;

case 3:printf("Your Precious vote has been casted to PARTY3\n");
      PARTY3++;
      tot_vte++;
      break;

case 4:printf("Your Precious vote has been casted to PARTY4\n");
      PARTY4++;
      tot_vte++;
      break;

case 5:printf("Your Precious vote has been casted to PARTY5\n");
      PARTY5++;tot_vte++;
      break;

default:printf("Please re-enter your vote\n");
      cast_vote();
}

return; //Return the control back to the main() function
}

/*
      DEFINING THE FUNCTION TO DECLARE WINNER AND
      DISPLAY THE TOTAL NUMBER OF VOTES PER PARTY
*/

void winner()
{

/*    Displaying the total number of votes per party  */
printf("The total number of votes for PARTY1 is %d\n",PARTY1);
printf("The total number of votes for PARTY2 is %d\n",PARTY2);
printf("The total number of votes for PARTY3 is %d\n",PARTY3);
printf("The total number of votes for PARTY4 is %d\n",PARTY4);
printf("The total number of votes for PARTY5 is %d\n",PARTY5);

/*    Displaying the winner   */
//It is goof to use any sorting algorithm such as Quick or Merge sort
//can be used in order to find the winner instead of this approach

printf("PARTY2RATULATIONS!!!\a\n");
if(PARTY1>PARTY2 && PARTY1>PARTY3 && PARTY1>PARTY4 && PARTY1>PARTY5) printf("The winner is PARTY1!!\n");
else if(PARTY2>PARTY1 && PARTY2>PARTY3 && PARTY2>PARTY4 && PARTY2>PARTY5) printf("The winner is PARTY2!!\n");
else if(PARTY3>PARTY1 && PARTY3>PARTY2 && PARTY3>PARTY4 && PARTY3>PARTY5) printf("The winner id PARTY3!!\n");
else if(PARTY4>PARTY1 && PARTY4>PARTY2 && PARTY4>PARTY3 && PARTY4>PARTY5) printf("The winner is PARTY4!!\n");
else printf("The winner is PARTY5!!\n");

return;
}
/*    END OF THE PROGRAM      */




Comments

Popular posts from this blog

Kotlin :A New Beginning

Hello guys, You might have heard about a new language supported for the android Officially by Google,Named as Kotlin. Kotlin is a statically typed Programming language.By statically typed language i mean that the type checking is done during complile time. It was developed in July 2011 by Jetbrains in Russia,In 2017 Google declared Kotlin as the official langauge for android. However the Traditional Java developers are sticking to java. New programmers who want to make android apps,Web apps can use Kotlin as a language of preference. With this Post i welcome you to the world of Kotlin. There is no good book for kotlin but there is the official documentation on Kotlin.I refer the official Kotlin documentation. The main aim of this post if to Introduce you Kotlin and its Uses and also how to write programs in Kotlin. The official documentation can be found on the website   KOTLIN I belive that this post will help you to write codes in Kotlin. So lets begin Fir...

Web programming part 2

Hello guys, I am back with another post on web programming. In the last post you guys had learnt how to create a simple HTML document. As i told you there are many tags that you might have to remember. In this Post i will be creating a small Login form using only HTML. By using only HTML i mean No styling will be given to the document. The purpose of this code is to demonstrate you tags like <form> and <label> you will also learn how to make a button in HTML. The code goes like this: < form  action ="/action_page.php" >      < label > < b > Username < /b > < /label >      < input  type ="text"  placeholder ="Enter Username" >      < label > < b > Password < /b > < /label >      < input  type ="password"  placeholder ="Enter Password" >      < button  type ="submit" ...

Using Java and Python simultaneously

Hi friends,                                                                                                                                    Firstly, I would like to thank you all for your love and support towards our blog.                   We here are happy to know that our previous posts on tutorials and mini projects have helped you a great deal.         So far many of you might have done some really amazing projects using a particular programming language. But in this post of mine, I would like to share with you my mini project for which I have collaborated two of the most am...