Search code examples
csegmentation-faultcs50

Using strcmp gives me segmentation fault (cs50 runoff problem)


In the vote function i can't figure out why if i enter a different voter's name, it gives me a segmentation faul instead of returning false.

bool vote(int voter, int rank, string name)
{
    if (strlen(name) > 0)
    {
        for (int i = 0 ; i < voter_count ; i++)
        {
            if (strcmp(candidates[i].name , name) == 0)
            {
                preferences[voter][rank] = i;
                return true;
            }
        }
    }
    return false;

I checked other codes for the same problem set and with the same code, they didn't get any wrong messages from the checker.


Solution

  • The use of the variable copy in the function indicates a new global variable has been added. That is expressly forbidden by the spec and will not pass check50.

    You should not modify anything else in plurality.c other than the implementations of the vote and print_winner functions (and the inclusion of additional header files, if you’d like).