Я всегда работаю с Psets на локальном компьютере и заменяю string
на char *
, поэтому мне не нужно использовать библиотеку CS50 в заголовочных файлах. Это единственное объяснение того, почему мой код не компилируется при запуске check50
. Код работает как на моей машине, так и в IDE CS50, но check50
все еще дает мне это ошибка:
code failed to compile
Log
running clang plurality.c -o plurality -std=c11 -ggdb -lm -lcs50...
running clang plurality_test.c -o plurality_test -std=c11 -ggdb -lm -lcs50...
plurality_test.c:68:1: warning: control may reach end of non-void function
[-Wreturn-type]
}
^
plurality_test.c:109:20: error: unknown type name 'string'
int main(int argc, string argv[])
^
1 warning and 1 error generated.
множественное число. c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
// Max number of candidates
#define MAX 9
// Candidates have name and vote count
typedef struct
{
char *name;
int votes;
} candidate;
// Array of candidates
candidate candidates[MAX];
// Number of candidates
int candidate_count;
// Function prototypes
bool vote(char name[]);
void print_winner(void);
int search(char name[]);
int main(int argc, char *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;
printf("Number of voters: ");
scanf("%i", &voter_count);
// Loop over all voters
for (int i = 0; i < voter_count; i++)
{
char name[10];
printf("Vote: ");
scanf("%s", name);
// 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(char 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 prev = -1;
int curr;
int id;
for (int i = 0; i < candidate_count + 1; i++)
{
curr = candidates[i].votes;
if (curr > prev)
{
id = i;
prev = candidates[id].votes;
}
}
printf("%s\n", candidates[id].name);
return;
}