CS50多数赢得了拒绝无效的答案,只会打印一个赢家

发布于 2025-01-18 09:57:20 字数 2694 浏览 1 评论 0原文

此代码的大多数是针对课堂预先写的,我只在底部写下功能,但是由于某种原因,如果不匹配命令行参数之一,它不会拒绝答案。

该计划应该计算每个候选人的选票数量,并打印选举的获胜者(S)。
您将候选人放在命令行论据中,该计划提示您投票多少和投票。
如果演员票之一与命令行论证之一不符合,它不会拒绝,尽管有领带,但它只会为我打印一个赢家。
我经历了一些演练,我的代码看起来与其他程序正确运行的其他人几乎相同。我确定我缺少一些东西,但找不到。

    #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)
    {
        // TODO
        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)
    {
        //TODO
        int highestVote = -1;
        for(int i = 0; i < candidate_count; i++)
        {
            if(candidates[i].votes > highestVote)
            {
                highestVote = candidates[i].votes;
            }
        }
        for(int i = 0; i < candidate_count; i++)
        {
            if(candidates[i].votes == highestVote)
            {
                printf("%s\n" , candidates[i].name);
            }
        }
    
    }

谢谢您的任何帮助!

The majority of this code comes pre-written for the class, I only wrote the functions on the bottom but for some reason it won't reject an answer if it doesn't match one of the command line arguments.

The program is supposed to count the number of votes for each candidate and print the winner(s)(in the case of a tie)of an election.
You put the candidates in the command line arguments and the program prompts you for how many people are voting and for the votes.
It won't reject if one of the cast votes doesn't match one of the command line arguments and it only prints one winner for me despite ties.
I've gone through some walk-throughs and my code looks practically the same as others who's programs run correctly. I'm sure I'm missing something but I can't find it.

    #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)
    {
        // TODO
        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)
    {
        //TODO
        int highestVote = -1;
        for(int i = 0; i < candidate_count; i++)
        {
            if(candidates[i].votes > highestVote)
            {
                highestVote = candidates[i].votes;
            }
        }
        for(int i = 0; i < candidate_count; i++)
        {
            if(candidates[i].votes == highestVote)
            {
                printf("%s\n" , candidates[i].name);
            }
        }
    
    }

Thank you for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文