You are on page 1of 8

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
char nam[10],dob[8];
char dummy[50];
//voter code starts here
struct voter_details
{
char name[10];
int adhaar_no;
char DOB[8];
int no_use;
char voted[2]; //1 signifies voted 0 signifies not voted
struct voter_details *next;

};

typedef struct voter_details voter;


voter *head;

void admin_disp(){
printf("#####################################################################\
n");
printf("## ADMIN ##\
n");
printf("#####################################################################\
n");
}

voter* voter_create(){
voter *newnode;
system("cls");
admin_disp();
newnode=(voter*)malloc(sizeof(voter));
printf("\n\tEnter the details of the voter\n");
printf("\tEnter the name of voter: ");
scanf("%s",&newnode->name);
printf("\tEnter the adhaar number(8-digit): ");
scanf("%d",&newnode->adhaar_no);
printf("\tEnter the DOB: ");
scanf("%s",&newnode->DOB);
newnode->next=NULL;
strcpy(newnode->voted,"NO");
return newnode;
}

voter* voter_add(){
int flg = 0;
voter *newnode;
if(head==NULL){
newnode=voter_create();
newnode->next=head;
head=newnode;
}else{
newnode = voter_create();
voter *p = head;
while(p != NULL){
if(p->adhaar_no == newnode->adhaar_no){
flg = 1;
break;
}
p = p-> next;
}
if(flg == 0){
newnode->next = head;
head = newnode;
}else{
printf("\nAadhaar number already in use.\n\tINVALID ENTRY\n");
free(newnode);
printf("\n\nEnter any character to continue: ");
scanf("%s",dummy);
}
}
return head;

voter* display()//display voters


{
voter *p;
p=head;
system("cls");
admin_disp();
printf("\n\tList of Voters:\n");
printf("\n");
printf("\tNAME\t\tAADHAAR\t\t\tDOB\t\t\tVoting\n");
while(p!=NULL){
printf("\t%s\t\t%d\t\t%s\t\t%s\n",p->name,p->adhaar_no,p->DOB,p->voted);
p=p->next;
}
printf("\n\nEnter any character to continue: ");
scanf("%s",dummy);
return head;
}

//voter code ends here

//candidate code starts here


struct candidate_details
{
char name[10];
int votes;
struct candidate_details *next1;

};

typedef struct candidate_details candidate;


candidate *head1;

candidate* candidate_create(){
candidate *newnode1;
newnode1=(candidate*)malloc(sizeof(candidate));
system("cls");
admin_disp();
printf("\n\tEnter the details of the candidate\n");
printf("\n\tEnter the name of candidate: ");
scanf("%s",&newnode1->name);
newnode1->votes=0;
newnode1->next1=NULL;
return newnode1;
}

candidate* candidate_add(){
candidate *newnode1;
if(head1==NULL){
newnode1=candidate_create();
newnode1->next1=head1;
head1=newnode1;
}else{
newnode1 = candidate_create();
newnode1->next1 = head1;
head1 = newnode1;
}
return head1;

candidate* display_cand()//display candidates


{
candidate *p;
p=head1;
system("cls");
admin_disp();
printf("\n\n--------List of Candidates-----------\n");
printf("\n\tName\t\tVotes\n");
while(p!=NULL){
printf("\t%s\t\t%d \n",p->name,p->votes);
p=p->next1;

}
return head1;
}

//candidate code ends here

void vote_results(){
int max_win=0,flg=0;
char win[10];
candidate *p;
p=head1;
system("cls");
admin_disp();
printf("\n");
display_cand();
while(p!=NULL){
if(p->votes>max_win){
strncpy(win,p->name,10);
max_win= p->votes;
}else if(p->votes==max_win){
flg = 1;
break;
}
p=p->next1;

}
printf("\n|------------------------------------------|\n");
printf("| RESULT |\n");
printf("|__________________________________________|\n");
if(flg == 0){
printf("\nwinner is %s with %d votes ",win,max_win);
}else{
printf("\nMultiple candidates have equal votes\n");
}
printf("\n\nEnter any character to continue: ");
scanf("%s",dummy);
}

void admin(int password)


{
int loop = 1,ch;
if (password==6969)
{
while(loop){
system("cls");
admin_disp();
printf("\n\t1.update voter database\n");
printf("\t2.Display list of voters\n");
printf("\t3.update candidate database\n");
printf("\t4.Diaply list of candidates\n");
printf("\t5.show vote results\n");
printf("\t6.Return to main menu.\n");
printf("Enter choice: ");
scanf("%d",&ch);
switch (ch)
{

case 1:
head = voter_add();
head = display();
break;
case 2:
head = display();
break;
case 3:
candidate_add();
display_cand();
printf("\n\nEnter any character to continue: ");
scanf("%s",dummy);
break;
case 4:
display_cand();
printf("\n\nEnter any character to continue: ");
scanf("%s",dummy);
break;
case 5:
vote_results();
break;
case 6:
loop = 0;
break;
default:
printf("invalid choice");
}
}
}

else{
printf("Incorrect password");
}

//Voting code
void pane_disp(){
printf("#####################################################################\
n");
printf("## WELCOME TO VOTING PANE ##\
n");
printf("#####################################################################\
n");
}

void casted(){
printf("#####################################################################\
n");
printf("## VOTE CASTED ##\
n");
printf("#####################################################################\
n");
}

void not_casted(){
printf("#####################################################################\
n");
printf("## VOTE NOT CASTED ##\
n");
printf("#####################################################################\
n");
}

voter* find_voter(){
long aadhar;
int confirm;
voter *p = head;
system("cls");
pane_disp();
printf("\nEnter your name as per aadhaar:\n");
scanf("%s",&nam);
printf("Enter aadhar number:\n");
scanf("%ld",&aadhar);
printf("Enter DOB as per aadhaar:\n");
scanf("%s",&dob);
printf("\nDetails entered are: \n%s\n%ld\n%s\n",nam,aadhar,dob);
printf("Enter 1 to confirm else enter 0:");
scanf("%d",&confirm);
if(confirm == 1){
while(p != NULL){
if(aadhar == p->adhaar_no){
if(strcmp(nam,p->name) == 0){
if(strcmp(dob,p->DOB)==0){
return p;
}
}
}
p = p->next;
}
}
return NULL;
}

int cast_vote(){
candidate* p = head1;
int ch=-1,count=1,confirm=0;
system("cls");
pane_disp();
printf("\n\tThe list of candidates are: \n\n");
printf("\tID\t\tNAME\n");
while(p != NULL){
printf("\t%d\t\t%s\n",count,p->name);
count += 1;
p = p->next1;
}
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("!!PLEASE BE CAUTIOUS WHILE CASTING YOUR VOTE!!");
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("\nEnter the candidate number to vote: \n");
scanf("%d",&ch);
if(ch > 0){
p = head1;
while(ch > 1 && p != NULL){
p = p->next1;
ch -= 1;
}
if(p == NULL){
printf("Invalid choice entered\n");
}
else{
printf("You have chosen to vote for %s\n",p->name);
printf("Enter 1 to confirm else 0 to recast: \n");
scanf("%d",&confirm);
if(confirm){
p->votes += 1;
printf("\n\n");
casted();
printf("\n\n");
printf("Enter any character to continue: ");
scanf("%s",dummy);
return 1;
}
}
}
return 0;
}

void vote(){
voter* p;
int loop=1,ch=1,vtd=0;
while(loop){
p = NULL;
system("cls");
pane_disp();
p = find_voter();
if(p == NULL){
system("cls");
printf("Invalid details.\n");
}else if(strcmp(p->voted,"OK")==0){
printf("Vote already cast.Contact admin if it was not you.\n");
}else{
vtd = cast_vote();
if(vtd == 1){
strcpy(p->voted,"OK");
strcpy(p->DOB,dob);
}else{
printf("\n\n");
not_casted();
printf("\n\n");
}
}
printf("\nEnter 1 to cast vote again else enter 0.\n");
scanf("%d",&ch);
if(ch == 0){
loop = 0;
}
}

void main(){

int ch=-1,passcode;
while(1){
system("cls");
printf("|
*********************************************************************|\n");
printf("| ELECTIONS 2023
|\n");
printf("|
_____________________________________________________________________|\n");
printf("\n\t1.To Vote\n\t2.Admin\n\t3.Exit\n\tEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
vote();
break;
case 2:
printf("Enter passcode: ");
scanf("%d",&passcode);
admin(passcode);
break;
case 3:
exit(0);
default:
printf("Invalid Choice.");
}
}
}

You might also like