CS50 Pset3错误:预期的标识符或'('

EdCase

我正在通过CS50执行PSET3,并且在编译代码时遇到问题。它不断提出错误

plurality.c:44:21: error: expected identifier or '('
        candidate[i].name = argv[i + 1];

这很奇怪,因为这是在我们开始问题之前给出的代码部分。

我的完整代码在这里...

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.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++)
    {
        candidate[i].name = argv[i + 1];
        candidate[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
    // Local boolean variable
    bool valid_vote = false;

    // Iterating over number of candidates and comparing string "name" to get a boolean result.
    for (int i = 0; i < candidate_count; i++)
    {

        if (strcmp(name, candidate[i].name) == 0)
        {
            candidate[i].votes++;
            valid_vote = true;

            // Force a break in the program to exit with the correct result
            break;
        }
    }
    return valid_vote;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    // Initialising a variable for the most votes, and the candidate
    int highest_votes = candidates[0].votes;
    string win = candidates[0].name;

    // Looping through results and finding the highest "votes" value
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidate[i].votes > highest_votes)
        {
            highest_votes = candidate[i].votes;
            win = candidate[i].name;
        }
    }

    // Comparing all "votes" values and printing winner(s)
    for (int j = 0; j < candidate_count; j++)
    {
        if (candidate[j].name == win)
        {
            printf("%s %s", win, candidates[j].name);
        }
        printf("%s", win);
    }
    return 0;
}

为了澄清,我不希望这个问题的帮助,只是特定的错误。

多谢你们

突变键盘

好的,这应该可以解决。

您正在使用结构名称candidate

typedef struct
{
    string name;
    int votes;
} candidate;

// Array of candidates
candidate candidates[MAX];

但是在for循环中,您要遍历candidate而不是candidates

所以改变这个:

 for (int i = 0; i < candidate_count; i++)
    {
        candidate[i].name = argv[i + 1];
        candidate[i].votes = 0;
    }

对此:

 for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章