gpt4 book ai didi

CS50 Pset3 错误 : expected identifier or '('

转载 作者:行者123 更新时间:2023-12-04 09:42:06 27 4
gpt4 key购买 nike

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

关于CS50 Pset3 错误 : expected identifier or '(' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62279964/

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