0

After finishing plurality, it fails check50. It returns "print_winner function did not print winner of election".

In print_winner function I did the following

void print_winner(void)

{

//create a pivot for bubble sorting

candidate temp[1];

//bubble sorting from bigger to smaller, winner is first on the list

for (int a=0; a < candidate_count - 1; a++)

{

    for (int b=0; b < candidate_count - a - 1; b++)

    {

        if (candidates[b].votes < candidates[b+1].votes)

        {

            temp[0].name = candidates[b].name;

            temp[0].votes = candidates[b].votes;

            candidates[b].name = candidates[b+1].name;

            candidates[b].votes = candidates[b+1].votes;

            candidates[b+1].name = temp[0].name;

            candidates[b+1].votes = temp[0].votes;

        }

    }

}

int total=1;

//compares the top of the list with the rest of the the values to search for a tie.

for (int c=1; c < candidate_count; c++)

{

    if (candidates[0].votes == candidates[c].votes)

    {

        total++;

    }

}

//create an array with the final number of the winners

string winners[total];

//migrate the winners to the variable winners

for (int i=0; i<total; i++)

{

    winners[i]=candidates[i].name;

}

//print the winners

for (int i=0; i<total; i++)

{

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

}

return;

It works when I check it manually, but somehow check50 doesn't recognize it. Any idea where I went wrong?

1

If you know you're printing the right answer, then it must be your formatting when you print. Try removing the space when you print each name. Use this printf ("%s\n", winners[i]); instead of printf ("%s \n", winners[i]);

| improve this answer | |
  • It worked! Thank you very much, that little space was making a lot of trouble. – oscar soberanes Mar 30 at 16:47

You must log in to answer this question.

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