【问题标题】:CS50 Pset3 error: expected identifier or '('CS50 Pset3 错误:预期标识符或“(”
【发布时间】:2020-06-09 10:03:32
【问题描述】:

我正在通过 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;
}

为了澄清,我不想就这个问题寻求帮助,只是具体的编译错误。

谢谢大家

【问题讨论】:

  • 您为此使用 C 或 C++ 编译器吗?如果 C 标准低于 C99,则不能执行 for(int i=0; i&lt;voter_count; i++) ,但需要在循环之前定义 int i 。另外,检查gcc -std=c99 main.c 吐出的内容。
  • 我使用了一个我相信的 C 编译器,它在一个专门为 CS50 学生设计的 IDE 上。
  • 我尝试了 'gcc -std=c99 main.c',它说'错误:预期标识符或'('在 '[' 令牌候选 [i].name = argv[i + 1 ];' 但话又说回来,这是已经提供给我们的代码的一部分。
  • 如果您只需要针对该编译错误的帮助,删除代码中不相关的部分将是创建最小示例的良好开端。
  • 错误信息并不能说明太多。您有一个错字:您的代码中根本没有变量cancidate,编译器似乎将该行作为返回类型candidate 的函数的声明,这也由于缺少括号而失败。

标签: c struct cs50


【解决方案1】:

好的,这应该可以解决它。

你正在使用结构名称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;
    }

【讨论】:

  • 这很有效,感谢您的帮助。 (它抛出了一些其他错误,但这不是代码的本质吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多