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

Intro to Python Programming - 2. Basic Output

Hello everyone!           Considering the lot of requests I received from my dear friends and viewers, I shall now start posting step by step tutorials for mastering Python programming. I proceed with the assumption that you have a basic foundation of the C programming language . If not much of it, at least having some idea about the type specifiers such as ‘%d for integer, %f for floating point variables etc.’ and also, some basic ideas about data types such as int , float , char etc. would be of great help. :)           Before we begin with the coding part of python, I want you to understand a few basic concepts, like: 1.      Python is scripting language. 2.      Python programs do not make use of statement terminators like semicolons ‘;’ as in C, C++ and Java. 3.      A basic Python program does not require any header ...

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...

Assembly code generator for non parenthesized arithmetic expressions using Python Programming Language.

Hi guys, I hope you like the code I'm posting. This program is written in Python Programming language and it converts the non parenthesized arithmetic expressions to Assembly level code. Try a = b + c / d to test the code. And this should be the expected output: The Genereated code is: LD R0 ,c LD R1 ,d DIV R0 ,R0 ,R1 LD R1 ,b LD R2 ,temp ADD R1 ,R1 ,R2 ST temp , R1 Please Note: Do include a space (" ") between the operators and operands. And keep it in your mind that this is a code generator. Hence please do try it with valid expressions only. Upgrading the code for implementing the 'if' and looping constructs like 'while' and 'for' and sharing the modified code with us and other coding enthusiasts is really very well appreciated. I shall make another post about the explanation of the code soon. Until then, please do comment on our post and share our link to other coding lovers like yourself! I ho...