Skip to main content
3 votes
Accepted

cs50 plurality non-void function does not return a value in all control paths error

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 ...
DinoCoderSaurus's user avatar
2 votes
Accepted

pset 3 plurality code help

for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes >= maxvote) { maxvote = candidates[i].votes; winner = candidates[i].name; ...
Ojou Nii's user avatar
  • 731
2 votes

Plurality print_winner function gives one error

Try an election with 3 candidates, A B C, and 6 voters, with the result A 3, B 1, C 2. The self-evident winner is A with 3 votes. The first print_winner function will return C. On the first iteration ...
DinoCoderSaurus's user avatar
1 vote
Accepted

check50 for plurality

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; ...
DinoCoderSaurus's user avatar
1 vote
Accepted

pset3 Plurality

https://cs50.stackexchange.com/a/38819/25714 Check this post on seg fault (plurality) that I have answered. If found to help be sure to tick the answer to keep other smiling too 😃
Shehroz Ali's user avatar
1 vote

pset 3 plurality code help

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 ...
Ojou Nii's user avatar
  • 731
1 vote

Plurality vote strange errors

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 ...
DinoCoderSaurus's user avatar
1 vote
Accepted

Plurality segmentation fault

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 ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

why am I getting a segmentation fault?

If there are 3 candidates, as in the example, what are the indexes of the candidates array? What is the maximum i that this for (int i = 0; i < candidate_count+1; i++) will try to process (...
DinoCoderSaurus's user avatar
1 vote

why does it not return a value for bool?

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 ...
Blauelf's user avatar
  • 21k
1 vote

pset3: Plurality - check50 errors despite code working

This if (candidates[i].votes > candidates[winnerPosition[0]].votes) will produce a memory violation. valgrind would give an error complaining about an unitialized value. The winnerPosition array is ...
DinoCoderSaurus's user avatar
1 vote
Accepted

Errors in check50 with plurality.c?

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). ...
DinoCoderSaurus's user avatar
1 vote
Accepted

Pset 3 plurality compiling error

First, remember that compilers are very dumb! They don't do logical analysis of code to determine that certain things can't happen (like the number of candidates being 0.) Now, with that in mind, you ...
Cliff B's user avatar
  • 69.1k
1 vote
Accepted

Pset3 Plurality - not able to get the right output

You're right, the problem is in this code. It's your "bubble sort" code. It's not actually sorting the list, it's only bringing the top winner to the end, and it's corrupting the list in ...
Cliff B's user avatar
  • 69.1k
1 vote
Accepted

Pset 3 Plurality "use of undeclared identifier"

Without seeing the entire code, we can only guess, but the most obvious answer is that candidate_count is declared as a global variable, but voter_count is not. Global vars can be used anywhere, ...
Cliff B's user avatar
  • 69.1k
1 vote
Accepted

Pset3, Plurality - seems to be skipping the first the return true for each first vote cast for a candidate

When used this way candidates[i].votes ++;, ++ is the post-increment operator, therefore the value is incremented after the expression is evaluated. The first vote will evaluate return true && ...
DinoCoderSaurus's user avatar
1 vote

plurality pset3

The loop should be like this: bool vote(string name) { for (int i = 0; i < candidate_count; i++) { if (strcmp(candidates[i].name, name) == 0) { candidates[i].votes++; return true; } } return false; ...
JacK's user avatar
  • 11
1 vote
Accepted

Plurality CS50 Week3 pset3

When You are using else if It executes either of the statements. Remove else if statement . Make another loop(Independent of the previous loop) and then print the name of candidate if it has maximum ...
MWD's user avatar
  • 86

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