I am working on the print function for Runoff and I keep getting the response
"The winner is: (null)"
I guess at this point I can't tell if the tabulate function isn't working or if print isn't working. Does anyone see an obvious fault with this code?
// Print the winner of the election, if there is one
bool print_winner(void)
{
int max=0; //holds the highest value found in candidates.vote
for (int i=0;i<candidate_count; i++)
{
max=candidates[i].votes; //holds the highest value found in candidates.vote
if(candidates[i].votes<candidates[i+1].votes)
{
max=candidates[i+1].votes;
}
}
if(max>(voter_count*.5)).
{
printf("the winner is:%s\n", candidates[max].name);
return true;
}
return false;
}
Thanks, Rob
EDIT. I have changed my function to accept a datastruct candidate for swaping usage and that appears to be going well. I am able to sort the candidates by vote count (verified by printf). The tie is still confounding me. If I have a clear winner, either unanimous or split but more then half, I print the winner is. If I have a tie, the program enters a loop that it doesn't come out of. printf tells me its reading the tie, but the if statement isn't in a loop. any thoughts here to help?
bool print_winner(void)
{
candidate swap={0,0,false};
int cc = candidate_count;
for (int i=0;i<cc; i++)
{
for(int j=0; j<cc-1; j++)
{
if(candidates[j].votes>candidates[j+1].votes)
{
swap=candidates[j];
candidates[j]=candidates[j+1];
candidates[j+1]=swap;
}
}
printf("%s ", candidates[i].name);
printf("%i", candidates[i].votes);
printf("\n");
}
if(candidates[cc-1].votes>(voter_count*.5))
{
printf("The winner is: %s\n",candidates[cc-1].name);
printf("number of votes: %i\n,", candidates[cc-1].votes);
return true;
}
if(candidates[cc-1].votes==candidates[cc-2].votes)
{
printf("tie");
return false;
}
return false;
}
Thanks in advance for any help. Rob