When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

tirtha_raj's blog

By tirtha_raj, history, 12 months ago, In English

In today's contest(Problem C), I did a pretty silly mistake where I redeclared a variable inside a loop due to which the global variable was not getting updated. Hence I was trying to find the fault in my logic and Spent exactly 1 hour and 40 minutes regretting my life. While going through my solution now I discovered this and now I want to kill myself.

Now in all seriousness how do I stop repeating these silly errors in the heat of the contest? I have been doing these a lot lately.

  • Vote: I like it
  • +10
  • Vote: I do not like it

»
12 months ago, # |
  Vote: I like it +6 Vote: I do not like it

I'd say just keep coding and facing these issues until they're gone. There used to be a time when I also was making way too many stupid mistakes like that and it's mostly over now. The thing is even if it happens few times then next time there is a better chance that you'll pay attention to such stuff when coding.

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

The same happens with me, but at least now we know what not to do. ¯_(ツ)_/¯

»
12 months ago, # |
  Vote: I like it +72 Vote: I do not like it

-Wshadow

  • »
    »
    12 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    what does this mean sir?

    • »
      »
      »
      12 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It warns when a variable is redeclared inside an inner block

»
12 months ago, # |
  Vote: I like it +14 Vote: I do not like it

Don't waste a lot of time, deleting and rewriting the solution can be a good option if you are confident that your approach is correct.

Debugging tools can also be helpful in identifying errors in the code, also using good names for your variable.

I have noticed that many people rely on print statements for debugging. But I believe that debugging tools are highly useful, but they seem to be underrated and rarely recommended, particularly in competitive programming.

  • »
    »
    12 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have "Debug Mode" in my environment which is pretty good to detect any compilation and runtime problems.

    P.S g++ -O3 -std=c++20 -Wall -Wextra -Wshadow -Wformat=2 -Wfloat-equal -Wconversion -Wlogical-op -Wshift-overflow=2 -Wduplicated-cond -Wcast-qual -Wcast-align -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2 -fsanitize=address -fsanitize=undefined -fsanitize=bounds -fno-sanitize-recover -fstack-protector

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

One very simple solution: name your variable in a distinct way. Also if you know how to debug, this mistake should be very noticeable.

»
12 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I would say the more you practice lesser the mistakes would occur and the most important part is that you debug your code for nearly 1 hour and 40 minutes and now whenever you have to declare a variable you will get a reflex in your mind thinking about it and probably you would not commit this type of error in future.