c - 我在我的复数代码中收到 'expected expression'错误

标签 c if-statement compiler-errors cs50

如标题所示,我正在研究一个CS50程序,该程序将模拟简单的复数投票。乍一看,我的代码似乎还不错。但是,当我尝试编译它时,我收到一个错误:

plurality.c:71:9: error: expected expression
if (strcmp(candidates[i].name, name) == 0)
^

就我而言,我的代码是正确的。我已将方括号匹配,并确保正确执行了if语句。谁能告诉我为什么我收到此错误

这是我的代码,也许一些聪明的人可以显示出什么地方出了问题。
#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 winner;
    winner = candidates[0].votes;
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > winner)
        {
            winner = candidates[i].votes;
        }
    }
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes == winner)
        {
            printf("%s\n", candidates[i].name);
        }
    }
    return;
}

我假设在if语句之外,编译器已经标记了一个问题,这就是为什么我包含整个程序的原因。如果有人可以向我解释为什么我如此愚蠢,将不胜感激。

最佳答案

for (int i = 0; i < candidate_count; i++)
    (
        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes++;
            return true;
        }
    )
    return false; `

您在此for循环块中使用括号而不是大括号。

关于c - 我在我的复数代码中收到 'expected expression'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61075905/

相关文章:

c - Doxygen 转义 C 中的嵌套注释

c - 以编程方式验证 X509 证书和私钥匹配

c++ - 简单的 C++ 比较 if 语句

c - sndfile.h C CodeBlocks Windows 7

c# - 无法按照教程创建动态搜索结果

c++ - 从 8 位字节中提取二进制数据并将其转换为原始类型 [C++]

c - 用户输入中断 while 循环以返回主程序

页面顶部的 PHP 回显

java - 我无法向用户显示输出

java - 比较parseInt(String)与Int输入错误: unexpected type Java