0

Plurality Check50: https://submit.cs50.io/check50/dfe0d69252268f63eddb40057eada6979586a661

However, code works when I run it in my terminal: https://imgur.com/a/xEp0EnH

Code:

void print_winner(void)
{
    int votes[candidate_count];
    char winner[candidate_count][50];

    // Copy candidate vote information to seperate array
    for (int i = 0; i < candidate_count; i++)
    {
        votes[i] = candidates[i].votes;
        strcpy(winner[i],candidates[i].name);
    }

    // Bubble sort array
    for (int outer = 0; outer < candidate_count; outer++)
    {
        for (int inner = outer; inner < candidate_count; inner++)
        {
            if (votes[inner] < votes[outer])
            {
                // Sort vote count
                int temp = votes[inner];
                votes[inner] = votes[outer];
                votes[outer] = temp;

                // Sort candidate name to match sorted vote count
                char temp2[50];
                strcpy(temp2, winner[inner]);
                strcpy(winner[inner],winner[outer]);
                strcpy(winner[outer], temp2);
            }
        }
    }

    // Determine if there are any ties.
    for (int i = candidate_count-1; i >= 0; i--)
    {
        if (votes[i] == votes[i-1] && votes[i] != votes[i-2])
        {
            //printf("Candidates %s and %s are tied! \n", winner[i], winner[i-1]);
            printf("%s \n", winner[i]);
            printf("%s \n", winner[i-1]);
            break;
        }

        else if (votes[i] == votes[i-2])
        {
            //printf("Candidates %s and %s and %s are tied! \n", winner[i], winner[i-1], winner[i-2]);
            printf("%s \n", winner[i]);
            printf("%s \n", winner[i-1]);
            printf("%s \n", winner[i-2]);
            break;
        }
        else
        {
            printf("%s \n", winner[i]);
            break;
        }
        break;
    }
}
3

Resolved. I feel like a complete idiot.

printf("%s \n", winner[i]);

should be

printf("%s\n", winner[i]);
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .