C++ 如何报告每个县的获胜者并打印两名候选人的条形图';总票数?
该程序将统计来自四个县总计和每个县的两名候选人的选票,并显示获胜者。用户必须输入每个县和候选人的姓名和选票。我让程序按照上述目标正确运行。但我想更进一步,显示每个县的获胜者,并打印出每两位候选人总选票的条形图。 要打印条形图,我将使用C++ 如何报告每个县的获胜者并打印两名候选人的条形图';总票数?,c++,arrays,loops,voting,C++,Arrays,Loops,Voting,该程序将统计来自四个县总计和每个县的两名候选人的选票,并显示获胜者。用户必须输入每个县和候选人的姓名和选票。我让程序按照上述目标正确运行。但我想更进一步,显示每个县的获胜者,并打印出每两位候选人总选票的条形图。 要打印条形图,我将使用PrintGraph,但不确定如何使用字符串 #include<iostream> #include<string> #include<vector> using namespace std; int tier1( co
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;
}
#包括
#包括
#包括
使用名称空间std;
int第1层(
常量标准::字符串和c0,
常量标准::字符串和c1,
const std::向量和县);
int main()
{
CUT请按照它告诉你的去做。-伙计,你开始是专横的。这都可以学习一个简单的矢量和字符串的C++教程。你哑巴,去完全地学习它。哈哈哈,我不是那个意思。当然,我是这样做的。你认为我是如何打出这个程序的大部分的?我看了几篇教程,觉得有点迷惑。我,因为我显然需要把字符串转换成int。我上个月刚开始C++,给我一个休息时间: