简体   繁体   中英

CS50 pset3: Plurality. Print_winner function incorrectly printing multiple candidates regardless of input when running the program

CS50's pset3 plurality requires you to print the winners of the "election" based on the number of votes they receive. Votes are input by the user.

My program's working fine up until I have to print the winners. I've been at it for a few days trying different methods but can't get my head around it.

My code currently outputs multiple candidates regardless of the input. I'm guessing that the variables "most_votes" and "election_winner" are retaining the assigned value from the top of the code.

How can I rewrite this to print a single winner if they receive the most votes and multiple winners if multiple candidates receive an equal number of votes?

Thanks for any help!

Here's my code:

// Print the winner (or winners) of the election
void print_winner(void)
{
    int most_votes = candidates[0].votes;
    string election_winner = candidates[0].name;
    
    // Loop over each candidate 
    for (int i = 1; i < candidate_count; i++)
    {
        // Count how many votes each candidate received and determine who
        // received the most votes
        if (most_votes < candidates[i].votes)
        {
            most_votes = candidates[i].votes;
            election_winner = candidates[i].name;
        }
    }
    
    // Loop over candidates a second time
    for (int j = 1; j < candidate_count; j++)
    {
        // Count how many votes each candidate received
        // Compare number of votes of each candidate
        // Check if mutiple candidates have the most votes
        if (most_votes == candidates[j].votes && election_winner != candidates[j].name)
        {
            // Print all candidates with the most votes
            printf("%s\n", candidates[j].name);
        }
    }
    // Print the name of winner with most votes
    printf("%s\n", election_winner);
    return;
}

You do not need (and shouldn't have) the variable election_winner , if there can be multiple winners.

  1. In the first loop only determine the value for most_votes .
  2. Loop over the candidates a second time (starting from 0, not 1 as you are currently doing). If the vote count of the candidate equals most_votes , then you know that the candidate is a winner, and you may print the name out, or whatever else is required.

Thanks for the help guys I was able to figure it out. Simplified it as @vmt suggested and worked!

Changing the variable "most_votes" to "0" instead of "candidates[0].votes" made it easier to figure out.

// Print the winner (or winners) of the election
void print_winner(void)
{
    int most_votes = 0;
    
    // Loop over each candidate 
    for (int i = 0; i < candidate_count; i++)
    {
        // Count which candidate has the most votes and assign them most_votes
        if (most_votes < candidates[i].votes)
        {
            most_votes = candidates[i].votes;
        }
    }
    
    // Loop again over candidates
    for (int j = 0; j < candidate_count; j++)
    {
        // Check if mutiple candidates have the most votes
        if (most_votes == candidates[j].votes)
        {
            // Print all candidates with the most votes
            printf("%s\n", candidates[j].name);
        }
    }
    return;
}

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