Here is the plurality problem from CS50 I can't find fix the problem, please help
PS I have another problem now, if there are 2 voters,1 is Alice,2nd is Bob the program should print out both as winners, but it gives out first candidate as a winner,how to deal with it?
//Update vote totals given a new vote
bool vote(string name){
for(int i=0;i<candidate_count; i++){
if(strcmp(candidates[i].name,name)==0){
candidates[i].vote++;
return true;
}
}
return false;
}
//Print the winner (ot winners)of the election
void print_winner(void){
int MaxVote=0;
string Winner;
for(int i=0;i<candidate_count; i++){
if(candidates[i].vote>MaxVote){
MaxVote=candidates[i].vote;
Winner=candidates[i].name;
}
}
for(int i=0;i<candidate_count; i++){
if(candidates[i].vote==MaxVote){
printf("%s\n",Winner);
return;
}
}
}
Here is the output
~/pset3/plurality/ $ ./plurality Alice Bob
Number of voters: 2
Vote: Alice
Vote: Bob
Alice
I just finished this problem and what you need to do is print the first winner in your first "for" loop (in line 8 of your example) so it will print the first winner, then go back and print anyone who tied with them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.