【问题标题】:Use a switch statement within a while loop to output votes for candidates,在 while 循环中使用 switch 语句为候选人输出投票,
【发布时间】: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”命令(如果没有,请使用其他编辑器)。这将暴露几个错误。然后阅读逻辑连接词,例如&amp;&amp;,以及如何在您最喜欢的 C++ 书籍中测试多个条件。并且更加注意标点符号。
  • 我试图将您的代码粘贴到我的 IDE 中(以便我可以编译和调试它),但我的 IDE 在将图像转换为代码时遇到问题。没有代码粘贴为文本 == 没有帮助。
  • 实际上,您的代码很少有任何意义,老实说,它看起来很大程度上是由猜测制成的。我建议您删除代码,然后退后几步,在较小的程序上练习,直到您理解它们(不允许反复试验),然后再尝试。

标签: c++ while-loop count switch-statement


【解决方案1】:

首先,您的格式会伤害您,并且会让您难以理解您的问题所在。让我们修复它:

#include <iostream>
using namespace std;

int main()
{
    int votesForA, voresForB, votesForC, spoiltVotes;
    int votingstations = 0, 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 (int i = 0, total = 0, sum = 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;

所以现在,在我回答你的问题之前,我们得先谈谈一些代码问题。

  1. while 循环中没有 switch 语句。 while 循环目前什么都不做
  2. 您声明了一个随机范围(多余的花括号)
  3. 您的 while 循环条件没有意义。
  4. 您将打印 0 作为投票站的数量

我不确定你的条件应该是什么,所以我将假设你想要所有投票的总数

在这种情况下,我认为可以解决您的代码中的一些问题:

#include <iostream>
using namespace std;

int main()
{
    int votesForA, voresForB, votesForC, spoiltVotes;
    int votingstations = 0, total, i;
    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 " << votingstations << " voting stations" << endl;
        
        while (count < votingstations)
        {
            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;

现在,在这一点上,我不完全确定您在问什么,但这将允许您循环通过每个投票站,直到您使用 while 循环达到 votingstations 的投票数。每次,只有获得选票的候选人才会使用开关打印一条消息。这听起来像你对我的要求。

这是输出与输入:

输出:

How many voting stations are there?

输入:

3

输出:

There are 3 voting stations

Which candidate got the vote?

输入:

a

输出:

You chose a

Candidate A's amount of votes is: 1

Which candidate got the vote?

输入:

A

输出:

You chose A

Candidate A's amount of votes is: 2

Which candidate got the vote?

输入:

B

输出:

You chose B

Candidate B's amount of votes is: 1

此时总票数等于votingstations,while 循环退出,for 循环重复。

您的代码中有很多无用的变量和冗余,但我显然不想解决这个问题,因为您可能会在程序的后面使用这些。

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多