0

I'm currently trying to do cs50 pset3 plurality problem and I have unfortunately ran into this error which I do not know how to overcome. The error is "non-void function does not return a value in all control paths error" and refers to this particular section of code where it refers to the last } of the code.

If you need the preceding lines of code do tell me I will post it. Your help is very much appreciated thank you!

bool vote(string name)
{
    for (int a = 0 ; a < candidate_count ; a++)
    {
        //if input name matches any name in candidate list
        if (strcmp(name, candidates[a].name) == 0)
        {
            return true;
        }
        //if input name does not match with any name 
        else 
        {
            return false;
        }
    }
}
2

The error means a path that does not take the if branch or the else branch will not return a boolean, since the returns are embedded in those blocks.

Think about this comment //if input name does not match with any name . What the program is doing is //if input name does not match this name . If program returns on the first non-matching name, it will not finish processing the entire slate of candidates.

You must log in to answer this question.

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