简体   繁体   English

CS50 多个无法编译

[英]CS50 Plurality fails to compile

I always work on Psets on my local machine and replace string with char * so I don't have to use the CS50 library in my header files.我总是在我的本地机器上处理 Psets 并用char *替换string ,所以我不必在我的头文件中使用 CS50 库。 This is the only explanation I have for why my code doesn't compile when running check50这是我对为什么我的代码在运行check50时无法编译的唯一解释

The code works as expected on my machine as well as in the CS50 IDE, but check50 still gives me this error:代码在我的机器和 CS50 IDE 上都按预期工作,但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.

plurality.c复数.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;
}

@Blauelf answered this: @Blauelf 回答了这个:

The checker code renames your main function and appends its own.检查器代码重命名您的main函数并附加它自己的。

The warning exists because main is the only function returning non- void for which the return value is still defined if you don't return a value explicitly (it would return 0 by default).警告存在是因为main是唯一一个返回非void函数,如果您不显式返回值(默认情况下它将返回 0),其返回值仍被定义。 For other functions, it would be up to the compiler what the return value is, often depending on the CPU architecture.对于其他函数,返回值是什么取决于编译器,通常取决于 CPU 架构。 By renaming the function, this special property no longer applies.通过重命名函数,此特殊属性不再适用。 Not a problem, since it's a warning only, and the function is never called.没问题,因为它只是一个警告,并且永远不会调用该函数。

Then they append their own main function, and that's where the error happens: That one expects you to #include <cs50.h> .然后他们附加自己的main函数,这就是错误发生的地方:那个人希望你#include <cs50.h> Make sure to add this line for the submission, even if you don't use its functions yourself.确保为提交添加此行,即使您自己不使用其功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM