我的复数代码收到 'expected expression' 错误
I'm receiving a 'expected expression' error on my plurality code
如标题所述,我正在开发一个 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 循环块中使用了圆括号而不是大括号。
如标题所述,我正在开发一个 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 循环块中使用了圆括号而不是大括号。