0

I've been working for weeks on pset3 - plurality, and all my logic is working fine in debug50, but not in real execution. It gives me either "segmentation fault" or (null). Basically, my logic is creating an organized array of the candidates (ganan [ ]) based on number of votes, find the largest number (int ganador), print its name, and checking for candidates with same number of votes. My code is the following (bool vote (string name) is working fine):

    // Print the winner (or winners) of the election
void print_winner(void)
{
    // Array for winners
    candidate ganan[voter_count];
    for (int k = 0; k < candidate_count; k ++)
    {
        for (int h = 0; h  < (candidates[k].votes); )
        {
            if (ganan[(candidates[k].votes)-h].votes == 0 || ganan[(candidates[k].votes)-h].votes < candidates[k].votes)
            {
                ganan[(candidates[k].votes)-h].name = candidates[k].name;
                ganan[(candidates[k].votes)-h].votes = candidates[k].votes;
                h = (candidates[k].votes);
            }
            else
            {
                h++;
            }
        }
    }

    // Find the winner
    int ganador;
    for (int ab = 0; ab < voter_count; ab++)
    {
        if (ganan [voter_count - ab].votes > 0)
        {
            ganador = voter_count - ab;
            ab = voter_count;
        }
    }

    printf("%s \n", ganan[ganador].name);
    for (int z = 0; z < voter_count; z++)
    {
        // Check if there's a tie
        if (ganan[z].votes == ganan[ganador].votes && ganador != z)
        {
            printf("%s \n", ganan[z].name);
        }
    }
}

1 Answer 1

2

You've stumbled on a known issue with debug50. It will, under unique circumstances, mask a segmentation fault. But, you can be sure that the seg fault is real. You'll need to isolate the offending line of code the hard way.

Read the following for details on ways to do just that: Do YOU know how to find a seg fault?? Advice to new programmers

You must log in to answer this question.

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