├── ELECTION.H ├── MAIN.C └── README.md /ELECTION.H: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct currentValidID{ 7 | int year; 8 | char branch[6]; 9 | int totalVoters; 10 | }; 11 | typedef struct candidate{ 12 | int cid; 13 | char cname[20]; 14 | int votes; 15 | }CANDIDATE; 16 | 17 | //GLOBALS -------------------------------------------------------- 18 | struct currentValidID currentValidID; //stores current Valid user ID parameters 19 | CANDIDATE candidateArray[20]; //to store information all candidates 20 | int numberOfCandidates; //Total number of candidates standing for election 21 | char studentVotes[200]; //to store information of votes given by each student 22 | //---------------------------------------------------------------- 23 | 24 | //To extract year from userID -- For example, userID:2018btecs00064 year:2018 25 | int extractYear(char userID[15]) 26 | { 27 | int year=0; 28 | char tmp; 29 | for(int i=0;i<4;i++){ 30 | tmp=userID[i]; 31 | year=(year*10)+(tmp-48); 32 | } 33 | return year; 34 | } 35 | 36 | int extractRollNo(char userID[15]) 37 | { 38 | int rollno=0; 39 | char tmp; 40 | for(int i=9;i<14;i++){ 41 | tmp=userID[i]; 42 | rollno=(rollno*10)+(tmp-48); 43 | } 44 | return rollno; 45 | } 46 | 47 | //Will check whether the global branch code and inputed branch code is matching or not 48 | int checkBranchCode(char userID[15]) 49 | { 50 | char branchCode[6]; 51 | for(int i=4;i<9;i++){ 52 | branchCode[i-4]=userID[i]; 53 | } 54 | branchCode[5]='\0'; 55 | if(strcmp(branchCode,currentValidID.branch)==0) 56 | return 1; 57 | else 58 | return 0; 59 | } 60 | 61 | int authenticateAdmin(){ 62 | char username[15], password[6]; 63 | 64 | printf("\nEnter username: "); 65 | scanf("%s",username); 66 | if((strcmp(username,"Admin"))!=0) 67 | return 0; 68 | else 69 | { 70 | printf("Enter Password: "); 71 | int i=0; 72 | for(i=0;i<5;i++) 73 | { 74 | password[i]=getch(); 75 | printf("%c",'*'); 76 | } 77 | password[i]='\0'; 78 | if((strcmp(password,"admiN"))!=0) 79 | return 0; 80 | } 81 | return 1; 82 | } 83 | 84 | void banID(){ 85 | printf("\nCreating Banned.txt...\n"); 86 | FILE *fp=fopen("Banned.txt", "w"); 87 | if(fp==NULL){ 88 | printf("Error: Banned.txt not created.\n"); 89 | fclose(fp); 90 | return; 91 | } 92 | printf("Just Enter last roll no to ban\nPress 0 to exit... "); 93 | int input; 94 | while(1){ 95 | printf("\nEnter Number: "); 96 | scanf("%d",&input); 97 | if(input==0) 98 | break; 99 | studentVotes[input-1]='$'; 100 | fprintf(fp,"%d\n",input); 101 | } 102 | fclose(fp); 103 | printf("Created Successfully\n"); 104 | } 105 | 106 | void createCandidateFiles(){ 107 | printf("\nCreating candidate files...\n"); 108 | FILE *fp; 109 | char filename[20]; 110 | for(int i = 1;i <= numberOfCandidates; i++){ 111 | sprintf(filename,"candidate%d.txt",i); 112 | fp=fopen(filename,"w"); 113 | fprintf( 114 | fp,"0\n%s", 115 | candidateArray[i-1].cname 116 | ); 117 | fclose(fp); 118 | } 119 | printf("Created Files successfully\n"); 120 | } 121 | 122 | void deleteIllegalVote(char userID[15]) 123 | { 124 | FILE *fp,*fcp; 125 | char filename[20]; 126 | char line[20]; 127 | 128 | int location = extractRollNo(userID); 129 | sprintf(filename,"candidate%d.txt",candidateArray[studentVotes[location-1]-49].cid); 130 | candidateArray[studentVotes[location-1]-49].votes--; 131 | studentVotes[location-1]='0'; 132 | if ((fp = fopen(filename,"r")) == NULL) 133 | { 134 | printf("\nFile cannot be opened...Operation Failed"); 135 | return; 136 | } 137 | printf("\nDeleting in process...\n "); 138 | if ((fcp = fopen("tmp.txt","w")) == NULL) 139 | { 140 | printf("\nFile cannot be opened...Operation Failed"); 141 | return; 142 | } 143 | 144 | while (!feof(fp)) 145 | { 146 | fscanf(fp,"%s",line); 147 | fprintf(fcp,"%s\n",line); 148 | } 149 | fclose(fp); 150 | fclose(fcp); 151 | if ((fp = fopen(filename,"w")) == NULL) 152 | { 153 | printf("\nFile cannot be opened...Operation Failed"); 154 | return; 155 | } 156 | int numFromFile; 157 | char cnameFromFile[20]; 158 | fcp = fopen("tmp.txt","r"); 159 | fscanf(fcp,"%d",&numFromFile); 160 | fprintf(fp,"%d",numFromFile-1); 161 | fscanf(fcp,"%s",cnameFromFile); 162 | fprintf(fp,"\n%s",cnameFromFile); 163 | while(!feof(fcp)){ 164 | fscanf(fcp,"%d",&numFromFile); 165 | if(numFromFile!=location) 166 | fprintf(fp,"\n%d",numFromFile); 167 | } 168 | fclose(fp); 169 | fclose(fcp); 170 | remove("tmp.txt"); 171 | printf("\nVote deleted successfully\nPress any key to continue..."); 172 | getch(); 173 | } 174 | 175 | int getWinner(){ 176 | int maxV = -1; 177 | int winnerCid; 178 | for(int i = 0;i < numberOfCandidates; i++){ 179 | if(candidateArray[i].votes > maxV) { 180 | winnerCid = candidateArray[i].cid; 181 | maxV = candidateArray[i].votes; 182 | } 183 | else if(candidateArray[i].votes == maxV) { 184 | return -1; 185 | } 186 | } 187 | return winnerCid; 188 | } 189 | 190 | void initiateNewElection() 191 | { 192 | printf("\nNew Election Initiation:\n"); 193 | 194 | printf("\nElections for which Year: "); 195 | scanf("%d",¤tValidID.year); 196 | printf("Enter branch code:"); 197 | scanf("%s",currentValidID.branch); 198 | printf("Enter max roll no.:"); 199 | scanf("%d",¤tValidID.totalVoters); 200 | printf("Enter the no. of candidates:"); 201 | scanf("%d",&numberOfCandidates); 202 | 203 | for (int i = 0; i < currentValidID.totalVoters; i++) 204 | { 205 | studentVotes[i] = '0'; 206 | } 207 | 208 | for (int i = 0;i < numberOfCandidates; i++) 209 | { 210 | candidateArray[i].cid=i+1; 211 | printf("Enter name of candidate %d: ",i+1); 212 | scanf(" %s",candidateArray[i].cname); 213 | candidateArray[i].votes=0; 214 | } 215 | return; 216 | } 217 | 218 | void saveElectionInfoInFile(){ 219 | printf("Saving Election Info in File...\n"); 220 | FILE *fp = fopen("ElectionInfo.txt", "w"); 221 | if(fp==NULL) 222 | { 223 | printf("\nError in file creation\n"); 224 | fclose(fp); 225 | return; 226 | } 227 | fprintf( 228 | fp,"%d\n%s\n%d\n%d", 229 | currentValidID.year, 230 | currentValidID.branch, 231 | currentValidID.totalVoters, 232 | numberOfCandidates 233 | ); 234 | fclose(fp); 235 | printf("Saved Successfully : )"); 236 | } 237 | 238 | void loadElectionInfoFromFile() 239 | { 240 | FILE *f1,*f2,*f3; 241 | f1=fopen("ElectionInfo.txt","r"); 242 | if(f1==NULL) 243 | printf("Not Exist"); 244 | fscanf(f1,"%d",¤tValidID.year); 245 | fseek(f1,2,SEEK_CUR); 246 | fscanf(f1,"%s",currentValidID.branch); 247 | fseek(f1,2,SEEK_CUR); 248 | fscanf(f1,"%d",¤tValidID.totalVoters); 249 | fseek(f1,2,SEEK_CUR); 250 | fscanf(f1,"%d",&numberOfCandidates); 251 | fclose(f1); 252 | 253 | //load candidates info and student votes 254 | for (int i = 0; i < currentValidID.totalVoters; i++) 255 | { 256 | studentVotes[i] = '0'; 257 | } 258 | for(int i=1;i<=numberOfCandidates;i++) 259 | { 260 | int location; 261 | char filename[20]; 262 | sprintf(filename,"candidate%d.txt",i); 263 | f2=fopen(filename,"r+"); 264 | candidateArray[i-1].cid=i; 265 | fscanf(f2,"%d",&candidateArray[i-1].votes); 266 | fscanf(f2,"%s",candidateArray[i-1].cname); 267 | while(!feof(f2)){ 268 | fscanf(f2,"%d",&location); 269 | studentVotes[location-1] = i+48; 270 | } 271 | fclose(f2); 272 | } 273 | 274 | //load banned votes 275 | int location; 276 | f3=fopen("banned.txt","r+"); 277 | while(!feof(f3)){ 278 | fscanf(f3,"%d",&location); 279 | studentVotes[location-1] = '$'; 280 | } 281 | fclose(f3); 282 | 283 | } 284 | 285 | void adminPanel() 286 | { 287 | while(1){ 288 | 289 | if(authenticateAdmin()!=1){ 290 | printf("\n Wrong Username or Password \n"); 291 | break; 292 | } 293 | 294 | printf("\n\nLOGGED IN SUCCESSFULLY (Press Enter)"); 295 | getch(); 296 | 297 | while(1) 298 | { 299 | char inputID[15]; 300 | char input;char banInp; 301 | int WinnerCid, totalVotedNow=0; 302 | printf("\n1.New Election\n2.Continue Previous Election\n3.Delete Illegal Vote\n4.Ban User IDs\n5.Result\n6.Logout\nOption:"); 303 | scanf(" %c",&input); 304 | 305 | switch(input) 306 | { 307 | case '1': 308 | initiateNewElection(); 309 | saveElectionInfoInFile(); 310 | createCandidateFiles(); 311 | break; 312 | case '2': 313 | loadElectionInfoFromFile(); 314 | break; 315 | case '3': 316 | printf("\nEnter user ID to delete its vote: "); 317 | scanf("%s",inputID); 318 | deleteIllegalVote(inputID); 319 | break; 320 | case '4': 321 | printf("Do you want to ban particular ID's?\nPress 1 if yes or any other key to continue..."); 322 | scanf(" %c",&banInp); 323 | if(banInp=='1'){ 324 | banID(); 325 | } 326 | break; 327 | case '5': 328 | WinnerCid = getWinner(); 329 | if(WinnerCid != -1){ 330 | printf("\nWinner is %s with %d votes\n",candidateArray[WinnerCid-1].cname,candidateArray[WinnerCid-1].votes); 331 | } 332 | else{ 333 | printf("\nIts A TIE"); 334 | } 335 | printf("\nFull Result\n"); 336 | for(int i=0;i %d votes\n",candidateArray[i].cid,candidateArray[i].cname,candidateArray[i].votes); 339 | } 340 | printf("\nVoting Percentage: %d %%\n\n",(totalVotedNow*100)/currentValidID.totalVoters); 341 | break; 342 | case '6': 343 | return; 344 | default: 345 | printf("Invalid Option"); 346 | getch(); 347 | } 348 | 349 | } 350 | } 351 | 352 | }; 353 | 354 | 355 | int isValid(char userID[15]) 356 | { 357 | if(strlen(userID)!=14) 358 | return 0; 359 | 360 | int inputedYear=extractYear(userID); 361 | int inputedRollNo = extractRollNo(userID); 362 | 363 | if(inputedYear!=currentValidID.year || checkBranchCode(userID)!=1 || inputedRollNo>currentValidID.totalVoters) 364 | return 0; 365 | 366 | return 1; 367 | } 368 | 369 | int isVoted(char userID[15]) 370 | { 371 | int location=extractRollNo(userID); 372 | if(studentVotes[location-1]=='0') 373 | return 0; 374 | else 375 | return 1; 376 | } 377 | 378 | int isBanned(char userID[15]){ 379 | int location=extractRollNo(userID); 380 | if(studentVotes[location-1]=='$') 381 | return 1; 382 | else 383 | return 0; 384 | } 385 | 386 | void saveVote(char userID[15],char voteInput) 387 | { 388 | char filename[20]; 389 | sprintf(filename,"candidate%d.txt",voteInput-48); 390 | FILE *fp = fopen(filename,"r+"); 391 | int location=extractRollNo(userID); 392 | studentVotes[location-1]=voteInput; 393 | candidateArray[voteInput-49].votes++; 394 | fseek(fp, 0, SEEK_SET); 395 | fprintf(fp,"%d\n",candidateArray[voteInput-49].votes); 396 | fseek(fp, 0, SEEK_END); 397 | fprintf(fp,"\n%d",location); 398 | fclose(fp); 399 | } 400 | 401 | 402 | void studentPanel() 403 | { 404 | char userID[15]; 405 | char voteInput; 406 | while(1) 407 | { 408 | printf("\n\n To exit press 0"); 409 | printf("\n Enter user ID:"); 410 | scanf("%s",userID); 411 | if(strcmp(userID, "0")==0) 412 | return; 413 | if(isValid(userID)!=1) 414 | { 415 | printf("\n Invalid User ID(Press Enter)"); 416 | getch(); 417 | continue; 418 | } 419 | if(isBanned(userID)!=0) 420 | { 421 | printf("\nThis User ID is currently banned...\nContact Admin for the reason...(Press Enter to continue)"); 422 | getch(); 423 | continue; 424 | } 425 | if(isVoted(userID)!=0) 426 | { 427 | printf("\n Your PRN entered is already voted\n Contact Admin for furthur query"); 428 | getch(); 429 | continue; 430 | } 431 | printf("\n\n Candidates for election:"); 432 | for (int i = 0; i < numberOfCandidates; i++) 433 | { 434 | printf("\n %d. %s",i+1,candidateArray[i].cname); 435 | } 436 | printf("\n\n Your Vote(Enter Number):"); 437 | voteInput=getch(); 438 | printf("*"); 439 | if(voteInput-48 < 1 || voteInput-48 > numberOfCandidates) 440 | { 441 | printf("\nInvalid Vote\nTry Again..."); 442 | getch(); 443 | continue; 444 | } 445 | saveVote(userID,voteInput); 446 | printf("\n\nThanks for your precious vote(Press Enter)"); 447 | getch(); 448 | } 449 | }; 450 | -------------------------------------------------------------------------------- /MAIN.C: -------------------------------------------------------------------------------- 1 | #include"election.h" 2 | 3 | int main(){ 4 | while(1){ 5 | printf("\n\t\t\t 1.Student panel \n\t\t\t 2.Admin panel \n\t\t\t 3.Exit \n\t\t\t Option:"); 6 | char input; 7 | scanf(" %c",&input); 8 | 9 | switch(input){ 10 | case '1': 11 | studentPanel(); 12 | break; 13 | case '2': 14 | adminPanel(); 15 | break; 16 | case '3': 17 | return 0; 18 | default: 19 | printf("\nInvalid option"); 20 | getch(); 21 | } 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mini-Voting-System-Using-C-Language 2 | 3 | -> Allows us to set up a flexible and trustworthy voting system 4 | -> Applicable for large as well as small group of people e.g. a batch, a class. 5 | -> Keeps a record of every voting process. 6 | 7 | # How to use? 8 | ### You can fork or download the repo, Once you have both the files "MAIN.C" and "ELECTION.H", You just need to compile and run MAIN.C 9 | #### ( It's too easy, isn't it? ☺) 10 | ##### For Admin Panel Use Username: "Admin" Password: "admiN" 11 | 12 | # Below is the video to see demo of project 13 | ## Link: https://drive.google.com/file/d/17yFA8VC9chpWav8kXXIYQR4B55sf4W9b/view?usp=sharing 14 | 15 | ### Feel free to raise issues if you find some errors or want to clear your doubts... 16 | 17 | #### #mini project #voting system #c project 18 | --------------------------------------------------------------------------------