How to Write a C++ Program to Design a Voting Machine

Written by breann kanobi
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Write a C++ Program to Design a Voting Machine
Create a virtual voting booth in C++. (Digital Vision/Digital Vision/Getty Images)

Code for a voting machine varies in complexity, depending on the desired end result of your machine. The most basic machine consists of statements explaining integers, input, tabulating results and output. Writing an adequate C++ voting machine requires moderate knowledge of C++. If you simply want to use another person's code for poll-taking purposes, you can find open source voting machine code options online (see Resources).

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Design the basic elements of your program. The most basic voting program presents a user with at least two options to chose from and tabulates the results of the vote.

  2. 2

    Open your preferred C++ coding application, such as Text Edit, and select "Create New File."

  3. 3

    Write the header for the program:

    Void DisplayOptions()

    Create the integers for input and output. For example, write:

    "int InputVotes"

    This creates the integer that determines the votes.

  4. 4

    Write the display options. Start with the display text, such as "It's Time to Vote in a Poll."

    {

    void DisplayOptions()

    cout<<"It's Time to Vote in a Poll"<<endl<<endl"

    Write the poll question, such as "What is Your Favorite Fruit."

    cout<<"What is Your Favorite Fruit?"<<endl<<endl"

    Then list the three options.

    out<<"Type 1 for Apples"<<endl<<endl"

    out<<"Type 2 for Oranges"<<endl<<endl"

    out<<"Type 3 for Strawberries"<<endl<<endl"

    }

  5. 5

    Create the array that tabulates the votes. Then, add a statement within the array that explains the loop's activities. Make your minimum a 1 and the maximum the number of options you intend to offer. In this example, the maximum is 3.

    {

    void main ()

    for

    (loopvalue=1; loopvalue <3; ++loopvalue)

    {VotesArray [loopvalue] = 1;}"

    Write the if and while statement that go with the for statement.

    if((option>=1 && (option<4))

    VotesArray(1)++;

  6. 6

    Create the code of the vote input by creating a switch.

    switch (option)

    {

    case 1:

    VotesArray[1]++;

    break;

    case 2;

    VotesArray[2]++;

    break;

    case 3:

    VotesArray[3]++;

    break;

    }}

  7. 7

    Close the code.

    return option;

    }

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.