【发布时间】:2021-07-22 05:06:18
【问题描述】:
我有 3 个候选人 A、B、C。 通过用户输入,用户通过输入 A、B 或 C 来选择候选人。 对于每个投票的候选人,该候选人必须得 1 分,其余的必须得 0 分。 我目前的输出是:
输入选项 A:A 为 1; B是16; C 为 0 输入选项 B:A 为 0; B为1; C 为 0(正确) 输入选项A:A为0; B是16; C 为 1
或者我必须使用只输出候选人和投票的代码?
#include <iostream>
using namespace std;
int main()
{
int votesForA, voresForB,votesForC, spoiltVotes;
int votingstations=0, i, total;
int count = 0;
int sum=0;
char candidate;
char candidate_A, candidate_B, candidate_C;
cout << "How many voting stations are there?"
cin >> votingstations;
for (i = 0; i <=4; i++) {
sum=i;
total=sum;
sum++;
cout << "There are " << total << " voting stations" << endl;
{
while (count < candidate_A, candidate_B, candidate_C);
cout << "Which candidate got the vote? " << endl;
cin >> candidate;
cout << "You chose " << candidate << endl;
count++;
switch (candidate)
{
case 'a':
case 'A':
votesForA=0;
candidate_A=votesForA;
votesForA++;
cout << "Candidate A's amount of votes is: " << votesForA << endl;
break;
【问题讨论】:
-
选择所有文本,然后使用编辑器的“reindent”命令(如果没有,请使用其他编辑器)。这将暴露几个错误。然后阅读逻辑连接词,例如
&&,以及如何在您最喜欢的 C++ 书籍中测试多个条件。并且更加注意标点符号。 -
我试图将您的代码粘贴到我的 IDE 中(以便我可以编译和调试它),但我的 IDE 在将图像转换为代码时遇到问题。没有代码粘贴为文本 == 没有帮助。
-
实际上,您的代码很少有任何意义,老实说,它看起来很大程度上是由猜测制成的。我建议您删除代码,然后退后几步,在较小的程序上练习,直到您理解它们(不允许反复试验),然后再尝试。
标签: c++ while-loop count switch-statement