简体   繁体   中英

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. This is the only explanation I have for why my code doesn't compile when running check50

The code works as expected on my machine as well as in the CS50 IDE, but check50 still gives me this error:

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

#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:

The checker code renames your main function and appends its own.

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). For other functions, it would be up to the compiler what the return value is, often depending on the CPU architecture. 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> . Make sure to add this line for the submission, even if you don't use its functions yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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