So I have seen this issue mentioned here before and the others have suggested that printing a newline after the name is the solution. However, my code does print a newline and in my tests, I'm getting the expected result.

I am getting these errors from check50:

:( print_winner prints name when someone has a majority
:( print_winner returns true when someone has a majority

This is my code:

// Print the winner of the election, if there is one
bool print_winner(void)
    // iterate based on the number of candidates
    for (int i = 0; i < candidate_count; i++)
        // if candidates votes/voter count are greater than 50 they are the winner
        if (((candidates[i].votes / voter_count) * 100) > 50)
            printf("%s\n", candidates[i].name);
            return true;

    // for (int i = 0; i < candidate_count; i++)
    // {
    //     printf("c: %s, v: %i, e: %d\n", candidates[i].name, candidates[i].votes, candidates[i].eliminated);
    // }

    return false;

As you can see there is a return value set, the winner is getting printed out, and there is a newline at the end of the statement. Here is some example output where John is expected to be the winner:

~/pset3/runoff/ $ ./runoff John Bob
Number of voters: 3
Rank 1: John
Rank 2: Bob

Rank 1: Bob
Rank 2: John

Rank 1: John
Rank 2: Bob


Any help is greatly appreciated.


I was able to solve this as follows:

Firstly I kept track of the majority (voter_count / 2) in a float variable. I then converted the candidate's votes into a float and then compared it to the majority variable. Finally, instead of printing the name inside the loop, I stored the winner's name in another variable and then waited for the loop to finish.

If a winner existed, I printed the name and then returned true, otherwise, it would return false.

You must log in to answer this question.

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