So my code does not compile because it does not return a value. I don't know how to make it work and return true and false as it is in the if statement

// Update vote totals given a new vote
bool vote(string name)
    // Iterate over candidates
    for (int i = 0; i < candidate_count; i++)
        // If found update vote total and return true
        if (strcmp(name, candidates[i].name) != 0)
            return true;
        // Else if no candidate is called name return false
            return false;

If anyone can give me directions or a link where I can find more information about this I would be very grateful.

  • What is the exact error message from the compile? Can you please edit your question and cut and paste the actual compile results? Re: the answer below. @blauelf is right as far as he goes. In addition, the loop will only execute only once because it must execute one of the two return statements. However, I'm trying to figure out exactly why it isn't compiling. I'm suspecting that maybe its the infamous "function can complete without returning a value" (forgot the exact language of that error). – Cliff B Jan 8 at 5:49

The return false; should be the last line of your function, after the loop, there is nothing to do in that else.

Rationale: If you have a name match, you know the vote is valid. But if you have a mismatch, it might still be valid, matching another name. So only after the loop, if you haven't had a name match with any candidate, do you know the vote is invalid.

| improve this answer | |

You must log in to answer this question.

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