我是编程的初学者。目前正在学习edx上的CS50课程。我在第三周,必须编写一个简单的投票计数器,但是在第65行我得到了以下错误
plurality.c:65:25:错误:成员参考基类型'candidate9‘不是一个结构或联合合并(candidates.votes,0,MAX-1,candidates.name);
但是我的candidates.votes和candidates.name是数组不是吗?请帮我解决这件事我只是不知道怎么解决。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
void merge(int a[], int low, int mid, int high); //This function will merge the two sorted arrays
void mergesort(int a[], int low, int high, string name[]); //This function will merge the arrays, is recursive
// 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");
}
}
mergesort(candidates.votes, 0, MAX-1, candidates.name);
// Display winner of election
print_winner();
}
// Update vote totals given a new vote
bool vote(string name)
{
// TODO
for(int i =0; i< MAX; i++)
{
if(candidates[i].name == name)
{
candidates[i].votes++;
return true;
}
}
return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
// TODO
int win = candidates.votes[MAX-1]
for(int i = MAX-2; i >=0; i--)
{
if(win != candidates[i].votes)
{
printf("%s",candidates[i].name);
return;
} else
{
printf("%s", candidates[i].name);
win = candidates[i].votes;
}
}
return;
}
void mergesort(int a[], int low, int high, string name[])
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
mergesort(a, low, mid);
mergesort(a, mid+1, high);
merge(a, low, mid, high, string name);
}
else
{
return;
}
}
void merge(int a[], int low, int mid, int high, string name)
{
int i, j, k, b[high], c[high];
i = low;
j = mid + 1;
k = low;
while(i <= mid && j <= high )
{
if(a[i] < a[j])
{
b[k] = a[i];
c[k] = name[i];
i++;
k++;
}
else
{
b[k] = a[j];
c[k] = name[j];
// printf("%i ", b[k]);
j++;
k++;
}
}
while(i <= mid)
{
b[k] = a[i];
c[k] = name[j];
//printf("%i ", b[k]);
k++;
i++;
}
while(j <= high)
{
//printf("%i ", b[k]);
b[k] = a[j];
c[k] = name[j];
k++;
j++;
}
for(i = low; i <= high ; i++)
{
a[i] = b[i];
name[i] = c[i];
// printf("%i", a[i]);
}
}发布于 2022-08-20 06:59:38
您已经将candidates数组声明为:
candidate candidates[MAX];然而,您正在尝试访问数组本身的votes和name成员,而不是单个元素。
mergesort(candidates.votes, 0, MAX-1, candidates.name);从mergesort的签名
void mergesort(int a[], int low, int high, string name[])您似乎需要将数组的每个成员的选票和名称值收集到新的数组中,然后将它们传递给函数。
int votes[MAX];
for (size_t i = 0; i < MAX; i++) {
votes[i] = candidates[i].votes;
}https://stackoverflow.com/questions/73424606
复制相似问题