3

Pretty straightforward problem. Your vote function ALWAYS returns false, even after registering the vote. If false is returned to main, it'll print the invalid vote message. If vote() succeeds in finding the name, it should return true, not execute a break. Once the name is found, there's no need to do anything else in the function after registering the ...


2

for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes >= maxvote) { maxvote = candidates[i].votes; winner = candidates[i].name; printf("The winner is %s\n", winner); } } the logic of your code to get the max number of votes is correct, but your logic to get the winner is a ...


2

candidate_count is not declared in main. It's a global variable (declared outside of main) on line 20 in the distribution file plurality.c at the time of this writing. The definition of main starts later on line 26. That's why you're able to see candidate_count in other functions. argc, on the other hand, is a local variable that you can reference only ...


2

You've stumbled on a known issue with debug50. It will, under unique circumstances, mask a segmentation fault. But, you can be sure that the seg fault is real. You'll need to isolate the offending line of code the hard way. Read the following for details on ways to do just that: Do YOU know how to find a seg fault?? Advice to new programmers


2

The error means a path that does not take the if branch or the else branch will not return a boolean, since the returns are embedded in those blocks. Think about this comment //if input name does not match with any name . What the program is doing is //if input name does not match this name . If program returns on the first non-matching name, it will not ...


1

So, I see a logic error in this bit of code: if (winner_int < candidates[i].votes) { winner = candidates[i].name; } Since you set winner_int equal to the number of votes that candidate[0] and never change that value, this will not always provide a valid winner in each situation. If candidate[0].votes == 3, candidate[1].votes == 4, and candidate[2]....


1

Yea, in true rubber duck fashion I figured out the problem immediately after posting this..., why would I ever want to += instead of just =...


1

Try this election: 3 candidates: a, b, c and 2 voters who vote for c. It reports a and b as (tied) winners. That's because c's votes are not considered in the first for (int i = 0; i < voter_count; i++), because voter_count is 2 and candidate c is index 2. (Neither is candidate c considered in the second for (int i = 0; i < voter_count; i++), for the ...


1

Segfault is a memory error which is caused due to leakage in memory or aka bytes I need to see the whole code Remember you only need to print the name of the candidate(s) once it's vote matches the max number of votes in the election So first use for-loop to iterate through every candidate to store the max number of votes in the election Then Use other for-...


1

No that's get_string() that function is what you use to prompt the user for their vote, and whatever the user inputs it will be stored in the variable called name. The vote function when called would look like vote(arguments);.


1

The problem with using recursion here, is that it executes x times not only the part with the sorting but also the printing part. If you want to go with recursion you could split into two separate functions: the recursive one, for the sorting part, and other with the printing part. If you want to keep it in one function, you could simply change the value of ...


1

The problem is in this condition in your second 'for' loop: for (int c = 0 ; c < i - candidate_count - 1 ; c++). Let's see: Before execute the second loop i = 0 and c = 0. To run the code 'c < i - candidate_count -1' has to be true. if i = 0, and you substrate candidate_count and 1, the result is always going to be negative so the code in the second ...


1

Consider the for loop in your vote() function: It compares the candidate name with the name introduced by the voter. In the very first pass (i = 0) what happens when the candidate name and the name introduced are not the same?


1

yo"re almost there. You made a small mistake in your implementation of your function print_winner() . If you don't see it I'd suggest the use of debug50 to check the value of your variables at every step of your program.


1

To help facilitate people giving you answers in the future it helps if you can provide more context on the issues you're having. What error you're seeing and what line does it start on. That said you're issues are with your print_winner function. First Issue: You have two loops but you never use j which your code is wrong or you're second loop is ...


1

Your code is incrementing maxvote every time your if statement is true.. The goal is to get the max number of votes and see then check if who are the candidates that have the same number of votes to the max votes you got. int maxvote = 0; for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes > 0) { ...


1

If you know you're printing the right answer, then it must be your formatting when you print. Try removing the space when you print each name. Use this printf ("%s\n", winners[i]); instead of printf ("%s \n", winners[i]);


1

There is no data that will show you what check50 sees. That is because program violates this instruction from the spec: You should not modify anything else in plurality.c other than the implementations of the vote and print_winner functions (and the inclusion of additional header files, if you’d like). check50 uses the distro main function for testing.


Only top voted, non community-wiki answers of a minimum length are eligible