gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:49:44 24 4
gpt4 key购买 nike

如标题所示,我正在研究一个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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com