#include
#include
#include
#include
#include
#include
int admin();
void greeting();
int get_candidate();
int register_candidate();
int get_schedule();
int schedule_election();
int register_voter();
int get_voter();
int search_voter();
void delete_candidate();
void update_candidate();
void update_voter();
void delete_voter();
void change_pass();
void cast_vote();
int Voting_Result();
int isValidName(char *name);
int isValidDate(const char *date);
int isAgeValid(const char *dob);
int isStrongPassword(const char *pwd);
void main(){
system("color 70");
int choice; //choose the operation
int red_flag=0;// delaration of variable for main exit
char repeat='y';
int success;
while(red_flag==0) //runs the loop until it becomes true
{
exit:
system("cls");
greeting(); //function for banner
printf("\n");
printf("\t\t\t\t1. View Schedule \n");
printf("\t\t\t\t2. View Candidate \n");
printf("\t\t\t\t3. View Voter Details\n");
printf("\t\t\t\t4. Cast Vote\n");
printf("\t\t\t\t5. Admin Settings\n");
printf("\t\t\t\t6. Exit\n");
printf("\t\t\t\tPlease choose the action from 1 to 6:");
scanf("%d",&choice);
switch(choice){
case 1:
system("cls");
greeting();
get_schedule(); //shows the schedule of election
system("pause");
break;
case 2:
system("cls");
greeting();
get_candidate(); //shows the registered candidate's list
system("pause");
break;
case 3:
system("cls");
greeting();
get_voter(); //shows the registered voter's list
system("pause");
break;
case 4:
system("cls");
greeting();
fflush(stdin);
cast_vote();
system("pause");
break;
case 5:
system("cls");
greeting();
success=admin();
if(success==1){
while(red_flag==0){
top:
system("cls");
int ch;
greeting();
printf("\n\n");
printf("\t\t\t\t**********Admin Settings**********\n");
printf("\t\t\t\t1. Create Schedule\n");
printf("\t\t\t\t2. Candidate Registration \n"); //add new candidate
printf("\t\t\t\t3. Edit Candidate Details\n"); //update or delete options
printf("\t\t\t\t4. Voter Registration\n"); //add new voter details
printf("\t\t\t\t5. Edit Voter Details\n"); //update or delete options
printf("\t\t\t\t6. Search Voter Details\n");
printf("\t\t\t\t7. Vote Result\n");
printf("\t\t\t\t8. Exit\n");
printf("\t\t\t\tPlease Choose the action from 1 to 8:");
scanf("%d",&ch);
switch(ch){
case 1:
system("cls");
printf("**********Admin Settings**********\n");
fflush(stdin);
schedule_election(); //function for to create schedule
system("pause") ;
break;
case 2:
system("cls");
printf("**********Admin Settings**********\n");
fflush(stdin);
register_candidate(); //function for to add candidate
printf("\n");
system("pause");
break;
case 3:
while(red_flag==0)
{
system("cls");
printf("**********Edit Candidate**********\n");
printf("1. Update Candidate\n");
printf("2. Delete Candidate\n");
printf("3. Exit\n");
printf("Choose the action:");
scanf("%d",&ch);
switch(ch)
{
case 1:
system("cls");
fflush(stdin);
get_candidate();
update_candidate(); //function to update candidate details
system("pause");
break;
case 2:
system("cls");
fflush(stdin);
get_candidate();
delete_candidate();
system("pause");
break;
case 3:
goto top;
break;
}
}
case 4:
system("cls");
printf("**********Admin Settings**********\n");
fflush(stdin);
register_voter();
system("pause");
break;
case 5:
while(red_flag==0)
{
system("cls");
greeting();
printf("\t\t\t\t**********Edit voter**********\n");
printf("\t\t\t\t1. Update Voter detail\n");
printf("\t\t\t\t2. Delete Voter detail\n");
printf("\t\t\t\t3. Change Password\n");
printf("\t\t\t\t4. Exit\n");
printf("\t\t\t\t\nChoose the action:");
scanf("%d",&ch);
switch(ch)
{
case 1:
system("cls");
fflush(stdin);
get_voter();
update_voter(); //function to update voter details
system("pause");
break;
case 2:
system("cls");
fflush(stdin);
get_voter();
delete_voter(); //function to delete the voter details
system("pause");
break;
case 3:
system("cls");
fflush(stdin);
get_voter();
change_pass(); //function to change the password of voters
system("pause");
break;
case 4:
goto top;
break;
}
}
case 6:
system("cls");
search_voter(); //function for to search voter
system("pause");
break;
case 7:
system("cls");
fflush(stdin);
Voting_Result(); //function to show the winner of the election
printf("\n");
system("pause");
break;
case 8:
goto exit;
break;
}
}
}
break;
case 6:
red_flag=1;
break;
default:
printf("Invalid command");
}
}
}
//Admin Credentials Validation function
int admin(void){
printf("\n\t\t\t\t***********LOGIN***********\n");
int i=1;
while(i<=3){
char username[15];
char password[12];
printf("\t\t\t\tEnter your username: ");
scanf("%s",&username);
printf("\t\t\t\tEnter your password: ");
scanf("%s",&password);
if(strcmp(username,"pranika")==0){
if(strcmp(password,"123")==0){
printf("\n\t\t\t\tGreeting.Login Success!");
system("pause");
return 1;
}
else
{
printf("\n\t\t\t\tWrong password\n");
}
}
else
{
printf("\n\t\t\t\tUser doesn't exist\n");
}
i++;
}
system("pause");
return 0;
}
int isValidName(char *name) {
for (int i = 0; i < strlen(name); i++) {
if (!isalpha(name[i])) {
return 0; // Invalid name
}
}
return 1; // Valid name
}
int isValidDate(const char *date) {
// First, check the format "yyyy/mm/dd"
if (strlen(date) == 10 && date[4] == '/' && date[7] == '/') {
int year, month, day;
// Parse the date components
if (sscanf(date, "%d/%d/%d", &year, &month, &day) == 3) {
// Check the year range
if (year >= 1000 && year <= 9999) {
// Check the month range
if (month >= 1 && month <= 12) {
// Check the day range
if (day >= 1 && day <= 31) {
// Handle months with 30 days
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
return 0; // Invalid day for these months
}
// Handle February with leap year consideration
if (month == 2) {
int isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (day > 29 || (day == 29 && !isLeap)) {
return 0; // Invalid day for February
}
}
// If all checks pass, the date is valid
return 1;
}
}
}
}
}
// If any of the checks fail, return 0 indicating an invalid date
return 0;
}
// Function to schedule an election
int schedule_election() {
struct location {
char constituency[20];
char time[20];
} c;
int year, month, day;
char repeat = 'y';
FILE *fp;
// Open the file to append election schedules
fp = fopen("schedule.txt", "a");
if (fp == NULL) {
printf("Error: Could not open file!\n");
return 0;
}
while (repeat == 'y' || repeat == 'Y') {
system("cls");
// Get the constituency name and validate it
do {
printf("Enter the name of the constituency:\n");
scanf("%s", c.constituency);
if (!isValidName(c.constituency)) {
printf("Invalid constituency name. Please enter a name that contains only letters.\n");
}
} while (!isValidName(c.constituency));
// Get the date and validate it
do {
printf("Enter DOB (yyyy/mm/dd):\n");
scanf("%d/%d/%d", &year, &month, &day);
// Format the date string
sprintf(c.time, "%04d/%02d/%02d", year, month, day);
if (!isValidDate(c.time)) {
printf("Invalid date format. Please enter a valid date (yyyy/mm/dd).\n");
}
} while (!isValidDate(c.time));
// Write the constituency and date to the file
fprintf(fp, "\n%25s %25s", c.constituency, c.time);
// Ask the user if they want to add another schedule
printf("Do you want to add another schedule? (y/n)\n");
repeat = getche();
}
fclose(fp);
return 1;
}
//read schedule from file
int get_schedule(){
struct location{
char constituency[20];
char time[20];
};
struct location c;
FILE *fp;
printf("\n\n");
printf("\t\t\t\t\t +--------------------------------------------------------+\n");
printf("\t\t\t\t\t | Constituency\t | Date of Election (yyyy/mm/dd) |\n");
printf("\t\t\t\t\t +--------------------------------------------------------+\n");
fp=fopen("schedule.txt","r");
if(fp==NULL){
printf("\nNo details to view!");
return 0;
}
while(!feof(fp)){
fscanf(fp,"%s %s",c.constituency,c.time);
printf("\t\t\t\t\t | %-23s| %-30s|\n",c.constituency,c.time);
}
printf("\t\t\t\t\t +--------------------------------------------------------+\n");
fclose(fp);
}
//greeting function
void greeting()
{
printf("\t\t +---------------------------------------------------------------------------------------------+\n");
printf("\t\t | Welcome to the election 2025 |\n");
printf("\t\t +---------------------------------------------------------------------------------------------+\n");
}
int register_candidate()
{
struct candidate{
char name[30];
char surname[30];
char party[40];
char from[30];
}n;
FILE *fp;
char repeat='y';
int i=1;
char line[200];
fp=fopen("candidatelist.txt","r");
while(fgets(line,sizeof(line),fp))
{
i++;
}
fp=fopen("candidatelist.txt","a");
if(fp==NULL){
printf("No such directory!");
return 0;
}
while(repeat=='y'){
system("cls");
do {
printf("Enter the first name of the candidate:\n");
scanf("%s", n.name);
if (!isValidName(n.name)) {
printf("Invalid first name. Please enter a name that contains only letters.\n");
}
} while (!isValidName(n.name));
do {
printf("Enter the last name of the candidate:\n");
scanf("%s", n.surname);
if (!isValidName(n.surname)) {
printf("Invalid last name. Please enter a name that contains only letters.\n");
}
} while (!isValidName(n.surname));
printf("Enter the name of the party:\n");
scanf("%s",n.party);
printf("Enter the location of candidate:\n");
scanf("%s",n.from);
fprintf(fp,"%25d %25s %25s %25s %25s\n",i,n.name,n.surname,n.party,n.from);
i++;
printf("Do you want to add next(y/n):\n");
repeat=getche();
getch();
}
fclose(fp);
}
//read candidate details from file
int get_candidate(){
struct candidate{
char name[30];
char surname[30];
char party[40];
char from[30];
}n;
int i;
printf("\n\n");
printf("\t\t +------------------------------------------------------------------------------------------------+\n");
printf("\t\t | Serial Number | Name of Candidate | Political Party | Candidancy From |\n");
printf("\t\t +------------------------------------------------------------------------------------------------+\n");
FILE *fp;
fp=fopen("candidatelist.txt","r");
if(fp==NULL){
printf("\nNo details to view!");
return 0;
}
while(fscanf(fp,"%d %s %s %s %s",&i,n.name,n.surname,n.party,n.from)!=EOF)
{
printf("\t\t |%-15d|%-12s %-12s |%-26s|%-26s|\n",i,n.name,n.surname,n.party,n.from);
}
printf("\t\t -------------------------------------------------------------------------------------------------+\n");
fclose(fp);
}
int isAgeValid(const char *dob) {
int year, month, day;
if (sscanf(dob, "%d/%d/%d", &year, &month, &day) != 3) {
return 0; // Invalid format
}
// Get the current date
time_t t = time(NULL);
struct tm current = *localtime(&t);
int current_year = current.tm_year + 1900;
int current_month = current.tm_mon + 1;
int current_day = current.tm_mday;
int age = current_year - year;
if (current_month < month || (current_month == month && current_day < day)) {
age--;
}
return age >= 18;
}
int isStrongPassword(const char *pwd) {
int hasUpper = 0, hasLower = 0, hasDigit = 0, hasSpecial = 0;
if (strlen(pwd) < 8) {
return 0; // Password is too short
}
for (int i = 0; pwd[i] != '\0'; i++) {
if (isupper(pwd[i])) hasUpper = 1;
if (islower(pwd[i])) hasLower = 1;
if (isdigit(pwd[i])) hasDigit = 1;
if (ispunct(pwd[i])) hasSpecial = 1;
}
return hasUpper && hasLower && hasDigit && hasSpecial;
}
//Register or add voter details
int register_voter() {
struct voter {
char name[20];
char surname[20];
char dob[20];
char address[20];
char password[30];
int has_voted;
} v;
char repeat = 'y';
int var, year, month, day;
FILE *fp;
char code[14] = "NP00";
char str[4];
char line[1000];
srand(time(0));
var = rand() % 100 + 1;
sprintf(str, "%d", var);
strcat(code, str);
// Get the date and validate it
do {
printf("Enter DOB (yyyy/mm/dd):\n");
scanf("%d/%d/%d", &year, &month, &day);
// Format the date string
sprintf(v.dob, "%04d/%02d/%02d", year, month, day);
if (!isValidDate(v.dob)) {
printf("Invalid date format. Please enter a valid date (yyyy/mm/dd).\n");
}
} while (!isValidDate(v.dob));
// Check age validity
if (!isAgeValid(v.dob)) {
printf("Not eligible to vote!\n");
return 0; // Exit the function if age is less than 18
}
fp = fopen("voterlist.txt", "r");
int i = 1;
while (fgets(line, sizeof(line), fp)) {
i++;
}
fclose(fp);
fp = fopen("voterlist.txt", "a");
if (fp == NULL) {
printf("No such directory\n");
return 0;
}
system("cls");
printf("Your voterID is %s\n", code);
// Get and validate the first name
do {
printf("Enter the first name of the voter:\n");
scanf("%s", v.name);
if (!isValidName(v.name)) {
printf("Invalid first name. Please enter a name that contains only letters.\n");
}
} while (!isValidName(v.name));
// Get and validate the surname
do {
printf("Enter the surname of the voter:\n");
scanf("%s", v.surname);
if (!isValidName(v.surname)) {
printf("Invalid surname. Please enter a name that contains only letters.\n");
}
} while (!isValidName(v.surname));
printf("Enter your address:\n");
scanf("%s", v.address);
// Get and validate the password
do {
printf("Enter your password:\n");
scanf("%s", v.password);
if (!isStrongPassword(v.password)) {
printf("Invalid password. A strong password must be at least 8 characters long, contain uppercase and lowercase letters, digits,\nand special characters.\n");
}
} while (!isStrongPassword(v.password));
v.has_voted = 0;
fprintf(fp, "%20s %10s %10s %20s %20s %20s %20d\n", code, v.name, v.surname, v.dob, v.address, v.password, v.has_voted);
printf("Added successfully!\n");
fclose(fp);
return 1;
}
int get_voter() {
struct voter {
char name[20];
char surname[20];
char dob[20];
char address[20];
char password[30];
} v;
printf("\n\n");
printf("\t+-------------------------------------------------------------------------------------------------------------------------------+\n");
printf("\t| Serial Number |\t\tVoter Name | Date Of Birth | Address | Password |\n");
printf("\t+-------------------------------------------------------------------------------------------------------------------------------+\n");
char code[14];
char has_voted[2]; // Variable to store the has_voted value
FILE *fp;
fp = fopen("voterlist.txt", "r");
if (fp == NULL) {
printf("\nNo voter details to view.");
return 0;
}
while (fscanf(fp, " %s %s %s %s %s %s %s", code, v.name, v.surname, v.dob, v.address, v.password, has_voted) != EOF) {
printf("\t|%-20s |%-11s%-15s|%25s|%25s|\t\t*****\t\t|\n", code, v.name, v.surname, v.dob, v.address);
}
printf("\t+-------------------------------------------------------------------------------------------------------------------------------+\n");
fclose(fp);
}
int search_voter(){ // Function to search the voter
struct voter{
char name[20];
char surname[20];
char dob[20];
char address[20];
char password[30];
}v;
FILE *fp;
char sn[14],code[14];
int found=0;
fp=fopen("voterlist.txt","r");
printf("Enter the votecode to search : ");
scanf("%s",&sn);
while(fscanf(fp," %s %s %s %s %s %s",code,v.name,v.surname,v.dob,v.address,v.password)!=EOF){
if(strcmp(code,sn)==0){
found=1;
printf("\n\n");
printf("\t\t*******************Search Voter Detail*******************\n");
printf("\n\n");
printf("\t\tSerial Number : %s\n",code);
printf("\t\tVoter Name : %s\n",v.name);
printf("\t\tLast Name : %s\n",v.surname);
printf("\t\tDateOfBirth : %s\n",v.dob);
printf("\t\tAddress : %s\n",v.address);
printf("\t\tPassword : %s\n",v.password);
printf("\t\t_________________________________________________________\n");
}
}
if(!found){
printf("record not found!\n");
}
fclose(fp);
getch();
}
void delete_candidate(){ // function to delete the candidate
struct candidate{
char name[30];
char surname[30];
char party[40];
char from[30];
}n;
FILE *fp,*fp1;
char line[1000];
int i,g=0,j=1,sn,found=0;
fp=fopen("candidatelist.txt","r");
while(fgets(line,sizeof(line),fp))
{
g++; // counts the line
}
fclose(fp);
printf("Please enter the serial number to delete candidate: ");
scanf("%d",&sn);
fp=fopen("candidatelist.txt","r");
fp1=fopen("temp1.txt","w");
while(j<=g)
{
fscanf(fp,"%d%s %s %s %s",&i,n.name,n.surname,n.party,n.from);
if(sn==i)
{
found=1;
j++;
continue;
}
if(found==1)
{
i=i-1; // update the line number
}
fprintf(fp1,"%25d %25s %25s %25s %25s\n",i,n.name,n.surname,n.party,n.from);
j++;
}
fclose(fp);
fclose(fp1);
remove("candidatelist.txt");
rename("temp1.txt","candidatelist.txt");
printf("Candidate deleted successfully!");
getch();
}
void update_candidate(){ // function to update candidate
struct candidate{
char name[30];
char surname[30];
char party[40];
char from[30];
}n;
FILE *fp,*fp1;
char nname[50],nsurname[30],nparty[100],nfrom[50];
char line[200];
int i,g=0,j=1,sn,found=0;
fp=fopen("candidatelist.txt","r");
printf("Please enter the serial number to update:\n");
scanf("%d",&sn);
while(fgets(line,sizeof(line),fp))
{
g++;
}
fclose(fp);
fp=fopen("candidatelist.txt","r");
fp1=fopen("temp1.txt","w");
while(j<=g)
{
fscanf(fp,"%d %s %s %s %s",&i,n.name,n.surname,n.party,n.from);
if(sn==i)
{
j++;
do {
printf("Enter the new first name of the candidate:\n");
scanf("%s", nname);
if (!isValidName(nname)) {
printf("Invalid first name. Please enter a name that contains only letters.\n");
}
} while (!isValidName(nname));
do {
printf("Enter the new last name of the candidate:\n");
scanf("%s", nsurname);
if (!isValidName(nsurname)) {
printf("Invalid last name. Please enter a name that contains only letters.\n");
}
} while (!isValidName(nsurname));
printf("Enter new party:\n");
scanf("%s",nparty);
printf("Enter the new address:\n");
scanf("%s",nfrom);
fprintf(fp1,"%25d %25s %25s %25s %25s\n",sn,nname,nsurname,nparty,nfrom);
}
else{
fprintf(fp1,"%25d %25s %25s %25s %25s\n",i,n.name,n.surname,n.party,n.from);
j++;
}
}
fclose(fp);
fclose(fp1);
remove("candidatelist.txt");
rename("temp1.txt","candidatelist.txt");
getch();
}
void delete_voter() {
struct voter {
char name[20];
char surname[20];
char dob[20];
char address[20];
char password[30];
int has_voted;
} v;
FILE *fp, *fp1;
char code[14], input_code[14];
int found = 0;
// Open the original voter list file for reading
fp = fopen("voterlist.txt", "r");
if (fp == NULL) {
printf("Could not open voterlist.txt\n");
return;
}
// Create a temporary file to write the updated voter list
fp1 = fopen("temp.txt", "w");
if (fp1 == NULL) {
printf("Could not create temp.txt\n");
fclose(fp);
return;
}
// Prompt the user to enter the voter code for deletion
printf("Please enter the voter code to delete:\n");
scanf("%s", input_code);
// Read each record from the original file
while (fscanf(fp, " %s %s %s %s %s %s %d", code, v.name, v.surname, v.dob, v.address, v.password, &v.has_voted) != EOF) {
// If the voter code matches the input code, skip writing this record to the temporary file
if (strcmp(code, input_code) == 0) {
found = 1;
printf("Voter with code %s deleted successfully!\n", input_code);
continue;
}
// Write the non-deleted records to the temporary file
fprintf(fp1, "%s %s %s %s %s %s %d\n", code, v.name, v.surname, v.dob, v.address, v.password, v.has_voted);
}
fclose(fp);
fclose(fp1);
// Replace the original file with the updated temporary file
if (found) {
remove("voterlist.txt");
rename("temp.txt", "voterlist.txt");
} else {
printf("Voter code not found!\n");
remove("temp.txt"); // Clean up the temporary file if no deletion was made
}
getch();
}
void update_voter() {
struct voter {
char name[20];
char surname[20];
char dob[20];
char address[20];
char password[30];
int has_voted;
} v;
FILE *fp, *fp1;
char code[14], input_code[14];
char new_name[20], new_surname[20], new_dob[20], new_address[20], new_password[30];
int year, month, day;
int found = 0;
// Prompt user for the voter code to update
printf("Please enter the voter code to update the details:\n");
scanf("%s", input_code);
fp = fopen("voterlist.txt", "r");
if (fp == NULL) {
printf("No such directory\n");
return;
}
fp1 = fopen("temp.txt", "w");
if (fp1 == NULL) {
printf("Could not open temporary file\n");
fclose(fp);
return;
}
// Reading each record and updating if the voter code matches
while (fscanf(fp, "%s %s %s %s %s %s %d", code, v.name, v.surname, v.dob, v.address, v.password, &v.has_voted) != EOF) {
if (strcmp(code, input_code) == 0) {
found = 1;
// Get and validate the first name
do {
printf("Enter the new first name of the voter:\n");
scanf("%s", new_name);
if (!isValidName(new_name)) {
printf("Invalid first name. Please enter a name that contains only letters.\n");
}
} while (!isValidName(new_name));
// Get and validate the surname
do {
printf("Enter the new surname of the voter:\n");
scanf("%s", new_surname);
if (!isValidName(new_surname)) {
printf("Invalid surname. Please enter a name that contains only letters.\n");
}
} while (!isValidName(new_surname));
// Get and validate the date of birth
do {
printf("Enter new DOB (yyyy/mm/dd):\n");
scanf("%d/%d/%d", &year, &month, &day);
// Format the date string
sprintf(new_dob, "%04d/%02d/%02d", year, month, day);
if (!isValidDate(new_dob)) {
printf("Invalid date format. Please enter a valid date (yyyy/mm/dd).\n");
} else if (!isAgeValid(new_dob)) {
printf("Not eligible to vote! Age must be 18 or above. Please enter a valid birth date.\n");
}
} while (!isValidDate(new_dob) || !isAgeValid(new_dob));
// Get the new address
printf("Enter your new address:\n");
scanf("%s", new_address);
// Get and validate the new password
do {
printf("Enter new password:\n");
scanf("%s", new_password);
if (!isStrongPassword(new_password)) {
printf("Invalid password. A strong password must be at least 8 characters long, contain uppercase and lowercase letters, digits, and special characters.\n");
}
} while (!isStrongPassword(new_password));
// Write updated details to the temporary file
fprintf(fp1, "%s %s %s %s %s %s %d\n", code, new_name, new_surname, new_dob, new_address, new_password, v.has_voted);
printf("Voter details updated successfully!\n");
} else {
// Write the original details to the temporary file if not updating
fprintf(fp1, "%s %s %s %s %s %s %d\n", code, v.name, v.surname, v.dob, v.address, v.password, v.has_voted);
}
}
fclose(fp);
fclose(fp1);
// Replace the original file with the updated file
if (found) {
remove("voterlist.txt");
rename("temp.txt", "voterlist.txt");
} else {
printf("Voter code not found!\n");
remove("temp.txt"); // Clean up the temporary file if no update was made
}
getch();
}
void change_pass(){ //function to change the password
struct voter{
char name[20];
char surname[20];
char dob[20];
char address[20];
char password[30];
}v;
FILE *fp,*fp1;
char line[300];
char sn[15],code[14];
int g=0,j=1;
char password[20];
printf("Please enter the votercode to update your password:");
scanf("%s",&sn);
fp=fopen("voterlist.txt","r");
while(fgets(line,sizeof(line),fp))
{
g++;
}
fclose(fp);
fp=fopen("voterlist.txt","r");
fp1=fopen("temp.txt","w");
while(j<=g)
{
fscanf(fp," %s %s %s %s %s %s",&code,v.name,v.surname,v.dob,v.address,v.password);
if(strcmp(code,sn)==0)
{
j++;
do {
printf("Enter your password:\n");
scanf("%s", password);
if (!isStrongPassword(password)) {
printf("Invalid password. A strong password must be at least 8 characters long, contain uppercase and lowercase letters, digits,\nand special characters.\n");
}
} while (!isStrongPassword(password));
fprintf(fp1,"%20s %10s %10s %20s%20s %20s\n",sn,v.name,v.surname,v.dob,v.address,password);
printf("Password successfully changed!");
}else
{
fprintf(fp1,"%20s %10s %10s %20s %20s %20s\n",code,v.name,v.surname,v.dob,v.address,v.password);
j++;
}
}
fclose(fp);
fclose(fp1);
remove("voterlist.txt");
rename("temp.txt","voterlist.txt");
getch();
}
void cast_vote() {
struct candidate {
char name[30];
char surname[30];
char party[40];
char from[30];
} n;
struct voter {
char code[14];
char name[20];
char surname[20];
char dob[20];
char address[20];
char password[30];
int has_voted;
} v;
struct cast {
int sn;
char fname[40];
char lname[40];
char party[45];
char place[45];
} vote[1000];
FILE *fp, *fp1, *fpv, *ftemp;
int i = 0, cast = 0, lc = 0, j = 0;
char votercode[14], code[14], password[30];
printf("\n\n");
printf("\t\t +------------------------------------------------------------------------------------------------+\n");
printf("\t\t | Serial Number | Name of Candidate | Political Party | Candidancy From |\n");
printf("\t\t +------------------------------------------------------------------------------------------------+\n");
fp = fopen("candidatelist.txt", "r");
if (fp == NULL) {
printf("No such directory!");
return;
}
while (fscanf(fp, "%d %s %s %s %s", &i, n.name, n.surname, n.party, n.from) != EOF) {
lc++;
printf("\t\t |%-15d|%-12s %-12s |%-26s|%-26s|\n",i,n.name,n.surname,n.party,n.from);
}
printf("\t\t -------------------------------------------------------------------------------------------------+\n");
fclose(fp);
int vc[lc];
printf("Please enter your votercode: ");
scanf("%s", votercode);
printf("Please enter your password: ");
scanf("%s", password);
// Open voter list for reading
fpv = fopen("voterlist.txt", "r");
if (fpv == NULL) {
printf("No such directory!");
return;
}
// Create a temporary file to store the updated voter list
ftemp = fopen("temp_voterlist.txt", "w");
if (ftemp == NULL) {
printf("Error opening temporary file!");
fclose(fpv);
return;
}
int found = 0; // To check if the voter is found
while (fscanf(fpv, "%s %s %s %s %s %s %d", v.code, v.name, v.surname, v.dob, v.address, v.password, &v.has_voted) != EOF) {
if (strcmp(v.code, votercode) == 0 && strcmp(v.password, password) == 0) {
found = 1;
if (v.has_voted == 1) {
printf("You have already voted!\n");
fclose(fpv);
fclose(ftemp);
remove("temp_voterlist.txt");
return;
} else {
printf("Enter serial number of the candidate you want to vote for: ");
scanf("%d", &cast);
// Update the vote count
fp1 = fopen("votecount.txt", "r");
if (fp1 == NULL) {
fp1 = fopen("votecount.txt", "w");
fp = fopen("candidatelist.txt", "r");
while (fscanf(fp, "%d %s %s %s %s", &vote[j].sn, vote[j].fname, vote[j].lname, vote[j].party, vote[j].place) != EOF) {
vc[i] = 0;
if (cast == vote[j].sn) {
vc[i] = vc[i] + 1;
}
fprintf(fp1, "%d %s %s\t%d\n", vote[j].sn, vote[j].fname, vote[j].lname, vc[i]);
i++;
j++;
}
fclose(fp);
fclose(fp1);
printf("Voted successfully!\n");
} else {
FILE *ftemp_vote = fopen("temp_votecount.txt", "w");
while (fscanf(fp1, "%d %s %s %d", &vote[j].sn, vote[j].fname, vote[j].lname, &vc[j]) != EOF) {
if (cast == vote[j].sn) {
vc[j] = vc[j] + 1;
}
fprintf(ftemp_vote, "%d %s %s\t%d\n", vote[j].sn, vote[j].fname, vote[j].lname, vc[j]);
j++;
}
fclose(ftemp_vote);
fclose(fp1);
remove("votecount.txt");
rename("temp_votecount.txt", "votecount.txt");
printf("Voted successfully!\n");
}
// Update the voter’s has_voted status
v.has_voted = 1;
}
}
// Write the voter details to the temporary file
fprintf(ftemp, "%20s %10s %10s %20s %20s %20s %20d\n", v.code, v.name, v.surname, v.dob, v.address, v.password, v.has_voted);
}
fclose(fpv);
fclose(ftemp);
// Replace the old voter list with the updated one
remove("voterlist.txt");
rename("temp_voterlist.txt", "voterlist.txt");
if (!found) {
printf("Invalid votercode or password!\n");
}
getch();
}
int Voting_Result()
{
struct cast{
int sn;
char fname[40];
char lname[40];
char party[45];
char place[45];
}vote[1000];
FILE *fp;
int vc[100],a=0,i,j,temp2;
char temp1[20],temp[20];
printf("\t\t\t\t|ELECTION WINNER|\n\n");
printf("\t\t\t\t +-----------------------------------------------------+\n");
printf("\t\t\t\t | Serial Number | Candidate Name | Votecount|\n");
printf("\t\t\t\t +-----------------------------------------------------+\n");
fp=fopen("votecount.txt","r");
if(fp==NULL)
{
printf("No details to view!");
return 0;
}
while(fscanf(fp,"%d %s%s%d",&vote[a].sn,vote[a].fname,vote[a].lname,&vc[a])!=EOF)
{
printf("\t\t\t\t |%-15d| %-13s %-11s|%-10d|\n",vote[a].sn,vote[a].fname,vote[a].lname,vc[a]);
a++;
}
printf("\t\t\t\t +-----------------------------------------------------+\n");
fclose(fp);
for(i=0;i