Create an Election System using C Language with Free Codes

Table of Contents

Introduction – Election System

Hello Guys, here we are going to create an Election System using C Language. This is Console based tinny project which boost your skill level. Because this give you a knowledge about how use the do-while loops and switch cases in C Language, how counting system works…

Feature of the Project

Here is the feature of our Election System Project.

  • Cast the Vote
  • Find Vote Count
  • Find Leading Candidate

Code::Blocks

For get the output of the Election System Project we used a Code::Blocks software. It is a beginner friendly and mostly used software by the C Language Developers.

C Language E-Book

Output – Election System Project

Explanation about Project

Here we explain the Project codes in Parts to understand the code briefly,

Part – 1 Preprocessor Directive

#include<stdio.h>

The lines Includes the standard input/output library in the program, which allows us to use the functions like ‘printf()’ and ‘scanf’.

Part – 2 Macro Definitions

#define CANDIDATE_COUNT
#define CANDIDATE1 "David Hull"
#define CANDIDATE2 "Kristin Canella"
#define CANDIDATE3 "Jim Brar"
#define CANDIDATE4 "Donald Truimph"

In this line we describe the define constants by using the ‘#define’ directive. In this “CANDIDATE_COUNT” is not a given value so it does not effect in this code. And the other constants define the names of four candidates.

Part – 3 Global Variables

int votesCount1=0, votesCount2=0, votesCount3=0, votesCount4=0, spoiledtvotes=0;

These variables store the counts of votes which is received by each candidate (‘votesCount1’ to ‘votesCount4’) and the count of spoiled (‘spoiledvotes’). They are initialized to be zero.

Part – 4 Functions Definition

Function – 1 : void castvote()
void castVote(){
int choice;
printf("\n\n ### Please choose your Candidate ####\n\n");
printf("\n 1. %s", CANDIDATE1);
printf("\n 2. %s", CANDIDATE2);
printf("\n 3. %s", CANDIDATE3);
printf("\n 4. %s", CANDIDATE4);
printf("\n 5. %s", "None of These");
printf("\n\n Input your choice (1 - 4) : ");
scanf("%d",&choice);
switch(choice){
    case 1: votesCount1++; break;
    case 2: votesCount2++; break;
    case 3: votesCount3++; break;
    case 4: votesCount4++; break;
    case 5: spoiledtvotes++; break;
    default: printf("\n Error: Wrong Choice !! Please retry");
             //hold the screen
             getchar();
}
printf("\n thanks for vote !!");
}

This Function is used to handles the process of votes casting. It prompts the user to choose the candidate or spoil the vote by selecting the “None of These”. Based the input of the user’s, it increments the corresponding vote count.

Function – 2 : void votesCount()
void votesCount(){
printf("\n\n ##### Voting Statics ####");
printf("\n %s - %d ", CANDIDATE1, votesCount1);
printf("\n %s - %d ", CANDIDATE1, votesCount2);
printf("\n %s - %d ", CANDIDATE1, votesCount3);
printf("\n %s - %d ", CANDIDATE1, votesCount4);
printf("\n %s - %d ", "Spoiled Votes", spoiledtvotes);
}

This Function is used for display the statistics of voting, including the total votes which is received by each candidate and the count of spoiled votes.

Function – 3 : getLeadingCandidate()
void getLeadingCandidate(){
    printf("\n\n  #### Leading Candiate ####\n\n");
    if(votesCount1>votesCount2 && votesCount1>votesCount3 && votesCount1 >votesCount4)
    printf("[%s]",CANDIDATE1);
    else if (votesCount2>votesCount3 && votesCount2>votesCount4 && votesCount2 >votesCount1)
    printf("[%s]",CANDIDATE2);
    else if(votesCount3>votesCount4 && votesCount3>votesCount2 && votesCount3 >votesCount1)
    printf("[%s]",CANDIDATE3);
    else if(votesCount4>votesCount1 && votesCount4>votesCount2 && votesCount4 >votesCount3)
    printf("[%s]",CANDIDATE4);
    else
    printf("----- Warning !!! No-win situation----");
}

The Function is used for identifies the leading candidate by comparing the vote counts of all candidates. It then prints the name of the candidate with the highest value. If there is tie or no votes have been cast yet, it displays a warning massage.

Part – 5 Main Function

int main()

The main function is the entry point of the program.

