To figure out the winner, I am trying to do a bubble sort and then print the last name (or names if the last two are tied). For some reason, after it goes through the first run-through of the bubble sort (correctly, with the "for" loop below), it forgets what it already did (after going through the "while" part).
void print_winner(void)
{
// TODO
int counter = 1;
do
{
counter = 0;
for (int b = 0; b < candidate_count - 1; b++)
{
if (candidates[b].votes > candidates[b + 1].votes)
{
swap_int(candidates[b].votes, candidates[b + 1].votes);
swap_string(candidates[b].name, candidates[b + 1].name);
counter++;
}
}
}
while (counter != 0);
...
void swap_int(int x, int y)
{
int temporary = x;
x = y;
y = temporary;
}
void swap_string(string x, string y)
{
string temporary = x;
x = y;
y = temporary;
}
At first, I thought maybe the names weren't following the votes, but using debug I figured out they are. It seems my program gets stuck in an infinite loop of trying to bubble sort because it gets stuck at square 1 each time