0

so this is week three and I know we will learn about memory location next week. However I got a segmentation fault and don't know how to fix it or why it happened.

// Update vote totals given a new vote
bool vote(string name)
{
    // Iterate over candidates
    for (int i = 0; i < candidate_count+1; i++)
    {
        // If found update vote total and return true
        if (strcmp(name, candidates[i].name) == 0)
        {
            candidates[i].votes++;
            printf("vote is %i \n", candidates[i].votes);
            return true;
        }
    }
    return false;
}

./plurality will mike John

Number of voters: 3

Vote: aa

Segmentation fault

2
  • Can you publish your complete code, or a link to it?
    – MARS
    Jan 15 '20 at 22:58
  • that is it, I haven't finished writing print winner yet
    – Chris
    Jan 16 '20 at 15:23
1

If there are 3 candidates, as in the example, what are the indexes of the candidates array? What is the maximum i that this for (int i = 0; i < candidate_count+1; i++) will try to process (remember, starting at 0)?

1
  • So by adding +1 it is overarching to space that has not been assigned yet. if I understand correctly
    – Chris
    Jan 16 '20 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .