0

So when I run my code it does not stop running

~/pset3/runoff/ $ ./runoff mike john Number of voters: 3

Rank 1: john Rank 2: mike

Rank 1: john Rank 2: mike

Rank 1: mike Rank 2: john

s s ??

And I can just keep typing things.

This is my code. Im sorry for dropping so much of it but I have no idea where the problem is.

also when I put and invalid vote the program quits

// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
    // Iterate over candidates
    for(int i = 0; i < candidate_count; i++)
    {
        // Check if name is a candidate
        if (strcmp(name, candidates[i].name) == 0)
        {
            // Update preference
            preferences[voter][rank] = i;
            return true;
        }
    }
    return false;
}

// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
    int j = 0;
    // Iterate over candidates
    for(int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].eliminated == true)
        {
            j++;
        }
        candidates[i].votes = preferences[i][j];
    }
    return;
}

// Print the winner of the election, if there is one
bool print_winner(void)
{
    // Iterrate over the candidates
    for(int i= 0; i < candidate_count; i++)
    {
        if(candidates[i].votes >= voter_count)
        {
            printf("%s", candidates[i].name);
            return true;
        }
    }
    return false;
}

// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
    int min = 0;
    // Iterate of candidates
    for (int i =0; i < candidate_count; i++)
    {
        // Check that candidate does not have 0 votes
        if(candidates[i].votes != 0)
        {
            // Save the minume votes in int min
            if (candidates[i].votes < min)
            {
                min = candidates[i].votes;
            }
        }
    }
    return min;
}

// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
    // Iterate over all candidates
    for(int i = 0; i < candidate_count; i++)
    {
        // Check if candidate has been eliminated
        if (candidates[i].eliminated != true)
        {
            //Check if candidate does not have min votes
            if(candidates[i].votes != min)
            {
                return false;
            }
        }
    }
    return true;
}

// Eliminate the candidate (or candidiates) in last place
void eliminate(int min)
{
    // Iterate over all candidates
    for(int i = 0; i < candidate_count; i++)
    {
        // Check if candidate has been eliminated
        if (candidates[i].eliminated != true)
        {
            //Check if candidate does not have min votes
            if(candidates[i].votes == min)
            {
                candidates[i].eliminated = true;
            }
        }
    }
    return;
}
  • This line if (candidates[i].eliminated == true) in tabulate caught my attention on a quick read. Isn't that the opposite of // Tabulate votes for non-eliminated candidates? – DinoCoderSaurus Jan 18 at 16:27
  • Well the idea is that if the candidate is eliminated(true) then j++ means that it should go to his second choice. But that does not explain why it does not stop running or why it fails with invalid candidates names. – Christopher Holder Jan 18 at 17:44
  • Sorry for the misdirect. – DinoCoderSaurus Jan 18 at 19:45
  • You should replace candidates[i].votes = preferences[i][j] in the Tabulate votes for non-eliminated candidates by candidates[k].votes++ where int k = preferences[i][j] – Aymene Cherradi May 24 at 0:22
  • Thanks mate, question is 4 months old with an accepted answer. There are many new questions tho and we appreciate your input. – Christopher Holder May 24 at 1:02
1

The program "never finishes" because it is stuck in an infinite loop because there is never a winner.

There's a problem in find_min, it always returns 0 because this if (candidates[i].votes < min) will always be false.

There are some problems in tabulate:

  • It's not counting all the voters. In your example there are 2 candidates and 3 voters; the last voter's votes will not be counted.
  • since preferences[voter][rank] holds a candidate "index", this candidates[i].votes = preferences[i][j]; is wrong.
  • What if "j++" preference is also eliminated?

This is more of a "back to the drawing board" than an answer. But you probably want to think through these problems and try solutions. The hints in the spec and the walkthrough may shed more light.

This might be a good exercise to take debug50 for a spin.

I found this challenging to test (I like to test each function as it's completed, but it tended to an infinite loop early on). I used check50 -l cs50/problems/2020/x/runoff after completing each function to have check50 verify them one a a time (the -l switch runs it locally).

| improve this answer | |
  • thank you for pointing out that I should be testing each function when complete. I will do that from now on. – Christopher Holder Jan 19 at 9:34

You must log in to answer this question.

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