Menu Display and User Input
do {
    // Menu Display
    printf("\n\n ###### Welcome to Election/Voting 2019 #####");
    printf("\n\n 1. Cast the Vote");
    printf("\n 2. Find Vote Count");
    printf("\n 3. Find leading Candidate");
    printf("\n 0. Exit");

    // User Input
    printf("\n\n Please enter your choice : ");
    scanf("%d", &choice);

This section presents a menu-driven interface to the user, allowing them to choose from options such as casting a vote, viewing vote counts, identifying the leading candidate, or exiting the program.

Switch Statement
switch(choice)
{
    case 1: castVote(); break;
    case 2: votesCount(); break;
    case 3: getLeadingCandidate(); break;
    default: printf("\n Error: Invalid Choice");
}

Based on the user’s choice, the program calls the corresponding function. If the choice is not valid, it displays an error message.

Part – 6 Loop and Exit

} while(choice!=0);

The Program Continues the display the menu and accept user input until the user chooses to exit by entering ‘0’.

getchar();
return 0;

This section holds the screen (waits for a character input) and returns 0 to indicate successful execution of the program.

Complete Codes of Project

//This Project is Created by www.digitalcollegelibrary.com
#include<stdio.h>

#define CANDIDATE_COUNT

#define CANDIDATE1 "David Hull"
#define CANDIDATE2 "Kristin Canella"
#define CANDIDATE3 "Jim Brar"
#define CANDIDATE4 "Donald Truimph"

int votesCount1=0, votesCount2=0, votesCount3=0, votesCount4=0, spoiledtvotes=0;

void castVote(){
int choice;
printf("\n\n ### Please choose your Candidate ####\n\n");
printf("\n 1. %s", CANDIDATE1);
printf("\n 2. %s", CANDIDATE2);
printf("\n 3. %s", CANDIDATE3);
printf("\n 4. %s", CANDIDATE4);
printf("\n 5. %s", "None of These");

printf("\n\n Input your choice (1 - 4) : ");
scanf("%d",&choice);

switch(choice){
    case 1: votesCount1++; break;
    case 2: votesCount2++; break;
    case 3: votesCount3++; break;
    case 4: votesCount4++; break;
    case 5: spoiledtvotes++; break;
    default: printf("\n Error: Wrong Choice !! Please retry");
             //hold the screen
             getchar();
}
printf("\n thanks for vote !!");
}

void votesCount(){
printf("\n\n ##### Voting Statics ####");
printf("\n %s - %d ", CANDIDATE1, votesCount1);
printf("\n %s - %d ", CANDIDATE1, votesCount2);
printf("\n %s - %d ", CANDIDATE1, votesCount3);
printf("\n %s - %d ", CANDIDATE1, votesCount4);
printf("\n %s - %d ", "Spoiled Votes", spoiledtvotes);
}

void getLeadingCandidate(){
    printf("\n\n  #### Leading Candiate ####\n\n");
    if(votesCount1>votesCount2 && votesCount1>votesCount3 && votesCount1 >votesCount4)
    printf("[%s]",CANDIDATE1);
    else if (votesCount2>votesCount3 && votesCount2>votesCount4 && votesCount2 >votesCount1)
    printf("[%s]",CANDIDATE2);
    else if(votesCount3>votesCount4 && votesCount3>votesCount2 && votesCount3 >votesCount1)
    printf("[%s]",CANDIDATE3);
    else if(votesCount4>votesCount1 && votesCount4>votesCount2 && votesCount4 >votesCount3)
    printf("[%s]",CANDIDATE4);
    else
    printf("----- Warning !!! No-win situation----");



}

int main()
{
int i;
int choice;

do{
printf("\n\n ###### Welcome to Election/Voting 2019 #####");
printf("\n\n 1. Cast the Vote");
printf("\n 2. Find Vote Count");
printf("\n 3. Find leading Candidate");
printf("\n 0. Exit");

printf("\n\n Please enter your choice : ");
scanf("%d", &choice);

switch(choice)
{
case 1: castVote();break;
case 2: votesCount();break;
case 3: getLeadingCandidate();break;
default: printf("\n Error: Invalid Choice");
}
}while(choice!=0);

//hold the screen
getchar();

return 0;
}

Conclusion – Election System

So Here we learn how the Election System made and which function is used for which work in this project and how the flow of the Project Codes working.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top