简体   繁体   中英

CS50 pset3 runoff print_winner

I can't find why it's throwing the error as when I manually input it works fine. but throws an error when they use their system. :( 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

int votes_need;

// printf("votes needed: %i\n", votes_need);
for (int i = 0; i < candidate_count; i++)
{
    if (voter_count % 2 != 0)
    {
        votes_need = ((voter_count + 1 )/ 2);

        if (candidates[i].votes >= votes_need)
        {
            printf("%s\n", candidates[i].name);
            return true;
        }
        else
        {
            printf("no winner yet!!!\n");
            return false;
        }
    }
    else
    {
        votes_need = ((voter_count)/ 2);

        if (candidates[i].votes > votes_need)
        {
            printf("%s\n", candidates[i].name);
            return true;
        }
        else
        {
            printf("no winner yet!!!\n");
            return false;
        }
    }
}
return false;

I think the problem is the else clauses that return false; within the i loop. As soon as it finds a candidate with fewer than half the votes, the function is over and returns to the caller. Maybe when you manually input, candidate[0] is always the winner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM