3

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 ...


3

Resolved. I feel like a complete idiot. printf("%s \n", winner[i]); should be printf("%s\n", winner[i]);


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

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 ...


2

If you fix the missing semicolon that @DinoCoderSaurus mentioned, this works fine. I suspect you hadn't compiled it after writing your function, so the default return false was still being used.


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


1

It should not seg fault if the election has 9 candidates. Remember 9 is the maximum number of candidates. Any election with fewer will seg fault in the vote function for loop. The loop should only process the number of candidates in the race. There is a variable created in the distro code that can be used to limit the loop.


1

You can edit whatever you need to in order to make the logic work so that the program produces the expected results. If in the end, the code you write produces the same logical output, it should be fine. Just take note of any place where you change this code, because it might be hard to debug if CS50 rejects the logic. That being said, I did not change the ...


1

candidate is the datatype. It is a custom datatyoe (also called a "user-defined" datatype). The name of the variable is candidates plural. You will find the datatype defined either in the code above this declaration code in the screenshot or at the top of one of the linked files; I can't remember at the moment.


1

It is normal. If you take a close look at the code provided, the vote function for now returns false and the main function will print invalid vote if the return value is false. Once implemented, the vote function should return false if the vote is invalid. Hope this helps. If it does then please check the tickmark.


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 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 ...


1

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 ...


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.


1

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 ...


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