C++ 如何允许用户指定名称,程序将使用该名称报告结果?
该程序将统计来自四个县总计和每个县的两名候选人的选票,并显示获胜者。用户必须输入每个县和候选人的投票。现在,感谢你们,我为上述目的解决了这些问题。但是现在,我想添加Cout语句,允许用户指定四个县和两个候选人的名字。以及使用名称来报告结果 我不知道如何在循环中加入变量,以便在报告结果(如总冠军和每个县的冠军)时打印姓名 谢谢你的时间C++ 如何允许用户指定名称,程序将使用该名称报告结果?,c++,arrays,loops,increment,voting,C++,Arrays,Loops,Increment,Voting,该程序将统计来自四个县总计和每个县的两名候选人的选票,并显示获胜者。用户必须输入每个县和候选人的投票。现在,感谢你们,我为上述目的解决了这些问题。但是现在,我想添加Cout语句,允许用户指定四个县和两个候选人的名字。以及使用名称来报告结果 我不知道如何在循环中加入变量,以便在报告结果(如总冠军和每个县的冠军)时打印姓名 谢谢你的时间 #include<iostream> using namespace std; int tier1(); int main(void) { int r
#include<iostream>
using namespace std;
int tier1();
int main(void)
{
int return_val = tier1();
if (return_val < 0) // print an error
return 0;
}
int tier1(){
int votes[8];
int i, j, N; // variables
int k = 0;
for (i=0; i<4; i++)
{
cout << "county" << i << "\n"; // lists the 4 counties/candidates
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 ";
return 0;
}
#包括
使用名称空间std;
int-tier1();
内部主(空)
{
int return_val=tier1();
if(return_val<0)//打印错误
返回0;
}
int第1层(){
整数票[8];
int i,j,N;//变量
int k=0;
对于(i=0;i,首先您需要拉入字符串和向量头的内容
#include <string>
#include <vector>
现在,您需要更新main
以从cin
读取候选人和县名称,并将这些名称传递给tier1
函数
int main()
{
cout << "Welcome to the VoteTime Program. Please do what it tells you.\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;
}
你知道这段代码不会编译,对吗?是的,它编译和运行时没有顶部的cout语句。我只是添加了那些cout语句作为一个想法,我知道它不完整,现在不会编译,如果你想看到程序编译和运行。我可以删除cout语句和int c0,c1,…。然后它应该运行。我继续进行编辑我刚才为一个想法添加的部分。基本上,现在,程序应该编译并在main
中运行,注释是不存在的。您不会打印错误消息。相反,return
语句绑定到if
语句并有条件地执行。哇,这肯定很有用!但我需要用户还要指定县的名称吗?更新了答案以包括对县的支持。如果您不能使用std::vector
,请切换到使用裸数组并揍您的讲师。我非常感谢您的帮助。但是,在“您忘记了”include之前,我遇到了一系列错误,例如预期的不合格id。:/code>:有一件事我没有注意到在用户为候选人添加姓名之后是帽子。它询问“候选人0获得了多少选票”,而不是说出用户添加的姓名?我不知道如何处理这个问题。最后,它确实说出了用户添加的姓名。只是不是在询问时。但对于县,它工作得非常好
int main()
{
cout << "Welcome to the VoteTime Program. Please do what it tells you.\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";
for (j = 0; j<2; j++)
{
cout << "How many votes did the candidate " << j << " get?\n";
N = 0;
cin >> N;
votes[k++] = N;;
}
//checking if it goes over 100 votes
if (votes[k - 2] + votes[k - 1] > 100)
{
cout << "One of the counties has too many votes. Exiting!\n";
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 ";
return 0;
}