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 ...
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 ...
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 ...
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
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 ...
The checker code renames your main function and appends its own.
The warning exists because main is the only function returning non-void for which the return value is still defined if you don't return a value explicitly (it would return 0 by default). For other functions, it would be up to the compiler what the return value is, often depending on the CPU ...
The return false; should be the last line of your function, after the loop, there is nothing to do in that else.
Rationale: If you have a name match, you know the vote is valid. But if you have a mismatch, it might still be valid, matching another name. So only after the loop, if you haven't had a name match with any candidate, do you know the vote is ...
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
Use other for-...
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);.
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 ...
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 ...
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?
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.
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 ...
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)
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]);
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.
strcpy will copy the characters from one string to another location. You specify winner.name as the target, but that one currently is "". The compiler would translate "" to a string (in this case consisting of just the null terminator) that is stored in some memory that is loaded before main runs, and is read-only (so you don't accidentally overwrite other ...