0

This keeps coming up with "segmentation fault" when I run it although everything I want it do is working. It seems to be coming from the last if statement because there's no fault when I delete that bracket but I can't figure out what's wrong with the bracket/an alternative to the bracket.

    int a = candidate_count;
    string winner[a];
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = i + 1; j < candidate_count; j++)
        {
            if (candidates[i].votes > candidates[j].votes)
            {
                winner[i] = candidates[i].name;
            }  
                if ((candidates[i].votes == candidates[j].votes) && (winner[i] = candidates[i].name))
                {
                    winner[j] = candidates[j].name;
                } 

        }
    }

    for (int i = 0; i < a; i++)
    {
        if (strcmp(winner[i], candidates[i].name) == 0)
        {
            printf("%s\n", winner[i]);
        }
    }
    return;
}

Please help, thanks!

2
  • can you post the whole file? – stensal Jun 13 '20 at 15:44
  • You use an array "winner" of the length a which we do not know how much it is. Let's assume it is big enough to store the values. You do not store a value into every single place in the array, you only place something in it under certain circumstances. So, checking every single place in the array with the for loop in the end probably gives you the segfault since not every placr contains a value. – MO_TM Jun 15 '20 at 13:12
0

you are acsessing a part of an array that you dont have acsess to

0

My approach was like this: 1-iterated through the candidates, and checked for the absolute maximum vote. 2-as I had the maximum vote, I iterated again through all the candidates, and checked which ones had the maximum vote, and declared the winner

You must log in to answer this question.

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