How to report the winner in each county and print out bar graph for two candidates' total votes?

79 views Asked by At

This program is to tally the votes from total and each of four counties for two candidates.As well displaying the winner. The user has to input the names and the votes for each county and the candidates. I got the program working correctly with the goals above. But I wanted to take it further by displaying the winner of each county and print out the bar graph of each two candidates' total votes. To print out the bar graph, I will use PrintGraph but wasn't sure how to use the strings for it.

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int tier1(
    const std::string& c0,
    const std::string& c1,
    const std::vector<std::string>& counties);

int main()
{
    cout << "Welcome to the VoteTime Program. Please do what it tells you.\n";
    cout << "After you name the two candidates and four counties, make sure the votes doesn't go over 100 votes.\n";

    std::string c0;
    cout << "Please name the candidate 0. (For example: Bob)\n";
    std::getline(cin, c0);

    std::string c1;
    cout << "Please name the candidate 1. (For example: John)\n";
    std::getline(cin, c1);

    std::vector<std::string> counties;
    for (int i = 0; i < 4; ++i)
    {
        std::string county;
        cout << "Please name county #" << i << '\n';
        std::getline(cin, county);
        counties.push_back(county);
    }

    int return_val = tier1(c0, c1, counties);
    if (return_val < 0) // print an error
        return 0;
}

int tier1(
    const std::string& c0,
    const std::string& c1,
    const std::vector<std::string>& counties)
{
    int votes[8];
    int i, j, N; // variables
    int k = 0;
    for (i=0; i < counties.size(); i++)
    {
        cout << "county" << counties[i] << "\n"; // lists the 4 counties
        for (j=0; j<2; j++)
        {
            cout << "How many votes did the candidate " << j << " get?\n";
            N=0;
            cin >> N;
            votes[k++] = N;
        }
        if (votes[k-2] + votes[k-1] > 100) //checking if it goes over 100 votes
        {
            cout << "One of the counties has too many votes. Exiting!\n"; // Print an error
            exit(1);
        }
    }

    int candidateOneVotes = 0; //resetting 
    int candidateTwoVotes = 0;
    for (i = 0; i < 8; i = i + 2)
    {
        cout << votes[i] << "\n";
        cout << votes[i+1] << "\n";
        candidateOneVotes += votes[i];
        candidateTwoVotes += votes[i+1];
    }
    if (candidateOneVotes > candidateTwoVotes){
        cout << "The winner of the election is " << c0 << "\n";
    }
    else
    {
        cout << "The winner of the election is " << c1 << "\n";
    }
    cout << "Here is the voting results:\n";
    cout << c0 << " got ";
    cout << candidateOneVotes;
    cout << " votes\n ";
    cout << c1 << " got ";
    cout << candidateTwoVotes;
    cout << " votes\n";

    //Program Completed 

    return 0;    
}
0

There are 0 answers