简体   繁体   中英

CS50 pset3 plurality program doesn't print_winner function (did not print both winners of election)

i just finish to program my plurality cs50 program. it runs smooth but when i run a check it says that it doesn't print the winner's name but it does. here it's the code, can anybody help me? as i said the function print_winner() works, if i run debug50 I can see that. with a command-line argument i'm suppose to declare no more then 9 candidate, chosse how many vote i want, vote for the candidate and then the program is suppose to declare the winner(s). the problem is that it does it, but according with bot50 it doesn't.

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }

    int voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}


// Update vote totals given a new vote
bool vote(string name)
{       
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes++;
            return true;
        }
    }
    return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
    int n = candidate_count;
    typedef struct
    {
        string name;
        int votes;
    }
    bubble;
    bubble bubbles[n];
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {    //doing a bubble sort to move the max vote at the end and doing 
               the same with the candidate name
                 
            if (candidates[i].votes > candidates[j].votes)
            {
                bubbles[0].votes = candidates[i].votes;
                candidates[i].votes = candidates[j].votes;
                candidates[j].votes = bubbles[0].votes;
                bubbles[0].name = candidates[i].name;
                candidates[i].name = candidates[j].name;
                candidates[j].name = bubbles[0].name;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (candidates[n - i].votes == candidates[n - 1].votes)
        {
            printf("%s ", candidates[n - i].name);
        }
        printf("\n");
    }
}

It would be nice to see more of your code, like where candidates and candidate_count are defined/initialized, and for instance, if you're doing #include <string> which indicates you're actually using std::string and not some other version of a string.

If that's the case, your use of std::string is odd. Typically strcmp() requires a pointer to a C-style array of chars or the type const char* ... as does printf() ... so you should append .c_str() to your usages of name and candidates[n - i].name .

Ie strcmp(candidates[i].name.c_str(), name.c_str()) and printf("%s ", candidates[n - i].name.c_str());

Again, though, that's assuming you're actually using std::string .

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