so I'm doing the plurality right now, and like how I said in the title there is an error about compiling my code I guess It should be something about returning but I still don't have any idea what to do, here is the code that I write down below.
1 Answer
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 have to realize that the compiler is going to look at a function and check every code path to make sure that it returns a value to the calling code. (This doesn't apply when a function returns a void, ie, no return values.)
In this particular case, the compiler looked at the vote() function and discovered that there's at least one way for the function to run and not hit a return statement. Imagine that candidate_count is 0. Then the for loop would never execute and the code would bypass the entire loop. Since there's no return statement after that, the compiler is unhappy and throws that error message.
Byt, you say, candidate_count must always be greater than 0! Well, that may be true, but too bad. The compiler doesn't care. Remember, it's stupid!
Fortunately, there's an easy fix for this error, generally speaking. You just need to make sure that there's a a catch-all return statement at the end. You could insert a return false;
statement after line 81 to cure this specific compiling problem.
Oh, btw, there's also a logic problem that you haven't found yet that's related to this. The for loop will never make more than one pass. I'll let you figure that one out. Just step through the code in your head, or with debug50 ;-)
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)
-
Thanks for your great explanation and answer, my code is now compiling perfectly and It passes all the check50 questions.– IbrahimCommented Jul 16, 2022 at 12:10