is this correct for vote function..struggled to figure out how to access candidate index so not sure if this is correct..any help/advice would be greatly appreciated

bool vote(int voter, int rank, string name)
    int voter = 0;
    for(int i = 0; i < candidate_count; i++)
      if(!strcmp(argv[i], name))
         preferences[voter + i][i]
         return true;
    return false;



A couple things: argv[] isn't available globally, so you're probably not able to compile your program because you're using it in this method. You should use the candidates array instead, since that is available globally, and it's set for you in main.

preferences[voter + i][i] this line isn't doing anything. You should be setting an element in this array to a value. Also, re-read how that array should be structured. Because of how the testing works, you have to set that array exactly how it's described in the problem, and it doesn't look like you have that right.

Also, you're not using the rank variable that's passed into the method. CS50x passed it in, so you should be using it. (Fixing the previous problem will mean you have to use it.)


From my understanding, the strcmp() function returns an integer value, note boolean value. So in order to check for equality, you'd have to change it to strcmp(argv[i], name) == 0

  • It'll convert to a boolean with the negation, so that part of the code is fine.
    – Llenk
    Jun 11 '20 at 15:25

You must log in to answer this question.

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