0

I'm not sure I understand why here:

// Update vote totals given a new vote
bool vote(string name)
{
    for (int j = 0; j < candidate_count; j++)
    {
        if (strcmp(name, candidates[j].name) == 0)
        {
            candidates[j].votes++;
        }
    }
    return true;
}

if I use the variable candidate_count in this function I don't get any errors, but if I use the variable voter_count I get this:

plurality.c:69:25: error: use of undeclared identifier 'voter_count'
    for (int j = 0; j < voter_count; j++)
                        ^
1 error generated.

1 Answer 1

1

Without seeing the entire code, we can only guess, but the most obvious answer is that candidate_count is declared as a global variable, but voter_count is not.

Global vars can be used anywhere, although their use is highly frowned upon, unless their use is well justified.

Remember too that local variables need to be declared within the functions in which they are used. They're also limited in scope to the curly braces containing them.

1
  • Ohhh I see! candidate_count IS a global variable! Thank you so much!!!
    – jiayou
    Jul 7, 2022 at 20:22

You must log in to answer this question.

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