2

I tried in two ways to show the winner of the election, and both testing manually are giving the correct result, but when testing by "check50" the message always appears:

:( print_winner prints name when someone has a majority;

print_winner did not print winner of election

:( print_winner returns true when someone has a majority;

print_winner did not print winner and then return true*

I have already researched this problem on this forum and analyzed the answers, but the problem still remains.

This is my code:

bool print_winner(void){

// TODO
float percent = 0;
bool win = false;
string name;

int majority = round((float) voter_count / 2);

for (int i = 0; i < candidate_count; i++)
{
    if (!candidates[i].eliminated)
    {

       //Try 1

       //if ((float)candidates[i].votes > majority)
       //{
       //    name = candidates[i].name;
       //    win = true;
       //}

       //Try 2
       percent = ((float)(candidates[i].votes / voter_count) * 100);
       if (percent > 50)
       {
            name = candidates[i].name;
            win = true;
       }
    }
}

if (win)
{
    printf("%s \n", name);
    return true;
}
else
{
    return false;
}

}

Thanks!

1

check50 is seeing if you print the candidate's name, then a newline. You're printing the candidate's name, then a space, then a newline. The formatting is pretty finicky, so you'll need to remove the space.

| improve this answer | |

You must log in to answer this question.

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