1

I'm trying to write a function that will be called in main(int argc, string argv[]) for pset 2: plurality.

Inside the function, I want a for loop. I've written this:

bool vote(string name)
{ 
    for (int x=0; x<argc; x++) 
    {
       if (strcmp(name, candidates[x].name)==0)
        {
            candidates[x].votes++;
            return true;
        }       
    }
    return false;
}

but when trying to compile, an error pops up. "error: expected expression"

I assume this has to do with the fact that argc is declared in main, yet I'm working in a function that's outside of main.

HOWEVER I've changed "argc" to "candidate_count" which is defined inside the main function, and it seems to work.

My question is this: Why does candidate_count work, but not argc? both are defined within the main function, so they should both work, no?

2

candidate_count is not declared in main. It's a global variable (declared outside of main) on line 20 in the distribution file plurality.c at the time of this writing. The definition of main starts later on line 26. That's why you're able to see candidate_count in other functions. argc, on the other hand, is a local variable that you can reference only within the body of the main function.

| improve this answer | |
  • Oh! So it's declared outside of the main function, but defined within main. Thank you so much! – DanielDances May 7 at 1:41

You must log in to answer this question.

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