如何报告每个县的获胜者并打印两个候选人总票数的条形图?

时间:2014-11-29 05:57:00

标签: c++ arrays loops voting

这个计划是为了计算两个候选人的总数和四个县中的每个县的投票数。同时显示胜利者。用户必须输入每个县和候选人的姓名和投票。我让程序正确地完成了上述目标。但我想通过展示每个县的获胜者并打印出每个两个候选人的条形图来进一步发展。总票数。 要打印条形图,我将使用PrintGraph,但不确定如何使用字符串。

#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 个答案:

没有答案