I am trying to do the Plurality exercise for problem set 3, but when I type this to test it, I get errors even though I believe my program looks correct.
check50 cs50/problems/2021/x/plurality
I get the following message:
:( print_winner identifies Alice as winner of election
Cause
print_winner function did not print winner of election
:( print_winner identifies Bob as winner of election
Cause
print_winner function did not print winner of election
:( print_winner identifies Charlie as winner of election
Cause
print_winner function did not print winner of election
:( print_winner prints multiple winners in case of tie
Cause
print_winner function did not print both winners of election
:( print_winner prints all names when all candidates are tied
Cause
print_winner function did not print all three winners of election
This is my code. To me it looks correct, and also prints what looks correct to me when I have tested it:
void print_winner(void)
{
for (int i=0; i<candidate_count; i++)
{
for (int j=0; j<candidate_count; j++)
{
if (candidates[j].votes==candidate_count-i)
{
printf("%s\n", candidates[j].name);
}
}
for (int j=0; j<candidate_count; j++)
{
if (candidates[j].votes==candidate_count-i)
{
return;
}
}
}
}