1

I can't get the vote function in Runoff to work properly. Can anyone help explain what I am doing wrong here?

bool vote(int voter, int rank, string name)
{
    for (int i = 0; i < voter_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (strcmp(candidates[j].name, name) == 0)
            {
                preferences[i][j] = rank;
                printf("%i", rank);
                return true;
            }
            if (strcmp(candidates[j].name, name) != 0)
            {
                return false;
            }
        }
    }
return false;
}
0

Why does this code have two nested for loops? Only one is needed, to step through the candidate array to find the right name and store the data.

It should only be recording the vote preference for the given voter that was passed as a parameter, and the candidate passed as a parameter.

Also, it will only check one candidate name because the name will either match the first name in the array and return or it won't match, but will still return.

| improve this answer | |
  • Cool, thank you for your insight! – getsendy Jun 23 at 15:44
1

Just add one for loop and loop till the candidate_count If name and candidates[i].name matches then you should assign the i value to preferences[][]

remember preferences[0][0] means that 1st voter has given 1st rank to the candidate no [i].....

| improve this answer | |
  • Got it! Thanks so much – getsendy Jun 23 at 15:44

You must log in to answer this question.

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