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
Post a Comment