0

I am doing the problem set 3 of CS50, called plurality. When I test my code, it gives me the right anwsers. In other words, it gives me the right winner of the election. If there are two or more winners, it also gives me their names. However, when I test the code with check50 I obtain the following:

:) plurality.c exists
:) plurality compiles
:) vote returns true when given name of first candidate
:) vote returns true when given name of middle candidate
:) vote returns true when given name of last candidate
:) vote returns false when given name of invalid candidate
:) vote produces correct counts when all votes are zero
:) vote produces correct counts after some have already voted
:) vote leaves vote counts unchanged when voting for invalid candidate
:( print_winner identifies Alice as winner of election
    print_winner function did not print winner of election
:( print_winner identifies Bob as winner of election
    print_winner function did not print winner of election
:( print_winner identifies Charlie as winner of election
    print_winner function did not print winner of election
:( print_winner prints multiple winners in case of tie
    print_winner function did not print both winners of election
:( print_winner prints all names when all candidates are tied
    print_winner function did not print all three winners of election

I really don't know what do I have to change. Here is my code:

[snip code]

// Number of candidates
int candidate_count, voter_count;

[snip code] (removed solution code)

2
  • At a quick glance there is an extra space in the first printf of print_winner. So this code would produce "Alice " which does not match "Alice". Commented Sep 2, 2020 at 16:03
  • No, it did not work. But thanks you anyway.
    – Camila
    Commented Sep 2, 2020 at 18:47

1 Answer 1

1

Looks like you changed plurality.c beyond what is allowed per the spec. You've made voter_count a global variable when in the distribution code, it is local to main.

I imagine you did that so you could access its value in your print_winner function, but you'll need to come up with a different algorithm for calculating the winner that doesn't take into account the number of voters. check50 does not have access to that value since it is only in main's scope.

You must log in to answer this question.

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