1
for (int i = 0; i < voter_count; i++)
{
    // vote for top-preferred candidate if possible  
    if (candidates[preferences[i][0]].eliminated == false)
    {
        candidates[preferences[i][0]].votes += 1;
    }
    else
    {
        // vote for the following option
        for (int j = 1; j < candidate_count; j++)
        {
            if (candidates[preferences[i][j]].eliminated == false)
            {
                candidates[preferences[i][j]].votes += 1;
            }
        }
    }
}

which i don't understand since this is what this part is for:

// vote for the following option
for (int j = 1; j < candidate_count; j++)
{
    if (candidates[preferences[i][j]].eliminated == false)
    {
        candidates[preferences[i][j]].votes += 1;
    }
}
5
  • Can you please explain in more detail what your problem is? also are there comments on your code and can you add the first line on to them?
    – Chris
    Commented Feb 22, 2020 at 0:52
  • why did you hardcode the rank?
    – Chris
    Commented Feb 22, 2020 at 1:00
  • added the comments.
    – Ash
    Commented Feb 22, 2020 at 21:46
  • in the first if statement i want the vote to go to the first candidate in the ballot, that's why it's hardcoded in this way...[i][0].....if the first candidate is eliminated and it's not possible to vote for them....that's what the else statement is for and why its for loop starts from one and keeps on incrementing until a non-eliminated candidate is found...[i][j]
    – Ash
    Commented Feb 22, 2020 at 21:47
  • I will test your code and get back to you
    – Chris
    Commented Feb 22, 2020 at 21:48

1 Answer 1

2

The problem is in this loop:

for (int j = 1; j < candidate_count; j++)
{
    if (candidates[preferences[i][j]].eliminated == false)
    {
        candidates[preferences[i][j]].votes += 1;
    }
}

If your preference 0 is taken it registers all other preferences. That is to say if their are 3 candidates and preference 0 has been eliminated it will add the votes to the candidates in your preference 1 and also in candidate 2.

Try using debug50 to help you find the error.

1
  • i added a break after line 5 and it worked. thank you so much.
    – Ash
    Commented Feb 23, 2020 at 22:10

You must log in to answer this question.

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