0

[Solved] C++ Once again code works properly in My Code Bits but fails Code Coach, don't understand why

Very odd, works in standard Sololearn editor/compiler but fails in the code coach. In My Code Bits if the first word satisfies the task it calls a return and the code ends immediately as I would expect. However in code coach it continues processing. Am I using the return statement incorrectly? Is there another way to terminate the function once the condition is satisfied? Or do I possibly need a function that isn't "void"? The task is at the bottom of the code, pretty self-explanatory. From the Community Code Coach "hard" level. As always, any advice or suggestions to a newbie welcome. As I do all my coding on a phone and have no computer, maybe someone with a better understanding, more experience or different compiler will see the error in my code. https://code.sololearn.com/c3GWvVZMV76F/?ref=app

22nd Nov 2022, 8:44 AM
Scott D
Scott D - avatar
6 Antworten
+ 1
The problem with the first code is that it returns on every palindrome found. You can use that, if the main function stops at that point. But it gets weird to treat the two conditions - palindrome and not palindrome with different logic. This is an important step in making code unmantainable. For the second one, you don't need the first while loop, since the 4 inputs are already inside the for loop. You don't need the second string variable, since it's a copy of the first one, and none of them ever change. Lastly, the inner while loop could be a for loop, and you can get rid of the 2nd counter (j) by simple arithmetic inside the string indexes in the comparison. Another recommendation is to use meaningful variable names. x, i, j, etc. work in small codes, but will be a headache when you code more complex stuff.
22nd Nov 2022, 2:14 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado [Edited] I think I (properly?) implemented your suggestions in both examples. They seem to work (both approaches passed the code coach) and look much cleaner thanks to your advice. And probably more efficient. For the second one, the first loop is changed to a while loop, the obvious benefit being it allows for any number of tests depending on how many signs (words) are entered. The inner while loop was switched to a for lool as suggested. I changed the string name but am unsure why descriptive names for counters (x, i) inside a for/while loop as well as the index would be beneficial. Is there a reason? As always thank you for the reply & tips, I realize developing good habits from the start is beneficial. And at this point I would have never seen the errors nor the resolutions and room for improvement. I have much to learn! Attached is working void function version. https://code.sololearn.com/ctIT85baZ3sY/?ref=app
22nd Nov 2022, 8:34 PM
Scott D
Scott D - avatar
+ 1
Great! It means you learned. For the descriptive names, the reason is readability. Their meanings seem obvious for you when you're writing, but maybe aren't for other people, or for yourself a couple months later. This is a simple code, but you got the idea: make it a habit, and you're good for more complex stuff.
23rd Nov 2022, 2:04 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Another points: 1. You have a void function which does everything, and a main function which only calls the void one. So the additional function is unnecessary - you can have everything in main itself. 2. In C/C++ style, we use names (for variables, functions, methods, etc.) with lower_case_and_underscores, instead of javaCase. Not vital, but makes it consistent with the majority of others.
23rd Nov 2022, 2:10 AM
Emerson Prado
Emerson Prado - avatar
0
I resolved the issue with Code Coach by moving all the code into int main, but my questions still stand. I'll try later to move it into a function outside of int main as I'm trying to get more proficient with that. And not that it's resolved any suggestions as to how I can clean it up or improve it are always welcome. Here's the working code: https://code.sololearn.com/cpvG9n1Mv0O1/?ref=app
22nd Nov 2022, 11:32 AM
Scott D
Scott D - avatar
0
Emerson Prado Understood, that's why I did two versions, the one with the outer function and the one completely in int main. I was just trying to get practice calling functions which is very new to me. Thanks again for the helpful tips!
23rd Nov 2022, 2:25 AM
Scott D
Scott D - avatar