Skip to content
Tutorials Center
My learning

Topics

Lesson 125 of 135 ยท Projects

Project: Online Voting System

Build a console voting system in C: register candidates with unique symbols, accept one vote per voter ID, count ballots and announce the winner or a tie.

An election program is a small lesson in data integrity. The interesting rules are not about printing: a voter may vote only once, a candidate needs a unique symbol, a ballot for a candidate that does not exist must be rejected, and the result must handle a tie honestly. In this project you will model that with structures, arrays and a few small functions, and you will test it with scripted input so the output is the same on every run.

Real voting software needs far more (secrecy, audit trails, identity checks), so treat this as a model of the counting logic only.

Design: the data first

Three pieces of state describe the whole election:

  • Candidates. A structure with a name, a one-character symbol (the thing a voter picks on the ballot) and a vote count. An array of them plus a counter candidate_count holds the field.
  • Voters who have voted. An array of voter IDs, so a second attempt with the same ID can be refused. For a few hundred voters, a linear search is fine. For a large election you would use a hash table.
  • Counters for invalid ballots (an unknown symbol) and refused repeat votes, because an honest result report shows them.

Each rule becomes one function that returns a clear status instead of printing, so the rules can be tested without a terminal:

TEXT
add_candidate(name, symbol)  -> OK, or error (full, empty name, symbol taken)
cast_vote(voter_id, symbol)  -> VOTE_OK, VOTE_REPEAT, VOTE_UNKNOWN, VOTE_FULL

Returning an enum rather than a bare int makes the callers readable: if (cast_vote(id, 'B') == VOTE_REPEAT) says what it means. See enums.

Stage 1: candidates and ballots

The election below registers three candidates (and tries to register a fourth with a symbol that is already taken), then processes a scripted list of ballots that includes a repeat voter and an unknown symbol.

C Stage 1: registering candidates and casting votes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice the decision in cast_vote that an invalid symbol does not use up the voter's vote: voter 104 first typed X, was told it was invalid, and could then vote for L. Whether a mistyped ballot spoils the vote is a rule of the election, not a coding detail, so write it down and test it.

The order of the checks matters too. Checking for a repeat first means a repeat voter can never change anything, whatever they type.

Stage 2: announcing the result

Finding the winner sounds trivial, but a careless "largest so far" loop reports the first candidate with the top count and hides a tie. The right approach is two passes: find the highest count, then count how many candidates reach it. One candidate means a winner. More than one means a tie, and you list them all. Zero votes overall means there is no result at all.

This stage reads votes from standard input, one per line as voter_id symbol, so you can change the input and see the result change. The input box below is preset with a tie.

C Stage 2: reading ballots and announcing the result

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The report shows every count, how many ballots were thrown away and why, and names every leader. A tie rule (a runoff, a coin flip, an older candidate) is again a rule for the election, not for the code.

Ideas for extending it

  • Store results in a file after the count so they survive the program, using file handling.
  • Replace the linear search with a hash table of voter IDs.
  • Hash voter IDs before storing them, so the file does not contain raw IDs.
  • Add an admin mode protected by a password read without echo (platform-specific).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does cast_vote check for a repeat voter before looking at the symbol?

    Checking identity first guarantees that a voter who has already voted cannot alter any count.
  2. 2. What goes wrong if the winner is found with a single loop that keeps the first candidate with the highest count?

    You need to count how many candidates share the top score; otherwise ties are hidden.
  3. 3. Why return an enum from cast_vote instead of printing inside it?

    Separating the rule from the output keeps the logic reusable and testable.
  4. 4. What should happen when scanf("%d %c", ...) does not return 2?

    scanf returns the number of values it stored, so anything other than 2 means no full ballot was read.

Summary

  • Model the election with a candidate structure, an array of voter IDs that have voted, and counters for rejected ballots.
  • Put each rule in a function that returns a status; keep output in the caller.
  • Check identity before content, and decide whether an invalid ballot uses up the vote.
  • Find a winner with two passes (highest count, then number of leaders) so ties are reported.
  • Always report invalid and repeat counts next to the totals.

Next: Project: Telecom Billing System