CS50 pset3 plurality program gives error for print_winner function (did not print both winners of election)

CS50 pset3 plurality program gives error for print_winner function (did not print both winners of election)

我已经完成了 cs50 的 pset3 的复数程序,但是在对我的代码进行 运行 check50 时,我收到以下针对 print_winner 函数的错误:

:) plurality.c exists
:) plurality compiles
:) vote returns true when given name of first candidate
:) vote returns true when given name of middle candidate
:) vote returns true when given name of last candidate
:) vote returns false when given name of invalid candidate
:) vote produces correct counts when all votes are zero
:) vote produces correct counts after some have already voted
:) vote leaves vote counts unchanged when voting for invalid candidate
:) print_winner identifies Alice as winner of election
:) print_winner identifies Bob as winner of election
:) print_winner identifies Charlie as winner of election
:( print_winner prints multiple winners in case of tie
    print_winner function did not print both winners of election
:) print_winner prints all names when all candidates are tied

尽管我的函数确实在平局的情况下打印了多个获胜者(并且我针对各种情况对其进行了多次测试),但此错误仍然存​​在,我不确定为什么。有人可以在下面检查我的代码并详细说明代码有什么问题吗? (很抱歉,如果它非常 messy/ineffecient - 我花了一段时间写它,因为我花了一段时间才弄清楚如何打印多个获奖者):

void print_winner(void)
{
    int j = 0;
    int max = candidates[0].votes;
    int competitor;
    int competitor2;
    string winner = candidates[0].name;

    // loop over all candidates + determine candidate with most votes (max)
    // + name candidate with most votes (winner)
    for (j = 1; j < candidate_count; j++)
    {
        competitor = candidates[j].votes;
        if (max < competitor)
        {
            max = competitor;
            winner = candidates[j].name;
        }
    }

    // loop over candidates again to determine if multiple winners + print winner/winners
    for (int f = 0; f < candidate_count; f++)
    {
        competitor2 = candidates[f].votes;
        if (max == competitor2 && candidates[f].name != winner)
        {
            for (int i = 0; i < candidate_count; i++)
            {
                printf("%s\n", candidates[i].name);
            }
            return;
        }
    }
    printf("%s\n", winner);
    return;
}

编辑:正如 DinoCoderSaurus 指出的那样,我的 i 循环有问题。我意识到这甚至没有必要,因为我的 f 循环已经提供了相同的功能。下面,我删除了 i 循环,稍微修改了 f 循环,并将 printf("%s\n", winner); 行移到了 f 循环之前,所以如果有多个获胜者,他们按正确顺序打印:

void print_winner(void)
{
    int j = 0;
    int max = candidates[0].votes;
    int competitor;
    int competitor2;
    string winner = candidates[0].name;

    // loop over all candidates + determine candidate with most votes (max)
    // + name candidate with most votes (winner)
    for (j = 1; j < candidate_count; j++)
    {
        competitor = candidates[j].votes;
        if (max < competitor)
        {
            max = competitor;
            winner = candidates[j].name;
        }
    }

    printf("%s\n", winner);
    // loop over candidates again to determine if multiple winners + print winner/winners
    for (int f = 0; f < candidate_count; f++)
    {
        competitor2 = candidates[f].votes;
        if (max == competitor2 && candidates[f].name != winner)
        {
            printf("%s\n", candidates[f].name);
        }
    }
    return;
}

尝试这次选举:3 位候选人:a、b、c。五位投票者:a,a,b,b,c.

预期的输出是什么?程序给出什么输出?

问题出在 i 循环中。如果找到 "second" 个获胜者,将打印哪些候选人姓名?