Open In App

Online Voting System in C

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Voting in elections is one of the most fundamental organs of any Democracy and in the modern world, voting is done through digital machines. In this article, we will try to write a simple Online Voting System using C programming language.

Features of Online Voting System in C

The online voting system offers the following functions:

  • Taking a Vote from the User
  • Storing Different Votes
  • Calculating Votes
  • Declaring Results

Components of Online Voting System

The Online Voting System program has the following components:

1. Header Files and Macro Definitions

  • This program makes use of only two standard header files <stdio.h> and <string.h>.
  • We have also defined a macro to represent the maximum number of candidates.

2. Structure with name “Candidate”

The structure with the name “Candidate” is defined to store the information of the candidate. It has the following fields:

  • name Array: This array of characters is used to store the name of the candidate.
  • symbol: This character variable stores the symbol allotted to the candidate.
  • votes: This variable is used to store the number of votes that the given candidate received.

3. Data Structures and Variables with Global Scope

Some of the data structures and variables are declared inside the global scope to avoid scope problems and make them available to all the functions in the program. These are:

  • allCandidate[]: This array stores all the candidates.
  • candidateCount: It is used to store the number of candidates.
  • symbol[]: This array is used to store all the symbols.
  • symbolTaken[]: Used to keep track of the symbols that are already allotted to some candidate.

4. Functions to Perform Different Tasks

Various functions are used in the program to provide modularity to the code. They are named based on the task they are performing:

  • fillCandidate(): This function is used to take user input about the candidate details and store them in the allCandidate[] array.
  • displayAllCandidates(): This function is used to display all available candidates while the voting is being performed.
  • getVotes(): This function is used to take the votes from the user and store them in the structure for the given candidate.
  • getResults(): After the voting, the getResults() function is used to calculate the votes and declare the results of the elections. 

C Program to Implement Online Voting System

C




#include <stdio.h>
#include <string.h>
// including necessary header files
  
#define MAX_C 11
// defining max number of candidates
  
// Structure of candidate
typedef struct Candidate {
    char name[50];
    int votes;
    char symbol;
} Candidate;
  
// global array of candidate details
Candidate allCandidates[MAX_C];
// global variable to keep the cound of candidates
int candidateCount = 0;
// global array to store all symbols
char symbols[10]
    = { '!', '@', '#', '$', '%', '^', '&', '*', '~', '+' };
// array to keep track of taken symbol
int symbolTaken[11];
  
// function declaration
void fillCandidate(int);
void displayAllCandidates();
void getVotes(int);
void getResults();
  
// driver code
int main()
{
    // initializing necessary data structures
    for (int i = 0; i < 11; i++) {
        symbolTaken[i] = 0;
    }
  
    // getting the number of candidates
    printf("Enter the number of candidates: ");
    scanf("%d", &candidateCount);
    if (candidateCount >= MAX_C) {
        printf("Number of candidates cannot be greater "
               "than 10.\n Terminating the program\n\n");
        return 0;
    }
  
    // filling the details of the candidate
    for (int i = 0; i < candidateCount; i++) {
        fillCandidate(i);
    }
  
    // getting the number of voters
    int numVoters;
    printf("Enter the number of voters: ");
    scanf("%d", &numVoters);
  
    // Collecting votes
    for (int i = 0; i < numVoters; i++) {
        getVotes(i);
    }
  
    // printing results
    getResults();
  
    return 0;
}
  
// function to populate the allCandidates array using the
// details provided by user
void fillCandidate(int cNum)
{
    printf("Available Symbols: \n");
    for (int j = 0; j < 10; j++) {
        if (symbolTaken[j] == 1)
            continue;
        printf("%d  %c\n", j + 1, symbols[j]);
    }
  
    int num = 0;
  
    printf("\nEnter the symbol number of candidate %d: ",
           cNum + 1);
    scanf("%d", &num);
  
    if (num <= 0 || num > 10 || symbolTaken[num - 1] == 1) {
        printf("This Symbol is not available. Please "
               "choose from the available symbols\n");
        num = 0;
        fillCandidate(cNum);
    }
    else {
        symbolTaken[num - 1] = 1;
        allCandidates[cNum].symbol = symbols[num - 1];
        printf("Enter the name of candidate %d: ",
               cNum + 1);
        scanf("%s", allCandidates[cNum].name);
  
        allCandidates[cNum].votes = 0;
    }
}
  
// function to display all candidates name with symbol
void displayAllCandidates()
{
    if (!allCandidates || !candidateCount) {
        perror("Invalid Candidate Array\n");
        return;
    }
  
    for (int j = 0; j < candidateCount; j++) {
        printf("%s\t\t", allCandidates[j].name);
    }
    printf("\n");
    for (int j = 0; j < candidateCount; j++) {
        printf("%3c\t\t\t", allCandidates[j].symbol);
    }
    printf("\n");
}
  
// function to get votes
void getVotes(int voterCount)
{
    displayAllCandidates();
    printf("Voter %d, please enter your choice (1-%d): ",
           voterCount + 1, candidateCount);
    int choice;
    scanf("%d", &choice);
  
    // checking for valid choice
    if (choice >= 1 && choice <= candidateCount) {
        allCandidates[choice - 1].votes++;
    }
    else {
        printf("Invalid choice! Please vote again.\n");
        getVotes(voterCount);
    }
}
  
// function to get results
void getResults()
{
    int maxVotes = 0;
    int winnerIndex = -1;
    int winnerFrequency = 0;
    for (int i = 0; i < candidateCount; i++) {
        if (allCandidates[i].votes > maxVotes) {
            maxVotes = allCandidates[i].votes;
            winnerIndex = i;
        }
    }
  
    for (int i = 0; i < candidateCount; i++) {
        if (allCandidates[i].votes == maxVotes) {
            winnerFrequency++;
        }
    }
  
    printf("\n-----RESULT-----\n");
  
    if (winnerFrequency > 1) {
        printf("No candidate has majority votes\n");
    }
    else if (winnerIndex != -1) {
        printf("The winner is: %s\nCandidate Symbol: "
               "%c\nwith %d votes!\n",
               allCandidates[winnerIndex].name,
               allCandidates[winnerIndex].symbol, maxVotes);
    }
    else {
        printf("No winner\n");
    }
}


Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads