+ 1
Why this program show error?? Give some suggestion about this if anything is wrong in this program???
https://code.sololearn.com/cK7mTEiZJwgc/?ref=app Donna [A^dr3w] Dan Walker Sreejith Hans Larry HAWKEYE
6 Antworten
+ 3
Harsh Agrawal remove the semicolon in line 16 (;)
and declare the variable 'count'(int count).
+ 1
thnxzzz Ulisses Cruz nd can u give more suggestion in this program to make it perfect or something else
+ 1
Dan Walker how does it trigger the same result??? i did not understand....can u elaborate?
0
Maybe you could put the last if statement outside of the while. at the moment we are checking the attempts twice in each loop, but once we exit the while loop we know that attempt >= 3.
Another suggestion is for safety: don't compare == 3 when in fact any number >= 3 would trigger the same result. Imagine a case over a HTTP connection where it gets duplicated and the attempts are incremented twice, so you skip 3 and this check will fail. It's a classic error when making a game and check for the enemy health being equal to 0, but the enemy may skip 0 and go negative, then we haven't handled this case.
If you follow the above, the break statements should just be "return;" to exit the flow of the program. Minor suggestions
0
So, strictly speaking you are interested in the user using 3 attempts or more to have them locked out. At the moment we only look for the third attempt and end the attempt. If they somehow passed this 3 they could try attempt 4, 5, 100 and so on but that piece of code wouldn't display an error, so generally I find it's good to be as inclusive as possible in the range of values you want to disallow.
Another minor suggestion is to indent your code as in Donna's example. When you write code inside a new set of { } indent it a few spaces. this helps the programmer (you) and other readers to see the structure of your code more quickly